-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
213 lines (182 loc) · 5.79 KB
/
main.tf
File metadata and controls
213 lines (182 loc) · 5.79 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
data "aws_availability_zones" "az" {}
data "aws_ec2_transit_gateway" "transit" {
count = length(var.tg_routes) > 0 ? 1 : 0
}
locals {
total_subnets_count = var.subnets.private.count + var.subnets.public.count
subnets = [
for i in range(local.total_subnets_count) :
cidrsubnet(var.cidr_block, ceil(log(local.total_subnets_count, 2 )), i )
]
private_subnets = [for i in range(var.subnets.private.count) : local.subnets[i]]
public_subnets = [for i in range(var.subnets.private.count, local.total_subnets_count) : local.subnets[i]]
}
## VPC
resource "aws_vpc" "main" {
cidr_block = var.cidr_block
enable_dns_hostnames = true
enable_dns_support = true
tags = { "Name" : var.project_name }
}
## Public Subnets
resource "aws_subnet" "public" {
count = var.subnets.public.count
vpc_id = aws_vpc.main.id
map_public_ip_on_launch = true
availability_zone = data.aws_availability_zones.az.names[count.index]
cidr_block = length(var.subnets.public.cidr) > 0 ? var.subnets.public.cidr[count.index] : local.public_subnets[count.index]
tags = {
"Name" = "public-subnet-${count.index}"
"AZ" = data.aws_availability_zones.az.names[count.index]
"Type" = "Public"
}
}
## Private Subnets
resource "aws_subnet" "private" {
count = var.subnets.private.count
vpc_id = aws_vpc.main.id
map_public_ip_on_launch = false
availability_zone = data.aws_availability_zones.az.names[count.index]
cidr_block = length(var.subnets.public.cidr) > 0 ? var.subnets.private.cidr[count.index] : local.private_subnets[count.index]
tags = {
"Name" = "private-subnet-${count.index}"
"AZ" = data.aws_availability_zones.az.names[count.index]
"Type" = "Private"
}
}
## Internet Gateway
resource "aws_internet_gateway" "igw" {
count = var.subnets.public.count > 0 ? 1 : 0
vpc_id = aws_vpc.main.id
}
## Elastic IP for the NAT Gateway
resource "aws_eip" "nat" {
count = var.subnets.public.count
vpc = true
tags = { Name = "natgw-eip-${count.index}" }
}
## NAT Gateway
resource "aws_nat_gateway" "ng" {
count = var.subnets.public.count
allocation_id = aws_eip.nat[count.index].id
subnet_id = aws_subnet.public[count.index].id
depends_on = [aws_internet_gateway.igw[0]]
tags = { Name = "natgw-${count.index}" }
}
## Private Route Table
resource "aws_route_table" "private" {
count = var.subnets.private.count
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.ng[count.index % length(aws_nat_gateway.ng)].id
}
# VPN
dynamic "route" {
for_each = var.tg_routes
content {
cidr_block = route.value
transit_gateway_id = data.aws_ec2_transit_gateway.transit[0].id
}
}
tags = { Name = "private-rt-${count.index}" }
}
## Public Route Table
resource "aws_route_table" "public" {
count = var.subnets.public.count > 0 ? 1 : 0
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw[count.index].id
}
tags = { Name = "public-rt" }
}
resource "aws_route_table_association" "public" {
count = var.subnets.public.count
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public[0].id
}
resource "aws_route_table_association" "private" {
count = var.subnets.private.count
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private[count.index].id
}
## Transit Gateway VPC Attachment
resource "aws_ec2_transit_gateway_vpc_attachment" "tg_vpc_attachment" {
count = length(var.tg_routes) > 0 ? 1 : 0
subnet_ids = aws_subnet.private.*.id
transit_gateway_id = data.aws_ec2_transit_gateway.transit[0].id
vpc_id = aws_vpc.main.id
}
resource "aws_flow_log" "vpc_flow_log" {
count = var.vpc_flow_log_enabled ? 1 : 0
iam_role_arn = aws_iam_role.flow_log_role[0].arn
log_destination = aws_cloudwatch_log_group.vpc_flow_log_group[0].arn
traffic_type = "ALL"
vpc_id = aws_vpc.main.id
}
resource "aws_cloudwatch_log_group" "vpc_flow_log_group" {
count = var.vpc_flow_log_enabled ? 1 : 0
name = "vpc_flow_log"
retention_in_days = 180
}
resource "aws_iam_role" "flow_log_role" {
count = var.vpc_flow_log_enabled ? 1 : 0
name = "${var.project_name}_vpc_flow_log_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "vpc-flow-logs.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy" "flow_log_policy" {
count = var.vpc_flow_log_enabled ? 1 : 0
name = "${var.project_name}_flow_log_policy"
role = aws_iam_role.flow_log_role[0].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
module "vpce" {
source = "./vpce"
count = length(var.vpc_endpoints) > 0 ? 1 : 0
vpc = {
id : aws_vpc.main.id,
cidr : aws_vpc.main.cidr_block,
private_subnets_ids : aws_subnet.private.*.id,
private_route_table_ids : aws_route_table.private.*.id
}
vpc_endpoints = var.vpc_endpoints
}
module "bastion" {
source = "./bastion"
count = var.bastion.enabled ? 1 : 0
acm_key_name = var.bastion.certificate_name
acm_key_file = var.bastion.certificate_key
vpc = { id : aws_vpc.main.id, public_subnet_id : aws_subnet.public[0].id }
}