728x90
- VPC, ec2, alb 구성 실습
[vpc.tf]
provider "aws" {
region = "ap-northeast-2"
}
variable "name" {
default = "ssungz"
}
variable "cidr" {
type = list(string)
default = ["10.100.0.0/16", "10.100.1.0/24", "10.100.2.0/24" ]
}
variable "az" {
type = list(string)
default = [ "ap-northeast-2a", "ap-northeast-2c" ]
}
resource "aws_vpc" "ssungz-vpc" {
cidr_block = var.cidr[0]
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "${var.name}-vpc"
}
}
resource "aws_subnet" "pub-subnet-1" {
vpc_id = aws_vpc.ssungz-vpc.id
cidr_block = var.cidr[1]
availability_zone = var.az[0]
tags = {
Name = "public-subnet-1"
}
}
resource "aws_subnet" "pub-subnet-2" {
vpc_id = aws_vpc.ssungz-vpc.id
cidr_block = var.cidr[2]
availability_zone = var.az[1]
tags = {
Name = "pub-subnet-2"
}
}
resource "aws_internet_gateway" "ssungz-igw" {
vpc_id = aws_vpc.ssungz-vpc.id
tags = {
Name = "ssungz-igw"
}
}
# resource "aws_internet_gateway_attachment" "igw-att" {
# internet_gateway_id = aws_internet_gateway.ssungz-igw.id
# vpc_id = aws_vpc.ssungz-vpc.id
# }
resource "aws_route" "ssungz-route" {
route_table_id = aws_route_table.ssungz-rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.ssungz-igw.id
}
resource "aws_route_table" "ssungz-rt" {
vpc_id = aws_vpc.ssungz-vpc.id
tags = {
Name = "${var.name}-rt"
}
}
resource "aws_route_table_association" "ssungz-rt-asso-1" {
subnet_id = aws_subnet.pub-subnet-1.id
route_table_id = aws_route_table.ssungz-rt.id
}
resource "aws_route_table_association" "ssungz-rt-asso-2" {
subnet_id = aws_subnet.pub-subnet-2.id
route_table_id = aws_route_table.ssungz-rt.id
}
[ec2.tf]
data "aws_ami" "myami" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
}
data "local_file" "init-sh" {
filename = "${path.module}/start-http.sh"
}
resource "aws_instance" "web-srv" {
count = 2
instance_type = "t2.micro"
ami = data.aws_ami.myami.id
user_data_replace_on_change = true
depends_on = [
aws_internet_gateway.ssungz-igw
]
associate_public_ip_address = true
vpc_security_group_ids = [aws_security_group.web-sg.id]
subnet_id = element([aws_subnet.pub-subnet-1.id, aws_subnet.pub-subnet-2.id], count.index)
user_data = data.local_file.init-sh.content
tags = {
Name = "${var.name}-web-srv-${count.index}"
}
}
[start-http.sh]
#!/bin/bash
HOSTNAME=`hostname -f`
yum install -y httpd
echo "hoon-test-web \n $HOSTNAME " > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
[sg.tf]
resource "aws_security_group" "web-sg" {
vpc_id = aws_vpc.ssungz-vpc.id
name = "ssungz SG"
}
resource "aws_security_group_rule" "web-inbound" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.web-sg.id
}
resource "aws_security_group_rule" "web-outbound" {
type = "egress"
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.web-sg.id
}
[alb.tf]
resource "aws_lb" "web-lb" {
name = "${var.name}-web-lb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.web-sg.id]
subnets = [aws_subnet.pub-subnet-1.id, aws_subnet.pub-subnet-2.id]
enable_deletion_protection = false
tags = {
Enviornment = "test"
}
}
resource "aws_lb_listener" "web-listen" {
load_balancer_arn = aws_lb.web-lb.arn
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = aws_lb_target_group.web-tg.arn
type = "forward"
}
}
resource "aws_lb_listener_rule" "web-listener-rule" {
listener_arn = aws_lb_listener.web-listen.arn
priority = 100
action {
type = "forward"
target_group_arn = aws_lb_target_group.web-tg.arn
}
condition {
path_pattern {
values = ["/"]
}
}
}
resource "aws_lb_target_group" "web-tg" {
name = "web-alb-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.ssungz-vpc.id
}
resource "aws_lb_target_group_attachment" "web-tg-att" {
for_each = { for idx, instance in aws_instance.web-srv : idx => instance.id }
target_group_arn = aws_lb_target_group.web-tg.arn
target_id = each.value
port = 80
}
output "alb_dns" {
value = aws_lb.web-lb.dns_name
}
[ssungz-web-srv-0]
[ssungz-web-srv-1]
728x90
'Cloud > Terraform' 카테고리의 다른 글
[T101] Terraform 101 Study 실습(5) - for (1) | 2024.07.03 |
---|---|
[T101] Terraform 101 Study 실습(4) - data resource (0) | 2024.07.02 |
[T101] Terraform 101 Study 실습(2) - precondition (0) | 2024.07.01 |
[T101] Terraform 101 Study 실습(1) - ec2 배포 (0) | 2024.07.01 |
[T101] Terraform 101 Study 3주차 (2) (0) | 2024.06.30 |