-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubnets.tf
121 lines (105 loc) · 3.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
#--------------------------------------------------------------
# This module creates all resources necessary for subnets
#--------------------------------------------------------------
locals {
number_subnets = length(split(",", var.subnet_cidrs))
}
resource "aws_subnet" "default" {
vpc_id = var.vpc_id
cidr_block = element(split(",", var.subnet_cidrs), count.index)
availability_zone = element(split(",", var.azs), count.index)
count = length(split(",", var.subnet_cidrs))
map_public_ip_on_launch = var.map_public_ip_on_launch
tags = merge(
{
"Name" = format("${var.subnet_name}-%d", count.index)
"az" = element(split(",", var.azs), count.index)
},
var.subnet_tags,
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_route_table" "default" {
count = local.number_subnets
vpc_id = var.vpc_id
tags = merge(
{
"Name" = var.subnet_name
"az" = element(split(",", var.azs), count.index)
},
var.subnet_tags,
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_route" "nat_gateway" {
count = var.nat_gateway_ids != [] ? local.number_subnets : 0
route_table_id = element(aws_route_table.default.*.id, count.index)
nat_gateway_id = element(var.nat_gateway_ids, count.index)
destination_cidr_block = "0.0.0.0/0"
depends_on = [aws_route_table.default]
}
resource "aws_route" "nat_instance" {
count = var.instance_ids != [] ? local.number_subnets : 0
route_table_id = element(aws_route_table.default.*.id, count.index)
instance_id = element(var.instance_ids, count.index)
destination_cidr_block = "0.0.0.0/0"
depends_on = [aws_route_table.default]
}
resource "aws_route" "gateway_id" {
count = var.gateway_ids != [] ? local.number_subnets : 0
route_table_id = element(aws_route_table.default.*.id, count.index)
gateway_id = element(var.gateway_ids, count.index)
destination_cidr_block = "0.0.0.0/0"
depends_on = [aws_route_table.default]
}
resource "aws_route_table_association" "default" {
count = length(split(",", var.subnet_cidrs))
subnet_id = element(aws_subnet.default.*.id, count.index)
route_table_id = element(aws_route_table.default.*.id, count.index)
lifecycle {
create_before_destroy = true
}
}
resource "aws_network_acl" "public" {
vpc_id = var.vpc_id
subnet_ids = aws_subnet.default.*.id
dynamic "egress" {
for_each = var.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.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 = merge(
{
"Name" = "${var.subnet_name}-ACLs"
},
var.subnet_tags,
)
depends_on = [aws_subnet.default]
}