-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmain.tf
226 lines (196 loc) · 6.38 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
locals {
tags = var.tags == null ? {} : var.tags
availability_zones = var.availability_zones != null ? var.availability_zones : slice(data.aws_availability_zones.available.names, 0, var.availability_zones_count)
}
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = merge(
{
"Name" = "${var.name_prefix}-vpc"
},
local.tags,
)
lifecycle {
ignore_changes = [tags]
}
}
resource "aws_vpc_endpoint" "s3" {
vpc_id = aws_vpc.vpc.id
service_name = "com.amazonaws.${data.aws_region.current.name}.s3"
}
resource "aws_subnet" "public_subnet" {
count = length(local.availability_zones)
vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(var.vpc_cidr, length(local.availability_zones) * 2, count.index)
availability_zone = local.availability_zones[count.index]
tags = merge(
{
"Name" = join("-", [var.name_prefix, "subnet", "public${count.index + 1}", local.availability_zones[count.index]])
},
local.tags,
)
lifecycle {
ignore_changes = [tags]
}
}
resource "aws_subnet" "private_subnet" {
count = length(local.availability_zones)
vpc_id = aws_vpc.vpc.id
cidr_block = cidrsubnet(var.vpc_cidr, length(local.availability_zones) * 2, count.index + length(local.availability_zones))
availability_zone = local.availability_zones[count.index]
tags = merge(
{
"Name" = join("-", [var.name_prefix, "subnet", "private${count.index + 1}", local.availability_zones[count.index]])
},
local.tags,
)
lifecycle {
ignore_changes = [tags]
}
}
#
# Internet gateway
#
resource "aws_internet_gateway" "internet_gateway" {
vpc_id = aws_vpc.vpc.id
tags = merge(
{
"Name" = "${var.name_prefix}-igw"
},
local.tags,
)
lifecycle {
ignore_changes = [tags]
}
}
#
# Elastic IPs for NAT gateways
#
resource "aws_eip" "eip" {
count = length(local.availability_zones)
domain = "vpc"
tags = merge(
{
"Name" = join("-", [var.name_prefix, "eip", local.availability_zones[count.index]])
},
local.tags,
)
lifecycle {
ignore_changes = [tags]
}
}
#
# NAT gateways
#
resource "aws_nat_gateway" "public_nat_gateway" {
count = length(local.availability_zones)
allocation_id = aws_eip.eip[count.index].id
subnet_id = aws_subnet.public_subnet[count.index].id
tags = merge(
{
"Name" = join("-", [var.name_prefix, "nat", "public${count.index}", local.availability_zones[count.index]])
},
local.tags,
)
lifecycle {
ignore_changes = [tags]
}
}
#
# Route tables
#
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.vpc.id
tags = merge(
{
"Name" = "${var.name_prefix}-public"
},
local.tags,
)
lifecycle {
ignore_changes = [tags]
}
}
resource "aws_route_table" "private_route_table" {
count = length(local.availability_zones)
vpc_id = aws_vpc.vpc.id
tags = merge(
{
"Name" = join("-", [var.name_prefix, "rtb", "private${count.index}", local.availability_zones[count.index]])
},
local.tags,
)
lifecycle {
ignore_changes = [tags]
}
}
#
# Routes
#
# Send all IPv4 traffic to the internet gateway
resource "aws_route" "ipv4_egress_route" {
route_table_id = aws_route_table.public_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.internet_gateway.id
depends_on = [aws_route_table.public_route_table]
}
# Send all IPv6 traffic to the internet gateway
resource "aws_route" "ipv6_egress_route" {
route_table_id = aws_route_table.public_route_table.id
destination_ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.internet_gateway.id
depends_on = [aws_route_table.public_route_table]
}
# Send private traffic to NAT
resource "aws_route" "private_nat" {
count = length(local.availability_zones)
route_table_id = aws_route_table.private_route_table[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.public_nat_gateway[count.index].id
depends_on = [aws_route_table.private_route_table, aws_nat_gateway.public_nat_gateway]
}
# Private route for vpc endpoint
resource "aws_vpc_endpoint_route_table_association" "private_vpc_endpoint_route_table_association" {
count = length(local.availability_zones)
route_table_id = aws_route_table.private_route_table[count.index].id
vpc_endpoint_id = aws_vpc_endpoint.s3.id
}
#
# Route table associations
#
resource "aws_route_table_association" "public_route_table_association" {
count = length(local.availability_zones)
subnet_id = aws_subnet.public_subnet[count.index].id
route_table_id = aws_route_table.public_route_table.id
}
resource "aws_route_table_association" "private_route_table_association" {
count = length(local.availability_zones)
subnet_id = aws_subnet.private_subnet[count.index].id
route_table_id = aws_route_table.private_route_table[count.index].id
}
# This resource is used in order to add dependencies on all resources
# Any resource uses this VPC ID, must wait to all resources creation completion
resource "time_sleep" "vpc_resources_wait" {
create_duration = "10s"
triggers = {
vpc_id = aws_vpc.vpc.id
cidr_block = aws_vpc.vpc.cidr_block
ipv4_egress_route_id = aws_route.ipv4_egress_route.id
ipv6_egress_route_id = aws_route.ipv6_egress_route.id
private_nat_ids = jsonencode([for value in aws_route.private_nat : value.id])
private_vpc_endpoint_route_table_association_ids = jsonencode([for value in aws_vpc_endpoint_route_table_association.private_vpc_endpoint_route_table_association : value.id])
public_route_table_association_ids = jsonencode([for value in aws_route_table_association.public_route_table_association : value.id])
private_route_table_association_ids = jsonencode([for value in aws_route_table_association.private_route_table_association : value.id])
}
}
data "aws_region" "current" {}
data "aws_availability_zones" "available" {
state = "available"
# New configuration to exclude Local Zones
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}