-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathvpc_hub.tf
133 lines (116 loc) · 4.28 KB
/
vpc_hub.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
resource "aws_vpc" "hub_vpc" {
cidr_block = var.hub_cidr_block
enable_dns_hostnames = true
enable_dns_support = true
tags = merge(var.tags, {
Name = "${local.prefix}-hub-vpc"
})
}
resource "aws_subnet" "hub_tgw_private_subnet" {
vpc_id = aws_vpc.hub_vpc.id
count = length(local.hub_tgw_private_subnets_cidr)
cidr_block = element(local.hub_tgw_private_subnets_cidr, count.index)
availability_zone = element(local.availability_zones, count.index)
map_public_ip_on_launch = false
tags = merge(var.tags, {
Name = "${local.prefix}-hub-tgw-private-${element(local.availability_zones, count.index)}"
})
}
resource "aws_subnet" "hub_nat_public_subnet" {
vpc_id = aws_vpc.hub_vpc.id
count = length(local.hub_nat_public_subnets_cidr)
cidr_block = element(local.hub_nat_public_subnets_cidr, count.index)
availability_zone = element(local.availability_zones, count.index)
map_public_ip_on_launch = true
tags = merge(var.tags, {
Name = "${local.prefix}-hub-nat-public-${element(local.availability_zones, count.index)}"
})
}
resource "aws_subnet" "hub_firewall_subnet" {
vpc_id = aws_vpc.hub_vpc.id
count = length(local.hub_firewall_subnets_cidr)
cidr_block = element(local.hub_firewall_subnets_cidr, count.index)
availability_zone = element(local.availability_zones, count.index)
map_public_ip_on_launch = false
tags = merge(var.tags, {
Name = "${local.prefix}-hub-firewall-public-${element(local.availability_zones, count.index)}"
})
}
resource "aws_internet_gateway" "hub_igw" {
vpc_id = aws_vpc.hub_vpc.id
tags = merge(var.tags, {
Name = "${local.prefix}-hub-igw"
})
}
resource "aws_eip" "hub_nat_eip" {
domain = "vpc"
depends_on = [aws_internet_gateway.hub_igw]
tags = merge(var.tags, {
Name = "${local.prefix}-nat-eip"
})
}
resource "aws_nat_gateway" "hub_nat" {
allocation_id = aws_eip.hub_nat_eip.id
subnet_id = element(aws_subnet.hub_nat_public_subnet.*.id, 0)
depends_on = [aws_internet_gateway.hub_igw]
tags = merge(var.tags, {
Name = "${local.prefix}-hub-nat"
})
}
resource "aws_route_table" "hub_tgw_private_rt" {
vpc_id = aws_vpc.hub_vpc.id
tags = merge(var.tags, {
Name = "${local.prefix}-hub-tgw-private-rt"
})
}
resource "aws_route_table" "hub_nat_public_rt" {
vpc_id = aws_vpc.hub_vpc.id
tags = merge(var.tags, {
Name = "${local.prefix}-hub-nat-rt"
})
}
resource "aws_route_table" "hub_firewall_rt" {
vpc_id = aws_vpc.hub_vpc.id
tags = merge(var.tags, {
Name = "${local.prefix}-hub-firewall-rt"
})
}
resource "aws_route_table" "hub_igw_rt" {
vpc_id = aws_vpc.hub_vpc.id
tags = merge(var.tags, {
Name = "${local.prefix}-hub-igw-rt"
})
}
resource "aws_route_table_association" "hub_tgw_rta" {
count = length(local.hub_tgw_private_subnets_cidr)
subnet_id = element(aws_subnet.hub_tgw_private_subnet.*.id, count.index)
route_table_id = aws_route_table.hub_tgw_private_rt.id
}
resource "aws_route_table_association" "hub_nat_rta" {
count = length(local.hub_nat_public_subnets_cidr)
subnet_id = element(aws_subnet.hub_nat_public_subnet.*.id, count.index)
route_table_id = aws_route_table.hub_nat_public_rt.id
}
resource "aws_route_table_association" "hub_firewall_rta" {
count = length(local.hub_firewall_subnets_cidr)
subnet_id = element(aws_subnet.hub_firewall_subnet.*.id, count.index)
route_table_id = aws_route_table.hub_firewall_rt.id
}
resource "aws_route_table_association" "hub_igw_rta" {
gateway_id = aws_internet_gateway.hub_igw.id
route_table_id = aws_route_table.hub_igw_rt.id
}
resource "aws_route" "db_private_nat_gtw" {
route_table_id = aws_route_table.hub_tgw_private_rt.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.hub_nat.id
}
resource "aws_route" "db_firewall_public_gtw" {
route_table_id = aws_route_table.hub_firewall_rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.hub_igw.id
}
resource "aws_main_route_table_association" "set-hub-default-rt-assoc" {
vpc_id = aws_vpc.hub_vpc.id
route_table_id = aws_route_table.hub_firewall_rt.id
}