-
Notifications
You must be signed in to change notification settings - Fork 5
/
public_routing.tf
85 lines (64 loc) · 2.51 KB
/
public_routing.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
# ---------------------------------------------------------------------------------------------------------------------
# CREATE PUBLIC ROUTING AND AN INTERNET GATEWAY IF A PUBLIC SUBNET IS DEFINED
# ---------------------------------------------------------------------------------------------------------------------
locals {
public_subnets = [for subnet in local.subnets : subnet if subnet.class == "public"]
public_groups = try(distinct(local.public_subnets.*.group), [])
public_subnets_by_group = {
for group in local.public_groups : group => [
for subnet in local.public_subnets : subnet if subnet.group == group
]
}
}
resource "aws_internet_gateway" "internet_gateway" {
count = var.module_enabled && length(local.public_subnets) > 0 ? 1 : 0
vpc_id = aws_vpc.vpc[0].id
tags = merge(
{
Name = var.vpc_name
# special mineiros.io tags that can be used in data sources
"mineiros-io/aws/vpc/vpc-name" = var.vpc_name
"mineiros-io/aws/vpc/igw-name" = var.vpc_name
},
var.module_tags,
var.internet_gateway_tags,
)
depends_on = [var.module_depends_on]
}
resource "aws_route_table" "public" {
for_each = var.module_enabled ? local.public_subnets_by_group : {}
vpc_id = aws_vpc.vpc[0].id
# propagating_vgws = try(local.subnets[each.key].propagating_vgws, null)
tags = merge(
{
Name = "${var.vpc_name}-public-${each.key}"
# special mineiros.io tags that can be used in data sources
"mineiros-io/aws/vpc/vpc-name" = var.vpc_name
"mineiros-io/aws/vpc/routetable-name" = "${var.vpc_name}-public-${each.key}"
"mineiros-io/aws/vpc/routetable-class" = "public"
},
var.module_tags,
var.route_table_tags,
var.public_route_table_tags,
)
depends_on = [var.module_depends_on]
}
resource "aws_route_table_association" "public" {
for_each = var.module_enabled ? {
for subnet in flatten(values(local.public_subnets_by_group)) : subnet.cidr_block => subnet
} : {}
subnet_id = aws_subnet.subnet[each.key].id
route_table_id = aws_route_table.public[each.value.group].id
depends_on = [var.module_depends_on]
}
resource "aws_route" "internet_gateway" {
for_each = aws_route_table.public
route_table_id = each.value.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.internet_gateway[0].id
# Workaround for https://github.com/terraform-providers/terraform-provider-aws/issues/338
timeouts {
create = "10m"
}
depends_on = [var.module_depends_on]
}