-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.tf
105 lines (87 loc) · 2.6 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
data "aws_availability_zones" "available_zones" {
state = "available"
}
resource "aws_vpc" "default" {
cidr_block = "10.32.0.0/16"
}
resource "aws_subnet" "public" {
count = 2
cidr_block = cidrsubnet(aws_vpc.default.cidr_block, 8, 2 + count.index)
availability_zone = data.aws_availability_zones.available_zones.names[count.index]
vpc_id = aws_vpc.default.id
map_public_ip_on_launch = true
}
resource "aws_subnet" "private" {
count = 2
cidr_block = cidrsubnet(aws_vpc.default.cidr_block, 8, count.index)
availability_zone = data.aws_availability_zones.available_zones.names[count.index]
vpc_id = aws_vpc.default.id
}
resource "aws_internet_gateway" "gateway" {
vpc_id = aws_vpc.default.id
}
resource "aws_route" "internet_access" {
route_table_id = aws_vpc.default.main_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gateway.id
}
resource "aws_eip" "gateway" {
count = 2
vpc = true
depends_on = [aws_internet_gateway.gateway]
}
resource "aws_nat_gateway" "gateway" {
count = 2
subnet_id = element(aws_subnet.public.*.id, count.index)
allocation_id = element(aws_eip.gateway.*.id, count.index)
}
resource "aws_route_table" "private" {
count = 2
vpc_id = aws_vpc.default.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = element(aws_nat_gateway.gateway.*.id, count.index)
}
}
resource "aws_route_table_association" "private" {
count = 2
subnet_id = element(aws_subnet.private.*.id, count.index)
route_table_id = element(aws_route_table.private.*.id, count.index)
}
resource "aws_security_group" "lb" {
name = "example-alb-security-group"
vpc_id = aws_vpc.default.id
ingress {
protocol = "tcp"
from_port = 80
to_port = 80
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_lb" "default" {
name = "example-lb"
subnets = aws_subnet.public.*.id
security_groups = [aws_security_group.lb.id]
}
resource "aws_lb_target_group" "hello_world" {
name = "example-target-group"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.default.id
target_type = "ip"
}
resource "aws_lb_listener" "hello_world" {
load_balancer_arn = aws_lb.default.id
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = aws_lb_target_group.hello_world.id
type = "forward"
}
}