-
Notifications
You must be signed in to change notification settings - Fork 0
/
public.tf
158 lines (143 loc) · 4.76 KB
/
public.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
locals {
public_count = var.enabled && var.type == "public" ? length(var.availability_zones) : 0
public_nat_gateways_count = var.enabled && var.type == "public" && var.nat_gateway_enabled ? length(var.availability_zones) : 0
}
module "public_label" {
source = "git::https://github.com/ceibo-it/terraform-null-label.git?ref=tags/0.0.1"
namespace = var.namespace
name = var.name
stage = var.stage
delimiter = var.delimiter
tags = var.tags
attributes = compact(concat(var.attributes, ["public"]))
enabled = var.enabled
}
resource "aws_subnet" "public" {
count = local.public_count
vpc_id = var.vpc_id
availability_zone = element(var.availability_zones, count.index)
cidr_block = var.cidr_blocks[count.index]
tags = merge(
module.public_label.tags,
{
"Name" = "${module.public_label.id}${var.delimiter}${element(var.availability_zones, count.index)}"
"AZ" = element(var.availability_zones, count.index)
"Type" = var.type
},
)
}
resource "aws_network_acl" "public" {
count = var.enabled && var.type == "public" && var.public_network_acl_id == "" ? 1 : 0
vpc_id = var.vpc_id
subnet_ids = aws_subnet.public.*.id
dynamic "egress" {
for_each = var.public_network_acl_egress
content {
action = lookup(egress.value, "action", null)
cidr_block = lookup(egress.value, "cidr_block", null)
from_port = lookup(egress.value, "from_port", null)
icmp_code = lookup(egress.value, "icmp_code", null)
icmp_type = lookup(egress.value, "icmp_type", null)
ipv6_cidr_block = lookup(egress.value, "ipv6_cidr_block", null)
protocol = lookup(egress.value, "protocol", null)
rule_no = lookup(egress.value, "rule_no", null)
to_port = lookup(egress.value, "to_port", null)
}
}
dynamic "ingress" {
for_each = var.public_network_acl_ingress
content {
action = lookup(ingress.value, "action", null)
cidr_block = lookup(ingress.value, "cidr_block", null)
from_port = lookup(ingress.value, "from_port", null)
icmp_code = lookup(ingress.value, "icmp_code", null)
icmp_type = lookup(ingress.value, "icmp_type", null)
ipv6_cidr_block = lookup(ingress.value, "ipv6_cidr_block", null)
protocol = lookup(ingress.value, "protocol", null)
rule_no = lookup(ingress.value, "rule_no", null)
to_port = lookup(ingress.value, "to_port", null)
}
}
tags = module.public_label.tags
depends_on = [aws_subnet.public]
}
resource "aws_route_table" "public" {
count = local.public_count
vpc_id = var.vpc_id
tags = merge(
module.public_label.tags,
{
"Name" = "${module.public_label.id}${var.delimiter}${element(var.availability_zones, count.index)}"
"AZ" = element(var.availability_zones, count.index)
"Type" = var.type
},
)
}
resource "aws_route" "public" {
count = var.internet_gateway_enabled ? local.public_count : 0
route_table_id = element(aws_route_table.public.*.id, count.index)
gateway_id = var.igw_id
destination_cidr_block = "0.0.0.0/0"
depends_on = [aws_route_table.public]
}
resource "aws_route_table_association" "public" {
count = local.public_count
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = element(aws_route_table.public.*.id, count.index)
depends_on = [
aws_subnet.public,
aws_route_table.public,
]
}
resource "aws_eip" "public" {
count = local.public_nat_gateways_count
vpc = true
lifecycle {
create_before_destroy = true
}
}
resource "aws_nat_gateway" "public" {
count = local.public_nat_gateways_count
allocation_id = element(aws_eip.public.*.id, count.index)
subnet_id = element(aws_subnet.public.*.id, count.index)
depends_on = [aws_subnet.public]
lifecycle {
create_before_destroy = true
}
tags = merge(
module.public_label.tags,
{
"Name" = "${module.public_label.id}${var.delimiter}${element(var.availability_zones, count.index)}"
"AZ" = element(var.availability_zones, count.index)
"Type" = var.type
},
)
}
# Dummy list of NAT Gateway IDs to use in the outputs for private subnets and when `nat_gateway_enabled=false` for public subnets
# Needed due to Terraform limitation of not allowing using conditionals with maps and lists
locals {
dummy_az_ngw_ids = slice(
[
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
],
0,
length(var.availability_zones),
)
}