-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
244 lines (200 loc) · 9.19 KB
/
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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# vpc.tf
# VPC and subnets for use with an application
##################################
# VPC
# The VPC for this application
resource "aws_vpc" "vpc" {
cidr_block = "${var.vpcCidr}"
enable_dns_hostnames = true
enable_dns_support = true
tags = "${merge( local.default_tags, map (
"Name", "vpc-${var.deptName}-${var.appName}-${var.envName}-${var.vpcType}-${var.region}",
"Ci", ""
))}"
}
# The the default route table for VPC so that we can add tags to it
resource "aws_default_route_table" "vpc_default_routetable" {
default_route_table_id = "${aws_vpc.vpc.main_route_table_id}"
tags = "${merge( local.default_tags, map (
"Name", "rt-vpc-${var.deptName}-${var.appName}-${var.envName}-${var.region}",
"Ci", ""
))}"
}
# Create DHCP Options for private and mixed VPCs
resource "aws_vpc_dhcp_options" "dhcp_opts" {
count = "${var.vpcType != "public" && var.create_dhcp_opts ? 1 : 0}"
domain_name = "${var.dnsSuffix}"
domain_name_servers = "${var.dnsServers}"
ntp_servers = "${var.ntpServers}"
tags = "${merge( local.default_tags, map (
"Name", "dhcpopts-vpc-${var.deptName}-${var.appName}-${var.envName}-${var.region}",
"Ci", ""
))}"
}
# Associate DHCP Options to VPC for private and mixed VPCs
resource "aws_vpc_dhcp_options_association" "dhcp_opts_assoc" {
count = "${var.vpcType != "public" && var.create_dhcp_opts ? 1 : 0}"
dhcp_options_id = "${aws_vpc_dhcp_options.dhcp_opts.id}"
vpc_id = "${aws_vpc.vpc.id}"
}
##################################
# PUBLIC Internet
# Create an Internet Gateway for the VPC so the web subnets can talk to internet
# if var.vpcType != private
resource "aws_internet_gateway" "igw" {
count = "${var.vpcType != "private" ? 1 : 0}"
vpc_id = "${aws_vpc.vpc.id}"
tags = "${merge( local.default_tags, map (
"Name", "igw-${var.deptName}-${var.appName}-${var.envName}-${var.vpcType}-${var.region}",
"Ci", ""
))}"
}
# Create a single public route table
# if var.vpcType == public
resource "aws_route_table" "public_routetable" {
count = "${var.vpcType != "private" ? 1 : 0}"
vpc_id = "${aws_vpc.vpc.id}"
tags = "${merge( local.default_tags, map (
"Name", "rt-public-${var.deptName}-${var.appName}-${var.envName}-${var.region}",
"Ci", ""
))}"
}
# Add a rule to the public route table, making the Internet GW the default route
# if var.vpcType != private and var.create_web_subnets == true
resource "aws_route" "igw_route" {
count = "${var.vpcType != "private" && var.create_web_subnets ? 1 : 0}"
route_table_id = "${aws_route_table.public_routetable.id}"
gateway_id = "${aws_internet_gateway.igw.id}"
destination_cidr_block = "0.0.0.0/0"
}
####################################
# Nat Gateway
# Create one elastic IP to be used by the NAT gateway
# if var.vpcType == public && var.create_natgw == true
resource "aws_eip" "ngw_eip" {
count = "${var.vpcType == "public" && var.create_natgw ? 1 : 0}"
vpc = true
}
# Add Nat gateway to the first web_subnet
# if var.vpcType == public && var.create_natgw == true
# NOTE: this is not HA. but outbound internet access for private subents isnt business critical
resource "aws_nat_gateway" "ngw" {
count = "${var.vpcType == "public" && var.create_natgw ? 1 : 0}"
allocation_id = "${aws_eip.ngw_eip.id}"
subnet_id = "${element(aws_subnet.web_subnet.*.id, 0)}"
depends_on = ["aws_internet_gateway.igw"]
tags = "${merge( local.default_tags, map (
"Name", "nat-${var.deptName}-${var.appName}-${var.envName}-public-${var.region}",
"Ci", ""
))}"
}
# Add a rule to vpc default route table, making the Nat GW the default route
# if var.vpcType == public and var.create_default_vpc_route == true && var.create_natgw == true
resource "aws_route" "ngw_route" {
count = "${var.vpcType == "public" && var.create_vpc_default_route && var.create_natgw ? 1 : 0}"
route_table_id = "${aws_vpc.vpc.main_route_table_id}"
nat_gateway_id = "${aws_nat_gateway.ngw.id}"
destination_cidr_block = "0.0.0.0/0"
}
####################################
# Transit Gateway Attachment
# Create a Transit Gateway Attachment for the VPC to the app subnets (one subnet per az)
# if var.vpcType != public && var.create_app_subnets == false
resource "aws_ec2_transit_gateway_vpc_attachment" "tgw_app_attach" {
count = "${var.attach_to_tgw != false && var.vpcType != "public" && var.create_app_subnets ? 1 : 0}"
vpc_id = "${aws_vpc.vpc.id}"
subnet_ids = [
"${aws_subnet.app_subnet.*.id}",
]
transit_gateway_id = "${data.terraform_remote_state.aws_dotc-infra-prd_net-transitgw-prd.tgw_ids[var.region]}"
tags = "${merge( local.default_tags, map (
"Name", "tgwattch-${aws_vpc.vpc.id}-${var.region}",
"Ci", ""
))}"
}
# Add a rule to vpc default route table, making the Transit GW the default route
# if var.vpcType != public && var.create_app_subnets == false
resource "aws_route" "tgw_route" {
count = "${var.attach_to_tgw != false && var.vpcType != "public" && var.create_vpc_default_route ? 1 : 0}"
route_table_id = "${aws_vpc.vpc.main_route_table_id}"
transit_gateway_id = "${aws_ec2_transit_gateway_vpc_attachment.tgw_app_attach.transit_gateway_id}"
destination_cidr_block = "0.0.0.0/0"
}
# Create a Transit Gateway Attachment for the VPC to the db subnets (one subnet per az)
# if var.vpcType != public && var.create_app_subnets == false && var.create_db_subnets == true
resource "aws_ec2_transit_gateway_vpc_attachment" "tgw_db_attach" {
count = "${var.attach_to_tgw != false && var.vpcType != "public" && ! var.create_app_subnets && var.create_db_subnets ? 1 : 0}"
vpc_id = "${aws_vpc.vpc.id}"
subnet_ids = [
"${aws_subnet.database_subnet.*.id}",
]
transit_gateway_id = "${data.terraform_remote_state.aws_dotc-infra-prd_net-transitgw-prd.tgw_ids[var.region]}"
tags = "${merge( local.default_tags, map (
"Name", "tgwattch-${aws_vpc.vpc.id}-${var.region}",
"Ci", ""
))}"
}
# Create a Transit Gateway Attachment for the VPC to the web subnets (one subnet per az)
# if var.vpcType != public && var.create_app_subnets == false && var.create_db_subnets == false
resource "aws_ec2_transit_gateway_vpc_attachment" "tgw_web_attach" {
count = "${var.attach_to_tgw != false && var.vpcType != "public" && ! var.create_app_subnets && ! var.create_db_subnets ? 1 : 0}"
vpc_id = "${aws_vpc.vpc.id}"
subnet_ids = [
"${aws_subnet.web_subnet.*.id}",
]
transit_gateway_id = "${data.terraform_remote_state.aws_dotc-infra-prd_net-transitgw-prd.tgw_ids[var.region]}"
tags = "${merge( local.default_tags, map (
"Name", "tgwattch-${aws_vpc.vpc.id}-${var.region}",
"Ci", ""
))}"
}
##########################################
# Web Subnets
# Create var.azQty public subnet(s)
# if var.create_web_subnets == true
resource "aws_subnet" "web_subnet" {
count = "${var.create_web_subnets ? var.azQty : 0}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${cidrsubnet(var.vpcCidr, var.subnetBits, count.index)}"
availability_zone = "${var.region}${element(local.azSuffixes, count.index)}"
map_public_ip_on_launch = true
tags = "${merge( local.default_tags, map (
"Name", "sn-web-${var.deptName}-${var.appName}-${var.envName}-${var.region}${element(local.azSuffixes, count.index)}",
"Ci", ""
))}"
}
# Associate all web Subnets to the public Route Table
# if var.vpcType != private and var.create_web_subnets == true
resource "aws_route_table_association" "publicRteTblAssoc" {
count = "${var.vpcType != "private" && var.create_web_subnets ? var.azQty : 0}"
subnet_id = "${element(aws_subnet.web_subnet.*.id, count.index)}"
route_table_id = "${aws_route_table.public_routetable.id}"
}
###############################################################
# App Subnets
# Create var.azQty private app subnet(s) if var.create_app_subnets == true
resource "aws_subnet" "app_subnet" {
count = "${var.create_app_subnets ? var.azQty : 0}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${cidrsubnet(var.vpcCidr, var.subnetBits, count.index + length(aws_subnet.web_subnet.*.id))}"
availability_zone = "${var.region}${element(local.azSuffixes, count.index)}"
map_public_ip_on_launch = false
tags = "${merge( local.default_tags, map (
"Name", "sn-app-${var.deptName}-${var.appName}-${var.envName}-${var.region}${element(local.azSuffixes, count.index)}",
"Ci", ""
))}"
}
###############################################################
# Database Subnets
# Create var.azQty database private subnet if var.create_db_subnets == true
resource "aws_subnet" "database_subnet" {
count = "${var.create_db_subnets ? var.azQty : 0}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${cidrsubnet(var.vpcCidr, var.subnetBits, count.index + length(aws_subnet.web_subnet.*.id) + length(aws_subnet.app_subnet.*.id))}"
availability_zone = "${var.region}${element(local.azSuffixes, count.index)}"
map_public_ip_on_launch = false
tags = "${merge( local.default_tags, map (
"Name", "sn-db-${var.deptName}-${var.appName}-${var.envName}-${var.region}${element(local.azSuffixes, count.index)}",
"Ci", ""
))}"
}