-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-vpc.tf
89 lines (72 loc) · 2.04 KB
/
aws-vpc.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
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
}
data "aws_availability_zones" "available" {
state = "available"
}
resource "aws_subnet" "public" {
count = 3
vpc_id = aws_vpc.main.id
cidr_block = "10.0.${count.index}.0/24"
availability_zone = element(data.aws_availability_zones.available.names, count.index)
map_public_ip_on_launch = true
}
resource "aws_subnet" "private" {
count = 3
vpc_id = aws_vpc.main.id
cidr_block = "10.0.${count.index + 3}.0/24"
availability_zone = element(data.aws_availability_zones.available.names, count.index)
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
}
resource "aws_nat_gateway" "nat" {
count = 1
allocation_id = aws_eip.nat[count.index].id
subnet_id = element(aws_subnet.public.*.id, count.index)
depends_on = [aws_internet_gateway.gw]
}
resource "aws_eip" "nat" {
count = 1
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
}
resource "aws_route_table_association" "public" {
count = 3
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = aws_route_table.public.id
}
resource "aws_route_table" "private" {
count = 3
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
# nat_gateway_id = element(aws_nat_gateway.nat.*.id, count.index)
# FIXME hardcoded single NAT gateway
nat_gateway_id = aws_nat_gateway.nat[0].id
}
}
resource "aws_route_table_association" "private" {
count = 3
subnet_id = element(aws_subnet.private.*.id, count.index)
route_table_id = element(aws_route_table.private.*.id, count.index)
}
# Output the VPC ID
output "vpc_id" {
value = aws_vpc.main.id
}
# Output the list of private subnet IDs
output "private_subnet_ids" {
value = aws_subnet.private.*.id
}
# Output the list of public subnet IDs
output "public_subnet_ids" {
value = aws_subnet.public.*.id
}