-
Notifications
You must be signed in to change notification settings - Fork 2
/
subnets.tf
172 lines (141 loc) · 4.95 KB
/
subnets.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
################
# Firewall subnet
################
resource "aws_subnet" "firewall" {
count = length(var.firewall_subnets) > 0 ? length(var.firewall_subnets) : 0
vpc_id = local.vpc_id
cidr_block = var.firewall_subnets[
count.index
]
availability_zone = element(var.azs, count.index)
tags = merge(tomap({
"Name" = format("%s-${lower(var.firewall_subnet_suffix)}-%s", var.name, element(var.azs, count.index))
}), var.tags)
}
################
# Public subnet
################
resource "aws_subnet" "public" {
#checkov:skip=CKV_AWS_130: "Ensure VPC subnets do not assign public IP by default" - This is a public subet.
count = length(var.public_subnets) > 0 && (!var.one_nat_gateway_per_az || length(var.public_subnets) >= length(var.azs)) ? length(var.public_subnets) : 0
vpc_id = local.vpc_id
cidr_block = var.public_subnets[
count.index
]
availability_zone = element(var.azs, count.index)
map_public_ip_on_launch = var.map_public_ip_on_launch
tags = merge(tomap({
"Name" = format("%s-${lower(var.public_subnet_suffix)}-%s", var.name, element(var.azs, count.index))
}), var.tags)
}
#################
# Private subnet
#################
resource "aws_subnet" "private" {
count = length(var.private_subnets) > 0 ? length(var.private_subnets) : 0
vpc_id = local.vpc_id
cidr_block = var.private_subnets[
count.index
]
availability_zone = element(var.azs, count.index)
tags = merge(tomap({
"Name" = format("%s-${lower(var.private_subnet_tags[count.index])}-%s", var.name, element(var.azs, count.index))
}), var.tags)
}
#################
# TGW subnet
#################
resource "aws_subnet" "tgw" {
count = length(var.tgw_subnets) > 0 ? length(var.tgw_subnets) : 0
vpc_id = local.vpc_id
cidr_block = var.tgw_subnets[
count.index
]
availability_zone = element(var.azs, count.index)
tags = merge(tomap({
"Name" = format("%s-${lower(var.tgw_subnet_tags[count.index])}-%s", var.name, element(var.azs, count.index))
}), var.tags)
}
##################
# Database subnet
##################
resource "aws_subnet" "database" {
count = length(var.database_subnets) > 0 ? length(var.database_subnets) : 0
vpc_id = local.vpc_id
cidr_block = var.database_subnets[
count.index
]
availability_zone = element(var.azs, count.index)
tags = merge(tomap({
"Name" = format("%s-${var.database_subnet_suffix}-%s", var.name, element(var.azs, count.index))
}), var.database_subnet_tags, var.tags)
}
resource "aws_db_subnet_group" "database" {
count = length(var.database_subnets) > 0 && var.create_database_subnet_group ? 1 : 0
name = "${lower(var.name)}-backend"
description = "Database subnet group for ${var.name}"
subnet_ids = aws_subnet.database[*].id
tags = merge(tomap({
"Name" = format("%s", var.name)
}), var.tags, var.database_subnet_group_tags)
}
##################
# Redshift subnet
##################
resource "aws_subnet" "redshift" {
count = length(var.redshift_subnets) > 0 ? length(var.redshift_subnets) : 0
vpc_id = local.vpc_id
cidr_block = var.redshift_subnets[
count.index
]
availability_zone = element(var.azs, count.index)
tags = merge(tomap({
"Name" = format("%s-${var.redshift_subnet_suffix}-%s", var.name, element(var.azs, count.index))
}), var.tags)
}
resource "aws_redshift_subnet_group" "redshift" {
count = length(var.redshift_subnets) > 0 ? 1 : 0
name = var.name
description = "Redshift subnet group for ${var.name}"
subnet_ids = [
aws_subnet.redshift[*].id
]
tags = merge(tomap({
"Name" = format("%s", var.name)
}), var.tags, var.redshift_subnet_group_tags)
}
#####################
# ElastiCache subnet
#####################
resource "aws_subnet" "elasticache" {
count = length(var.elasticache_subnets) > 0 ? length(var.elasticache_subnets) : 0
vpc_id = local.vpc_id
cidr_block = var.elasticache_subnets[
count.index
]
availability_zone = element(var.azs, count.index)
tags = merge(tomap({
"Name" = format("%s-${var.elasticache_subnet_suffix}-%s", var.name, element(var.azs, count.index))
}), var.tags)
}
resource "aws_elasticache_subnet_group" "elasticache" {
count = length(var.elasticache_subnets) > 0 ? 1 : 0
name = var.name
description = "ElastiCache subnet group for ${var.name}"
subnet_ids = aws_subnet.elasticache[*].id
}
#####################################################
# intra subnets - private subnet without NAT gateway
#####################################################
resource "aws_subnet" "intra" {
count = length(var.intra_subnets) > 0 ? length(var.intra_subnets) : 0
vpc_id = local.vpc_id
cidr_block = var.intra_subnets[
count.index
]
availability_zone = element(var.azs, count.index)
# tags = merge(tomap("Name", format("%s-intra-%s", var.name, element(var.azs, count.index))), var.tags, var.intra_subnet_tags)
tags = merge(tomap({
"Name" = format("%s-${var.intra_subnet_tags[count.index]}-%s", var.name, element(var.azs, count.index))
}), var.tags)
}