-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.tf
211 lines (185 loc) · 7.56 KB
/
main.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
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
# Resource block for creation of VPC
resource "aws_vpc" "vpc" {
cidr_block = var.cidr_block
enable_dns_hostnames = true
enable_dns_support = true
instance_tenancy = var.instance_tenancy
enable_network_address_usage_metrics = var.enable_network_address_usage_metrics
tags = merge(
{ "Name" = format("%s-vpc", var.name) },
var.tags,
var.vpc_tags
)
}
# Private route53 zone creation
resource "aws_route53_zone" "vpc_route53" {
name = var.route53_zone
vpc {
vpc_id = aws_vpc.vpc.id
}
tags = merge(
{ "Name" = format("%s-route53-zone", var.name) },
var.tags
)
}
# Resource block for internet gateway setup
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = merge(
{ "Name" = format("%s-igw", var.name) },
var.tags
)
}
# Resource block for public subnets route table
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.vpc.id
tags = merge(
{ "Name" = format("%s-public-rt", var.name) },
var.tags
)
}
# Default route for public route table
resource "aws_route" "default_public_route" {
route_table_id = aws_route_table.public_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
# Updating main route table to public route table
resource "aws_main_route_table_association" "default_public_route" {
route_table_id = aws_route_table.public_route_table.id
vpc_id = aws_vpc.vpc.id
}
# Additional Routes to public route table
resource "aws_route" "additional_public_route" {
for_each = var.additional_public_routes
route_table_id = aws_route_table.public_route_table.id
gateway_id = each.value.gateway_id
destination_cidr_block = each.value.destination_cidr_block
}
# Public Subnets creation
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.vpc.id
count = length(var.public_subnets) >= length(var.azs) ? length(var.public_subnets) : 0
availability_zone = length(regexall("^[a-z]{2}-", element(var.azs, count.index))) > 0 ? element(var.azs, count.index) : null
availability_zone_id = length(regexall("^[a-z]{2}-", element(var.azs, count.index))) == 0 ? element(var.azs, count.index) : null
cidr_block = element(concat(var.public_subnets, [""]), count.index)
map_public_ip_on_launch = true
tags = merge(
{ "Name" = format("${var.name}-public-%s", element(var.azs, count.index)) },
var.tags,
var.public_subnets_tags
)
}
# Route table association with public subnets
resource "aws_route_table_association" "public_subnets_association" {
count = length(var.public_subnets)
route_table_id = aws_route_table.public_route_table.id
subnet_id = element(aws_subnet.public_subnet[*].id, count.index)
}
# Private Subnets creation
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.vpc.id
count = length(var.private_subnets) >= length(var.azs) ? length(var.private_subnets) : 0
availability_zone = length(regexall("^[a-z]{2}-", element(var.azs, count.index))) > 0 ? element(var.azs, count.index) : null
availability_zone_id = length(regexall("^[a-z]{2}-", element(var.azs, count.index))) == 0 ? element(var.azs, count.index) : null
cidr_block = element(concat(var.private_subnets, [""]), count.index)
map_public_ip_on_launch = true
tags = merge(
{ "Name" = format("${var.name}-private-%s", element(var.azs, count.index)) },
var.tags,
var.private_subnets_tags
)
}
# Private route table creation
resource "aws_route_table" "private_route_table" {
count = length(var.azs)
vpc_id = aws_vpc.vpc.id
tags = merge(
{ "Name" = format("%s-private-rt-%s", var.name, element(var.azs, count.index)) },
var.tags
)
}
# Private route table association
resource "aws_route_table_association" "private_route_table_association" {
count = length(var.private_subnets)
subnet_id = element(aws_subnet.private_subnet[*].id, count.index)
route_table_id = element(aws_route_table.private_route_table[*].id, count.index)
}
# Nat gateway elastic ip
resource "aws_eip" "nat" {
count = length(var.azs)
domain = "vpc"
tags = merge(
{ "Name" = format("%s-eip-%s", var.name, element(var.azs, count.index)) },
var.tags
)
depends_on = [aws_internet_gateway.igw]
}
# Nat gateway creation and setup
resource "aws_nat_gateway" "nat_gateway" {
count = length(var.azs)
subnet_id = element(aws_subnet.public_subnet[*].id, count.index)
allocation_id = element(aws_eip.nat[*].id, count.index)
tags = merge(
{ "Name" = format("%s-nat-%s", var.name, element(var.azs, count.index)) },
var.tags
)
depends_on = [aws_internet_gateway.igw]
}
# Nat gateway association
resource "aws_route" "private_route_nat_association" {
count = length(var.azs)
route_table_id = element(aws_route_table.private_route_table[*].id, count.index)
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = element(aws_nat_gateway.nat_gateway[*].id, count.index)
}
# Database subnet creation
resource "aws_subnet" "database_subnet" {
vpc_id = aws_vpc.vpc.id
count = length(var.database_subnets) >= length(var.azs) ? length(var.database_subnets) : 0
availability_zone = length(regexall("^[a-z]{2}-", element(var.azs, count.index))) > 0 ? element(var.azs, count.index) : null
availability_zone_id = length(regexall("^[a-z]{2}-", element(var.azs, count.index))) == 0 ? element(var.azs, count.index) : null
cidr_block = element(concat(var.database_subnets, [""]), count.index)
map_public_ip_on_launch = true
tags = merge(
{ "Name" = format("${var.name}-db-%s", element(var.azs, count.index)) },
var.tags,
var.database_subnets_tags
)
}
# Database route table association
resource "aws_route_table_association" "database_route_table_association" {
count = length(var.database_subnets)
subnet_id = element(aws_subnet.database_subnet[*].id, count.index)
route_table_id = element(aws_route_table.private_route_table[*].id, count.index)
}
# Additional routes for private route table
locals {
additional_routes = {
for route in aws_route_table.private_route_table[*].id : route => var.additional_private_routes
}
}
# Additional routes to private route table
resource "aws_route" "additional_private_route" {
count = length(var.additional_private_routes) > 0 ? length(aws_route_table.private_route_table[*].id) : 0
route_table_id = element(aws_route_table.private_route_table[*].id, count.index)
gateway_id = local.additional_routes[element(aws_route_table.private_route_table[*].id, count.index)][0].gateway_id
destination_cidr_block = local.additional_routes[element(aws_route_table.private_route_table[*].id, count.index)][0].destination_cidr_block
}
# VPC Flow logs bucket creation
data "aws_caller_identity" "current_account" {}
resource "aws_s3_bucket" "flow_logs_bucket" {
count = var.flow_logs_enabled ? 1 : 0
bucket = format("%s-%s-flow-logs-bucket", var.name, data.aws_caller_identity.current_account.account_id)
}
resource "aws_flow_log" "vpc_flow_log" {
count = var.flow_logs_enabled ? 1 : 0
log_destination = aws_s3_bucket.flow_logs_bucket[0].arn
log_destination_type = "s3"
traffic_type = var.flow_logs_traffic_type
vpc_id = aws_vpc.vpc.id
destination_options {
file_format = var.flow_logs_file_format
per_hour_partition = true
}
}