-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.tf
324 lines (290 loc) · 14.4 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
locals {
azs = length(var.availability_zones)
public_subnets_native = var.public_subnet_enabled ? length(var.public_subnet_cidrs) > 0 ? var.public_subnet_cidrs : [for netnum in range(0, local.azs) : cidrsubnet(var.vpc_cidr, 8, netnum)] : []
secondary_public_subnets = var.public_subnet_enabled && var.secondry_cidr_enabled ? [
for cidr_block in var.secondary_cidr_blocks : [
for netnum in range(0, local.azs) : cidrsubnet(cidr_block, 8, netnum)
]
] : []
public_subnets = concat(local.public_subnets_native, flatten(local.secondary_public_subnets))
intra_subnets_native = var.intra_subnet_enabled ? length(var.intra_subnet_cidrs) > 0 ? var.intra_subnet_cidrs : [for netnum in range(local.azs * 3, local.azs * 4) : cidrsubnet(var.vpc_cidr, 8, netnum)] : []
secondary_intra_subnets = var.intra_subnet_enabled && var.secondry_cidr_enabled ? [
for cidr_block in var.secondary_cidr_blocks : [
for netnum in range(local.azs * 3, local.azs * 4) : cidrsubnet(cidr_block, 8, netnum)
]
] : []
intra_subnets = concat(local.intra_subnets_native, flatten(local.secondary_intra_subnets))
private_subnets_native = var.private_subnet_enabled ? length(var.private_subnet_cidrs) > 0 ? var.private_subnet_cidrs : [for netnum in range(local.azs, local.azs * 2) : cidrsubnet(var.vpc_cidr, 4, netnum)] : []
secondary_private_subnets = var.private_subnet_enabled && var.secondry_cidr_enabled ? [
for cidr_block in var.secondary_cidr_blocks : [
for netnum in range(local.azs, local.azs * 2) : cidrsubnet(cidr_block, 4, netnum)
]
] : []
private_subnets = concat(local.private_subnets_native, flatten(local.secondary_private_subnets))
database_subnets_native = var.database_subnet_enabled ? length(var.database_subnet_cidrs) > 0 ? var.database_subnet_cidrs : [for netnum in range(local.azs * 2, local.azs * 3) : cidrsubnet(var.vpc_cidr, 8, netnum)] : []
secondary_database_subnets = var.database_subnet_enabled && var.secondry_cidr_enabled ? [
for cidr_block in var.secondary_cidr_blocks : [
for netnum in range(local.azs * 2, local.azs * 3) : cidrsubnet(cidr_block, 8, netnum)
]
] : []
database_subnets = concat(local.database_subnets_native, flatten(local.secondary_database_subnets))
single_nat_gateway = var.one_nat_gateway_per_az == true ? false : true
create_database_subnet_route_table = var.database_subnet_enabled
create_flow_log_cloudwatch_log_group = var.flow_log_enabled == true || var.flow_log_cloudwatch_log_group_skip_destroy == true ? true : false
is_supported_arch = data.aws_ec2_instance_type.arch.supported_architectures[0] == "arm64" ? false : true # for VPN Instance
nacl_allow_vpc_access_rule = [{
rule_no = 97
action = "allow"
from_port = 0
to_port = 0
protocol = "-1"
cidr_block = var.vpc_cidr
}
]
enable_ipv6 = var.ipv6_enabled
ipv6_only = var.ipv6_enabled && var.ipv6_only ? true : false
public_subnet_assign_ipv6_address_on_creation = var.public_subnet_assign_ipv6_address_on_creation == true && var.ipv6_enabled == true ? true : false
private_subnet_assign_ipv6_address_on_creation = var.private_subnet_assign_ipv6_address_on_creation == true && var.ipv6_enabled == true ? true : false
database_subnet_assign_ipv6_address_on_creation = var.database_subnet_assign_ipv6_address_on_creation == true && var.ipv6_enabled == true ? true : false
intra_subnet_assign_ipv6_address_on_creation = var.intra_subnet_assign_ipv6_address_on_creation == true && var.ipv6_enabled == true ? true : false
public_subnet_ipv6_prefixes = var.public_subnet_enabled ? [for i in range(local.azs) : i] : []
private_subnet_ipv6_prefixes = var.private_subnet_enabled ? [for i in range(local.azs) : i + length(data.aws_availability_zones.available.names)] : []
database_subnet_ipv6_prefixes = var.database_subnet_enabled ? [for i in range(local.azs) : i + 2 * length(data.aws_availability_zones.available.names)] : []
intra_subnet_ipv6_prefixes = var.intra_subnet_enabled ? [for i in range(local.azs) : i + 3 * length(data.aws_availability_zones.available.names)] : []
}
data "aws_availability_zones" "available" {}
data "aws_ec2_instance_type" "arch" {
instance_type = var.vpn_server_instance_type
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.2.0"
name = format("%s-%s-vpc", var.environment, var.name)
cidr = var.vpc_cidr # CIDR FOR VPC
azs = var.availability_zones
use_ipam_pool = var.ipam_enabled ? true : false
ipv4_ipam_pool_id = var.ipam_enabled && var.create_ipam_pool ? aws_vpc_ipam_pool.ipam_pool[0].id : null
ipv4_netmask_length = var.ipam_enabled ? var.ipv4_netmask_length : null
create_database_subnet_group = length(local.database_subnets) > 1 && var.enable_database_subnet_group ? true : false
intra_subnets = local.intra_subnets
public_subnets = local.public_subnets
private_subnets = local.private_subnets
database_subnets = local.database_subnets
enable_flow_log = var.flow_log_enabled
enable_nat_gateway = length(local.private_subnets) > 0 && !var.ipv6_only ? true : false
single_nat_gateway = local.single_nat_gateway
enable_vpn_gateway = false
enable_dns_hostnames = true
flow_log_traffic_type = "ALL"
secondary_cidr_blocks = var.secondry_cidr_enabled ? var.secondary_cidr_blocks : []
one_nat_gateway_per_az = var.one_nat_gateway_per_az
map_public_ip_on_launch = var.auto_assign_public_ip
flow_log_destination_type = "cloud-watch-logs"
manage_default_network_acl = true
default_network_acl_ingress = concat(local.nacl_allow_vpc_access_rule, var.default_network_acl_ingress)
manage_default_security_group = true
default_security_group_ingress = [] # Enforcing no rules being present in the default security group.
default_security_group_egress = []
create_database_nat_gateway_route = false
create_database_subnet_route_table = local.create_database_subnet_route_table
create_flow_log_cloudwatch_iam_role = var.flow_log_enabled
create_flow_log_cloudwatch_log_group = local.create_flow_log_cloudwatch_log_group
flow_log_max_aggregation_interval = var.flow_log_max_aggregation_interval
flow_log_cloudwatch_log_group_skip_destroy = var.flow_log_cloudwatch_log_group_skip_destroy
flow_log_cloudwatch_log_group_retention_in_days = var.flow_log_cloudwatch_log_group_retention_in_days
flow_log_cloudwatch_log_group_kms_key_id = var.flow_log_cloudwatch_log_group_kms_key_arn
enable_ipv6 = local.enable_ipv6
public_subnet_ipv6_native = local.ipv6_only
private_subnet_ipv6_native = local.ipv6_only
database_subnet_ipv6_native = local.ipv6_only
intra_subnet_ipv6_native = local.ipv6_only
#assign_ipv6_address_on_creation = local.assign_ipv6_address_on_creation
public_subnet_assign_ipv6_address_on_creation = local.public_subnet_assign_ipv6_address_on_creation
private_subnet_assign_ipv6_address_on_creation = local.private_subnet_assign_ipv6_address_on_creation
database_subnet_assign_ipv6_address_on_creation = local.database_subnet_assign_ipv6_address_on_creation
intra_subnet_assign_ipv6_address_on_creation = local.intra_subnet_assign_ipv6_address_on_creation
public_subnet_ipv6_prefixes = local.public_subnet_ipv6_prefixes
private_subnet_ipv6_prefixes = local.private_subnet_ipv6_prefixes
database_subnet_ipv6_prefixes = local.database_subnet_ipv6_prefixes
intra_subnet_ipv6_prefixes = local.intra_subnet_ipv6_prefixes
# TAGS TO BE ASSOCIATED WITH EACH RESOURCE
tags = tomap(
{
"Name" = format("%s-%s-vpc", var.environment, var.name)
"Environment" = var.environment
},
)
public_subnet_tags = tomap({
"Name" = "${var.environment}-${var.name}-public-subnet"
"Subnet-group" = "public"
"kubernetes.io/role/elb" = 1
})
public_route_table_tags = tomap({
"Name" = "${var.environment}-${var.name}-public-route-table"
})
private_subnet_tags = tomap({
"Name" = "${var.environment}-${var.name}-private-subnet"
"Subnet-group" = "private"
"kubernetes.io/role/internal-elb" = 1
})
private_subnet_tags_per_az = { for az in var.availability_zones : az => {
"Karpenter" = "${az}"
} }
private_route_table_tags = tomap({
"Name" = "${var.environment}-${var.name}-private-route-table"
})
database_subnet_tags = tomap({
"Name" = "${var.environment}-${var.name}-database-subnet"
"Subnet-group" = "database"
})
database_route_table_tags = tomap({
"Name" = "${var.environment}-${var.name}-database-route-table"
})
intra_subnet_tags = tomap({
"Name" = "${var.environment}-${var.name}-intra-subnet"
"Subnet-group" = "intra"
})
intra_route_table_tags = tomap({
"Name" = "${var.environment}-${var.name}-intra-route-table"
})
igw_tags = tomap({
"Name" = "${var.environment}-${var.name}-igw"
})
nat_gateway_tags = tomap({
"Name" = "${var.environment}-${var.name}-nat"
})
default_network_acl_name = format("%s-%s-nacl", var.environment, var.name)
default_network_acl_tags = {
"Name" = format("%s-%s-nacl", var.environment, var.name)
"Environment" = var.environment
}
}
module "vpn_server" {
count = var.vpn_server_enabled && local.is_supported_arch ? 1 : 0
depends_on = [module.vpc]
source = "./modules/vpn"
name = var.name
vpc_id = module.vpc.vpc_id
vpc_cidr = var.vpc_cidr
environment = var.environment
vpn_key_pair = var.vpn_key_pair_name
public_subnet = module.vpc.public_subnets[0]
vpn_server_instance_type = var.vpn_server_instance_type
}
resource "aws_vpc_ipam" "ipam" {
count = var.ipam_enabled && var.create_ipam_pool ? 1 : 0
operating_regions {
region_name = var.region
}
}
# IPv4
resource "aws_vpc_ipam_pool" "ipam_pool" {
count = var.ipam_enabled && var.create_ipam_pool ? 1 : 0
description = "IPv4 pool"
address_family = "ipv4"
ipam_scope_id = aws_vpc_ipam.ipam[0].private_default_scope_id
locale = var.region
allocation_default_netmask_length = 16
}
resource "aws_vpc_ipam_pool_cidr" "ipam_pool_cidr" {
count = var.ipam_enabled ? 1 : 0
ipam_pool_id = var.create_ipam_pool ? aws_vpc_ipam_pool.ipam_pool[0].id : var.ipam_pool_id
cidr = var.create_ipam_pool ? var.vpc_cidr : var.existing_ipam_managed_cidr
}
# private links for S3
data "aws_route_tables" "aws_private_routes" {
count = var.vpc_s3_endpoint_enabled ? 1 : 0
depends_on = [module.vpc]
vpc_id = module.vpc.vpc_id
tags = {
Name = "${var.environment}-${var.name}-private-route-table"
}
}
resource "aws_vpc_endpoint" "private-s3" {
count = var.vpc_s3_endpoint_enabled ? 1 : 0
depends_on = [data.aws_route_tables.aws_private_routes]
vpc_id = module.vpc.vpc_id
service_name = "com.amazonaws.${var.region}.s3"
route_table_ids = data.aws_route_tables.aws_private_routes[0].ids
vpc_endpoint_type = "Gateway"
policy = <<POLICY
{
"Statement": [
{
"Action": "*",
"Effect": "Allow",
"Resource": "*",
"Principal": "*"
}
]
}
POLICY
tags = {
Name = "${var.environment}-${var.name}-endpoint"
}
}
# allow 443 to access ecr repo
resource "aws_security_group" "vpc_endpoints" {
count = var.vpc_ecr_endpoint_enabled ? 1 : 0
name_prefix = "${var.environment}-vpc-endpoints"
description = "Associated to ECR/s3 VPC Endpoints"
vpc_id = module.vpc.vpc_id
ingress {
description = "Allow Nodes to pull images from ECR via VPC endpoints"
protocol = "tcp"
from_port = 443
to_port = 443
cidr_blocks = [var.vpc_cidr]
}
}
# private links for ECR.dkr
resource "aws_vpc_endpoint" "private-ecr-dkr" {
count = var.vpc_ecr_endpoint_enabled ? 1 : 0
depends_on = [data.aws_route_tables.aws_private_routes]
vpc_id = module.vpc.vpc_id
service_name = "com.amazonaws.${var.region}.ecr.dkr"
subnet_ids = module.vpc.private_subnets
security_group_ids = [aws_security_group.vpc_endpoints[0].id]
vpc_endpoint_type = "Interface"
private_dns_enabled = true
policy = <<POLICY
{
"Statement": [
{
"Action": "*",
"Effect": "Allow",
"Resource": "*",
"Principal": "*"
}
]
}
POLICY
tags = {
Name = "${var.environment}-${var.name}-ecr-dkr-endpoint"
}
}
# private links for ECR.api
resource "aws_vpc_endpoint" "private-ecr-api" {
count = var.vpc_ecr_endpoint_enabled ? 1 : 0
depends_on = [data.aws_route_tables.aws_private_routes]
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
service_name = "com.amazonaws.${var.region}.ecr.api"
vpc_endpoint_type = "Interface"
private_dns_enabled = true
policy = <<POLICY
{
"Statement": [
{
"Action": "ecr.api",
"Effect": "Allow",
"Resource": "*",
"Principal": "*"
}
]
}
POLICY
tags = {
Name = "${var.environment}-${var.name}-ecr-api-endpoint"
}
}