-
Notifications
You must be signed in to change notification settings - Fork 1
/
routing.tf
77 lines (68 loc) · 1.81 KB
/
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
locals {
isNATGW = length(var.private_subnet_cidrs) == 0 ? false : true
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
tags = merge({
Name = "${var.name_prefix}inet-gw"
},
var.common_tags
)
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
tags = merge({
Name = "${var.name_prefix}default-inet-rtb"
},
var.common_tags
)
}
resource "aws_route" "default_public" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
resource "aws_route_table_association" "public" {
for_each = aws_subnet.public
subnet_id = each.value.id
route_table_id = aws_route_table.public.id
}
resource "aws_eip" "nat_gw" {
count = local.isNATGW ? 1 : 0
tags = merge({
Name = "${var.name_prefix}natgw-ip"
},
var.common_tags
)
}
resource "aws_nat_gateway" "gw" {
count = local.isNATGW ? 1 : 0
allocation_id = aws_eip.nat_gw[0].id
subnet_id = aws_subnet.public[keys(aws_subnet.public)[0]].id
depends_on = [aws_internet_gateway.gw]
tags = merge({
Name = "${var.name_prefix}natgw"
},
var.common_tags
)
}
resource "aws_route_table" "private" {
count = local.isNATGW ? 1 : 0
vpc_id = aws_vpc.main.id
tags = merge({
Name = "${var.name_prefix}default-nat-rtb"
},
var.common_tags
)
}
resource "aws_route" "default_private" {
count = local.isNATGW ? 1 : 0
route_table_id = aws_route_table.private[0].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.gw[0].id
}
resource "aws_route_table_association" "private" {
for_each = local.isNATGW ? aws_subnet.private : {}
subnet_id = each.value.id
route_table_id = aws_route_table.private[0].id
}