20230405
# main.tf
provider "aws" {
region = "ap-northeast-2"
}
module "module_resource" {
source = "./resource"
}
# vpc - main.tf
// vpc 생성
resource "aws_vpc" "myvpc01" {
cidr_block = "100.100.0.0/16"
enable_dns_hostnames = true
tags = {
Name = "myvpc01"
}
}
// subnet 생성
resource "aws_subnet" "myvpc01_subnet1" {
vpc_id = aws_vpc.myvpc01.id
cidr_block = "100.100.10.0/24"
availability_zone = "ap-northeast-2a"
map_public_ip_on_launch = true
tags = {
Name = "myvpc01_subnet1"
}
}
resource "aws_subnet" "myvpc01_subnet2" {
vpc_id = aws_vpc.myvpc01.id
cidr_block = "100.100.20.0/24"
availability_zone = "ap-northeast-2b"
map_public_ip_on_launch = true
tags = {
Name = "myvpc01_subnet2"
}
}
resource "aws_subnet" "myvpc01_subnet3" {
vpc_id = aws_vpc.myvpc01.id
cidr_block = "100.100.30.0/24"
availability_zone = "ap-northeast-2c"
map_public_ip_on_launch = true
tags = {
Name = "myvpc01_subnet3"
}
}
resource "aws_subnet" "myvpc01_subnet4" {
vpc_id = aws_vpc.myvpc01.id
cidr_block = "100.100.40.0/24"
availability_zone = "ap-northeast-2d"
map_public_ip_on_launch = true
tags = {
Name = "myvpc01_subnet4"
}
}
// internet gateway
resource "aws_internet_gateway" "myig01" {
vpc_id = aws_vpc.myvpc01.id
}
// routing table
resource "aws_default_route_table" "myrt01" {
default_route_table_id = aws_vpc.myvpc01.default_route_table_id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.myig01.id
}
}
// security group
resource "aws_security_group" "mysg01" {
vpc_id = aws_vpc.myvpc01.id
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}
ingress {
description = "back"
from_port = 8000
to_port = 8000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "mysql"
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
// 변수 생성
output "my_security_group_id" {
value = aws_security_group.mysg01.id
}
output "my_vpc_subnet1_id" {
value = aws_subnet.myvpc01_subnet1.id
}
output "my_vpc_subnet2_id" {
value = aws_subnet.myvpc01_subnet3.id
}
output "my_vpc_id" {
value = aws_vpc.myvpc01.id
}
# resource - main.tf
// vpc 모듈 호출
module "module_vpc" {
source = "../vpc"
}
// EC2 instance 생성
resource "aws_instance" "web" {
ami = "ami-0c6e5afdd23291f73"
instance_type = "t2.micro"
vpc_security_group_ids = ["${module.module_vpc.my_security_group_id}"]
subnet_id = "${module.module_vpc.my_vpc_subnet1_id}"
key_name = "first"
tags = {
group = "web"
}
}
resource "aws_instance" "web2" {
ami = "ami-0c6e5afdd23291f73"
instance_type = "t2.micro"
vpc_security_group_ids = ["${module.module_vpc.my_security_group_id}"]
subnet_id = "${module.module_vpc.my_vpc_subnet2_id}"
key_name = "first"
tags = {
group = "web"
}
}
// application load balancer 생성
resource "aws_lb" "myalb01" {
name = "myalb01"
internal = false
load_balancer_type = "application"
security_groups = ["${module.module_vpc.my_security_group_id}"]
subnets = ["${module.module_vpc.my_vpc_subnet1_id}", "${module.module_vpc.my_vpc_subnet2_id}"]
enable_cross_zone_load_balancing = true
}
// target group 생성
resource "aws_alb_target_group" "mytarget01" {
name = "mytarget01"
port = 80
protocol = "HTTP"
vpc_id = "${module.module_vpc.my_vpc_id}"
target_type = "instance"
}
// target group - EC2 연결
resource "aws_alb_target_group_attachment" "att01" {
target_group_arn = aws_alb_target_group.mytarget01.arn
target_id = aws_instance.web.id
port = 80
}
resource "aws_alb_target_group_attachment" "att02" {
target_group_arn = aws_alb_target_group.mytarget01.arn
target_id = aws_instance.web2.id
port = 80
}
// load balancer & target group 연결
resource "aws_alb_listener" "alb_listener01" {
load_balancer_arn = aws_lb.myalb01.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_alb_target_group.mytarget01.arn
}
}
# RDS
// db subnet 지정
resource "aws_db_subnet_group" "mydb_subnet_group" {
name = "mydb_subnet_group"
subnet_ids = ["${module.module_vpc.my_vpc_subnet1_id}", "${module.module_vpc.my_vpc_subnet2_id}"]
}
// RDS 생성
resource "aws_db_instance" "mydb01" {
vpc_security_group_ids = ["${module.module_vpc.my_security_group_id}"]
db_subnet_group_name = aws_db_subnet_group.mydb_subnet_group.name
allocated_storage = 20
identifier = "mydb01"
db_name = "mydb"
engine = "mysql"
engine_version = "8.0.32"
instance_class = "db.t3.micro"
username = "admin"
password = "qwer1234"
parameter_group_name = "default.mysql8.0"
publicly_accessible = true
skip_final_snapshot = true
multi_az = false
}
resource "aws_db_instance" "mydb02" {
vpc_security_group_ids = ["${module.module_vpc.my_security_group_id}"]
db_subnet_group_name = aws_db_subnet_group.mydb_subnet_group.name
allocated_storage = 20
identifier = "mydb02"
db_name = "mydb"
engine = "mysql"
engine_version = "8.0.32"
instance_class = "db.t3.micro"
username = "admin"
password = "qwer1234"
parameter_group_name = "default.mysql8.0"
publicly_accessible = true
skip_final_snapshot = true
multi_az = false
}
'CLOUD > [P] 실시간 채팅 프로그램' 카테고리의 다른 글
[WebSocket/AWS] WebSocket API - Lambda 함수 작성 (0) | 2023.04.13 |
---|---|
[WebSocket/AWS] 주제 선정 (0) | 2023.04.05 |
[WebSocket/AWS] AWS Cloud Architecture 구상도 (0) | 2023.04.04 |
[WebSocket/Django] Chatting : DB 불러오기 (0) | 2023.03.23 |
[WebSocket/Django] Chatting : DB 저장하기 (0) | 2023.03.23 |