-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic_subnets.tf
51 lines (41 loc) · 1.25 KB
/
public_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
resource "aws_subnet" "public_subnets" {
# Loop through the map of public subnets
for_each = {
for k, v in var.subnets : k => v
if v.is_public
}
# Creating public subnets
vpc_id = aws_vpc.main.id
cidr_block = each.value.cidr_block
availability_zone = each.value.availability_zone
map_public_ip_on_launch = true
# Default tags for every resource
tags = var.default_tags
}
resource "aws_route_table" "public_route_table" {
# Looping over the subnets to create a route table for each
for_each = aws_subnet.public_subnets
# VPC id
vpc_id = aws_vpc.main.id
# Default route to the internet
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
# Tags
tags = var.default_tags
# Dependencies on another modules
depends_on = [
aws_vpc.main,
aws_internet_gateway.gw,
aws_subnet.public_subnets
]
}
resource "aws_route_table_association" "public_route_table_association" {
# Looping over the subnets to create a route table for each
for_each = aws_subnet.public_subnets
# Creating the association
subnet_id = each.value.id
route_table_id = aws_route_table.public_route_table[each.key].id
depends_on = [aws_route_table.public_route_table]
}