-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathvpc.tf
153 lines (125 loc) · 4 KB
/
vpc.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Main VPC resource
resource "aws_vpc" "consul" {
cidr_block = var.vpc_cidr
instance_tenancy = var.vpc_instance_tenancy
enable_dns_support = true
enable_dns_hostnames = true
assign_generated_ipv6_cidr_block = true
tags = merge(
{ "Name" = "${var.main_project_tag}-vpc" },
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
## Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.consul.id
tags = merge(
{ "Name" = "${var.main_project_tag}-igw" },
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
## Egress Only Gateway (IPv6)
resource "aws_egress_only_internet_gateway" "eigw" {
vpc_id = aws_vpc.consul.id
}
## The NAT Elastic IP
resource "aws_eip" "nat" {
vpc = true
tags = merge(
{ "Name" = "${var.main_project_tag}-nat-eip" },
{ "Project" = var.main_project_tag },
var.vpc_tags
)
depends_on = [aws_internet_gateway.igw]
}
## The NAT Gateway
resource "aws_nat_gateway" "nat" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public.0.id
tags = merge(
{ "Name" = "${var.main_project_tag}-nat" },
{ "Project" = var.main_project_tag },
var.vpc_tags
)
depends_on = [
aws_internet_gateway.igw,
aws_eip.nat
]
}
## Public Route Table
resource "aws_route_table" "public" {
vpc_id = aws_vpc.consul.id
tags = merge(
{ "Name" = "${var.main_project_tag}-public-rtb" },
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
## Public routes
resource "aws_route" "public_internet_access" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
## Private Route Table
resource "aws_route_table" "private" {
vpc_id = aws_vpc.consul.id
tags = merge(
{ "Name" = "${var.main_project_tag}-private-rtb" },
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
## Private Routes
resource "aws_route" "private_internet_access" {
route_table_id = aws_route_table.private.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat.id
}
resource "aws_route" "private_internet_access_ipv6" {
route_table_id = aws_route_table.private.id
destination_ipv6_cidr_block = "::/0"
egress_only_gateway_id = aws_egress_only_internet_gateway.eigw.id
}
## Public Subnets
resource "aws_subnet" "public" {
count = var.vpc_public_subnet_count
vpc_id = aws_vpc.consul.id
cidr_block = local.public_cidr_blocks[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
ipv6_cidr_block = cidrsubnet(aws_vpc.consul.ipv6_cidr_block, 8, count.index)
assign_ipv6_address_on_creation = true
tags = merge(
{ "Name" = "${var.main_project_tag}-public-${data.aws_availability_zones.available.names[count.index]}" },
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
## Private Subnets
resource "aws_subnet" "private" {
count = var.vpc_private_subnet_count
vpc_id = aws_vpc.consul.id
// Increment the netnum by the number of public subnets to avoid overlap
cidr_block = local.private_cidr_blocks[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index]
tags = merge(
{ "Name" = "${var.main_project_tag}-private-${data.aws_availability_zones.available.names[count.index]}" },
{ "Project" = var.main_project_tag },
var.vpc_tags
)
}
## Public Subnet Route Associations
resource "aws_route_table_association" "public" {
count = var.vpc_public_subnet_count
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = aws_route_table.public.id
}
## Private Subnet Route Associations
resource "aws_route_table_association" "private" {
count = var.vpc_private_subnet_count
subnet_id = element(aws_subnet.private.*.id, count.index)
route_table_id = aws_route_table.private.id
}