176 lines
4.0 KiB
HCL
176 lines
4.0 KiB
HCL
provider "aws" {
|
||
region = "ap-northeast-1"
|
||
}
|
||
|
||
resource "aws_vpc" "demo_vpc_main" {
|
||
cidr_block = "10.9.0.0/16"
|
||
enable_dns_support = true
|
||
enable_dns_hostnames = true
|
||
tags = {
|
||
Name = "demo_vpc_main"
|
||
}
|
||
}
|
||
|
||
resource "aws_subnet" "demo_subnet_public_1" {
|
||
vpc_id = aws_vpc.demo_vpc_main.id
|
||
cidr_block = "10.9.1.0/24"
|
||
map_public_ip_on_launch = true
|
||
availability_zone = "ap-northeast-1a"
|
||
|
||
tags = {
|
||
Name = "demo_subnet_public_1"
|
||
}
|
||
}
|
||
|
||
resource "aws_subnet" "demo_subnet_public_2" {
|
||
vpc_id = aws_vpc.demo_vpc_main.id
|
||
cidr_block = "10.9.2.0/24"
|
||
map_public_ip_on_launch = true
|
||
availability_zone = "ap-northeast-1c"
|
||
|
||
tags = {
|
||
Name = "demo_subnet_public_2"
|
||
}
|
||
}
|
||
|
||
resource "aws_internet_gateway" "demo_igw_main" {
|
||
vpc_id = aws_vpc.demo_vpc_main.id
|
||
tags = {
|
||
Name = "demo_igw_main"
|
||
}
|
||
}
|
||
|
||
resource "aws_route_table" "demo_route_table_main" {
|
||
vpc_id = aws_vpc.demo_vpc_main.id
|
||
|
||
route {
|
||
cidr_block = "0.0.0.0/0"
|
||
gateway_id = aws_internet_gateway.demo_igw_main.id
|
||
}
|
||
|
||
tags = {
|
||
Name = "demo_route_table_main"
|
||
}
|
||
}
|
||
|
||
resource "aws_route_table_association" "demo_assoc_public_1" {
|
||
subnet_id = aws_subnet.demo_subnet_public_1.id
|
||
route_table_id = aws_route_table.demo_route_table_main.id
|
||
}
|
||
|
||
resource "aws_route_table_association" "demo_assoc_public_2" {
|
||
subnet_id = aws_subnet.demo_subnet_public_2.id
|
||
route_table_id = aws_route_table.demo_route_table_main.id
|
||
}
|
||
|
||
resource "aws_security_group" "demo_sg_web" {
|
||
vpc_id = aws_vpc.demo_vpc_main.id
|
||
|
||
ingress {
|
||
from_port = 80
|
||
to_port = 80
|
||
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"]
|
||
}
|
||
|
||
tags = {
|
||
Name = "demo_sg_web"
|
||
}
|
||
}
|
||
|
||
resource "aws_instance" "demo_web_1" {
|
||
ami = "ami-0f75d1a8c9141bd00"
|
||
instance_type = "t2.micro"
|
||
subnet_id = aws_subnet.demo_subnet_public_1.id
|
||
security_groups = [aws_security_group.demo_sg_web.id]
|
||
|
||
user_data = <<-EOF
|
||
#!/bin/bash
|
||
yum update -y
|
||
yum install -y httpd
|
||
echo "<html><body><h1>Terraformで作成されたWebサーバ1</h1></body></html>" > /var/www/html/index.html
|
||
systemctl enable httpd
|
||
systemctl start httpd
|
||
EOF
|
||
|
||
tags = {
|
||
Name = "demo_web_1"
|
||
}
|
||
}
|
||
|
||
resource "aws_instance" "demo_web_2" {
|
||
ami = "ami-0f75d1a8c9141bd00"
|
||
instance_type = "t2.micro"
|
||
subnet_id = aws_subnet.demo_subnet_public_2.id
|
||
security_groups = [aws_security_group.demo_sg_web.id]
|
||
|
||
user_data = <<-EOF
|
||
#!/bin/bash
|
||
yum update -y
|
||
yum install -y httpd
|
||
echo "<html><body><h1>Terraformで作成されたWebサーバ2</h1></body></html>" > /var/www/html/index.html
|
||
systemctl enable httpd
|
||
systemctl start httpd
|
||
EOF
|
||
|
||
tags = {
|
||
Name = "demo_web_2"
|
||
}
|
||
}
|
||
|
||
resource "aws_lb" "demo_lb_main" {
|
||
name = "demo-lb-main"
|
||
internal = false
|
||
load_balancer_type = "application"
|
||
security_groups = [aws_security_group.demo_sg_web.id]
|
||
subnets = [
|
||
aws_subnet.demo_subnet_public_1.id,
|
||
aws_subnet.demo_subnet_public_2.id
|
||
]
|
||
|
||
tags = {
|
||
Name = "demo_lb_main"
|
||
}
|
||
}
|
||
|
||
resource "aws_lb_target_group" "demo_tg_web" {
|
||
name = "demo-tg-web"
|
||
port = 80
|
||
protocol = "HTTP"
|
||
vpc_id = aws_vpc.demo_vpc_main.id
|
||
|
||
tags = {
|
||
Name = "demo_tg_web"
|
||
}
|
||
}
|
||
|
||
resource "aws_lb_target_group_attachment" "demo_attach_web_1" {
|
||
target_group_arn = aws_lb_target_group.demo_tg_web.arn
|
||
target_id = aws_instance.demo_web_1.id
|
||
port = 80
|
||
}
|
||
|
||
resource "aws_lb_target_group_attachment" "demo_attach_web_2" {
|
||
target_group_arn = aws_lb_target_group.demo_tg_web.arn
|
||
target_id = aws_instance.demo_web_2.id
|
||
port = 80
|
||
}
|
||
|
||
resource "aws_lb_listener" "demo_listener_http" {
|
||
load_balancer_arn = aws_lb.demo_lb_main.arn
|
||
port = 80
|
||
protocol = "HTTP"
|
||
|
||
default_action {
|
||
type = "forward"
|
||
target_group_arn = aws_lb_target_group.demo_tg_web.arn
|
||
}
|
||
}
|