-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
executable file
·187 lines (163 loc) · 6.7 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
terraform {
required_version = ">= 0.13.0"
}
module "labels" {
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=tags/0.8.0"
namespace = var.namespace
stage = var.stage
name = var.name
tags = var.tags
}
resource "aws_vpc" "this" {
cidr_block = var.cidr_block
assign_generated_ipv6_cidr_block = var.enable_ipv6
enable_dns_hostnames = var.enable_dns_hostnames
enable_dns_support = var.enable_dns_support
instance_tenancy = var.instance_tenancy
tags = module.labels.tags
}
resource "aws_subnet" "public" {
count = var.enable_public_subnet ? length(var.availability_zones) : 0
vpc_id = aws_vpc.this.id
cidr_block = cidrsubnet(aws_vpc.this.cidr_block, 8, 0 + count.index)
map_public_ip_on_launch = var.map_public_ipv4
ipv6_cidr_block = var.enable_ipv6 ? cidrsubnet(aws_vpc.this.ipv6_cidr_block, 8, 0 + count.index) : null
assign_ipv6_address_on_creation = var.enable_ipv6
availability_zone = var.availability_zones[count.index]
tags = merge(module.labels.tags, { Name = "${module.labels.name}-public-${count.index}" })
}
resource "aws_subnet" "private" {
count = var.enable_private_subnet ? length(var.availability_zones) : 0
vpc_id = aws_vpc.this.id
cidr_block = cidrsubnet(aws_vpc.this.cidr_block, 4, 2 + count.index)
ipv6_cidr_block = var.enable_ipv6 ? cidrsubnet(aws_vpc.this.ipv6_cidr_block, 8, 20 + count.index) : null
assign_ipv6_address_on_creation = var.enable_ipv6
availability_zone = var.availability_zones[count.index]
tags = merge(module.labels.tags, { Name = "${module.labels.name}-private-${count.index}" })
}
resource "aws_subnet" "isolated" {
count = var.enable_isolated_subnet ? length(var.availability_zones) : 0
vpc_id = aws_vpc.this.id
cidr_block = cidrsubnet(aws_vpc.this.cidr_block, 7, 4 + count.index)
ipv6_cidr_block = var.enable_ipv6 ? cidrsubnet(aws_vpc.this.ipv6_cidr_block, 8, 10 + count.index) : null
assign_ipv6_address_on_creation = var.enable_ipv6
availability_zone = var.availability_zones[count.index]
tags = merge(module.labels.tags, { Name = "${module.labels.name}-isolated-${count.index}" })
}
// NAT GATEWAY AND ROUTING
resource "aws_eip" "this" {
count = var.enable_private_subnet ? length(var.availability_zones) : 0
vpc = true
tags = module.labels.tags
}
resource "aws_internet_gateway" "this" {
count = var.enable_public_subnet ? 1 : 0
vpc_id = aws_vpc.this.id
tags = module.labels.tags
}
resource "aws_egress_only_internet_gateway" "this" {
count = var.enable_ipv6 && var.enable_private_subnet ? 1 : 0
vpc_id = aws_vpc.this.id
tags = module.labels.tags
}
resource "aws_nat_gateway" "this" {
count = var.enable_private_subnet ? length(var.availability_zones) : 0
subnet_id = aws_subnet.public.*.id[count.index]
allocation_id = aws_eip.this.*.id[count.index]
tags = module.labels.tags
}
resource "aws_route_table" "private" {
count = var.enable_private_subnet ? length(var.availability_zones) : 0
vpc_id = aws_vpc.this.id
tags = merge(module.labels.tags, { Name = "${module.labels.name}-private-${count.index}" })
}
resource "aws_route_table_association" "private" {
count = var.enable_private_subnet ? length(var.availability_zones) : 0
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private[count.index].id
}
resource "aws_route" "private_ipv4" {
count = var.enable_private_subnet ? length(var.availability_zones) : 0
route_table_id = aws_route_table.private[count.index].id
nat_gateway_id = aws_nat_gateway.this[0].id
destination_cidr_block = "0.0.0.0/0"
}
resource "aws_route" "private_ipv6" {
count = var.enable_ipv6 && var.enable_private_subnet ? length(var.availability_zones) : 0
route_table_id = aws_route_table.private[count.index].id
egress_only_gateway_id = aws_egress_only_internet_gateway.this[0].id
destination_ipv6_cidr_block = "::/0"
}
resource "aws_route_table" "public" {
count = var.enable_public_subnet ? 1 : 0
vpc_id = aws_vpc.this.id
tags = merge(module.labels.tags, { Name = "${module.labels.name}-public" })
}
resource "aws_route" "public_ipv4" {
count = var.enable_public_subnet ? 1 : 0
route_table_id = aws_route_table.public[count.index].id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this[0].id
}
resource "aws_route" "public_ipv6" {
count = var.enable_ipv6 && var.enable_public_subnet ? 1 : 0
route_table_id = aws_route_table.public[count.index].id
destination_ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.this[0].id
}
resource "aws_route_table_association" "public" {
count = var.enable_public_subnet ? length(var.availability_zones) : 0
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public[0].id
}
// ACL FOR isolated SUBNET
resource "aws_network_acl" "isolated" {
count = var.enable_isolated_subnet ? 1 : 0
vpc_id = aws_vpc.this.id
subnet_ids = aws_subnet.isolated.*.id
tags = module.labels.tags
dynamic "ingress" {
for_each = aws_subnet.private.*.cidr_block
content {
protocol = -1
rule_no = 100 + ingress.key
action = "allow"
cidr_block = ingress.value
from_port = 0
to_port = 0
}
}
dynamic "ingress" {
for_each = var.enable_ipv6 ? aws_subnet.private.*.ipv6_cidr_block : []
content {
protocol = -1
rule_no = 200 + ingress.key
action = "allow"
ipv6_cidr_block = ingress.value
from_port = 0
to_port = 0
}
}
dynamic "egress" {
for_each = aws_subnet.private.*.cidr_block
content {
protocol = -1
rule_no = 100 + egress.key
action = "allow"
cidr_block = egress.value
from_port = 0
to_port = 0
}
}
dynamic "egress" {
for_each = var.enable_ipv6 ? aws_subnet.private.*.ipv6_cidr_block : []
content {
protocol = -1
rule_no = 200 + egress.key
action = "allow"
ipv6_cidr_block = egress.value
from_port = 0
to_port = 0
}
}
}