Skip to content

Commit 16ad441

Browse files
committed
feat: Updated resources to be dynamic and added support for flow logs to be published in cloudwatch
1 parent 341f695 commit 16ad441

File tree

2 files changed

+185
-86
lines changed

2 files changed

+185
-86
lines changed

main.tf

Lines changed: 83 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ module "labels" {
2525
## A VPC is a virtual network that closely resembles a traditional network that you'd operate in your own data center.
2626
###--------------------------------------------------------------------------------------------
2727
resource "aws_vpc" "default" {
28-
count = var.vpc_enabled ? 1 : 0
29-
30-
cidr_block = var.cidr_block
31-
ipv4_ipam_pool_id = try(var.additional_cidr_block.ipv4_ipam_pool_id, null)
32-
ipv4_netmask_length = try(var.additional_cidr_block.ipv4_netmask_length, null)
33-
ipv6_cidr_block = try(var.additional_ipv6_cidr_block.ipv6_cidr_block, null)
34-
ipv6_ipam_pool_id = try(var.additional_ipv6_cidr_block.ipv6_ipam_pool_id, null)
35-
ipv6_netmask_length = try(var.additional_ipv6_cidr_block.ipv6_netmask_length, null)
28+
count = var.enable ? 1 : 0
29+
cidr_block = var.ipam_pool_enable ? null : var.cidr_block
30+
ipv4_ipam_pool_id = var.ipv4_ipam_pool_id
31+
ipv4_netmask_length = var.ipv4_netmask_length
32+
ipv6_cidr_block = var.ipv6_cidr_block
33+
ipv6_ipam_pool_id = var.ipv6_ipam_pool_id
34+
ipv6_netmask_length = var.ipv6_netmask_length
3635
instance_tenancy = var.instance_tenancy
3736
enable_dns_hostnames = var.dns_hostnames_enabled
3837
enable_dns_support = var.dns_support_enabled
@@ -67,7 +66,7 @@ resource "aws_vpc_ipv4_cidr_block_association" "default" {
6766
# An AWS Internet Gateway virtual router that enables communication between VPC and the internet
6867
####---------------------------------------------------------------------------------------
6968
resource "aws_internet_gateway" "default" {
70-
count = var.vpc_enabled ? 1 : 0
69+
count = var.enable ? 1 : 0
7170

7271
vpc_id = join("", aws_vpc.default.*.id)
7372
tags = merge(
@@ -84,7 +83,7 @@ resource "aws_internet_gateway" "default" {
8483
# An egress-only internet gateway provides outbound-only internet connectivity for resources within a VPC
8584
##---------------------------------------------------------------------------------------------------
8685
resource "aws_egress_only_internet_gateway" "default" {
87-
count = var.vpc_enabled && var.enabled_ipv6_egress_only_internet_gateway ? 1 : 0
86+
count = var.enable && var.enabled_ipv6_egress_only_internet_gateway ? 1 : 0
8887

8988
vpc_id = join("", aws_vpc.default.*.id)
9089
tags = module.labels.tags
@@ -96,7 +95,7 @@ resource "aws_egress_only_internet_gateway" "default" {
9695
# The default security group serves as a baseline security configuration within the VPC.
9796
####----------------------------------------------------------------------------------
9897
resource "aws_default_security_group" "default" {
99-
count = var.vpc_enabled && var.restrict_default_sg == true ? 1 : 0
98+
count = var.enable && var.restrict_default_sg == true ? 1 : 0
10099

101100
vpc_id = join("", aws_vpc.default.*.id)
102101
dynamic "ingress" {
@@ -142,16 +141,27 @@ resource "aws_default_security_group" "default" {
142141
# Provides a resource to create an ASSOCIATION between gateway and routing table.
143142
# #----------------------------------------------------------------------------------
144143
resource "aws_default_route_table" "default" {
145-
count = var.vpc_enabled && var.aws_default_route_table ? 1 : 0
144+
count = var.enable && var.aws_default_route_table ? 1 : 0
146145

147146
default_route_table_id = aws_vpc.default[0].default_route_table_id
148-
route {
149-
cidr_block = "0.0.0.0/0"
150-
gateway_id = aws_internet_gateway.default[0].id
151-
}
152-
route {
153-
ipv6_cidr_block = "::/0"
154-
egress_only_gateway_id = aws_egress_only_internet_gateway.default[0].id
147+
dynamic "route" {
148+
for_each = var.default_route_table_routes
149+
content {
150+
# One of the following destinations must be provided
151+
cidr_block = route.value.cidr_block
152+
ipv6_cidr_block = lookup(route.value, "ipv6_cidr_block", null)
153+
destination_prefix_list_id = lookup(route.value, "destination_prefix_list_id", null)
154+
155+
# One of the following targets must be provided
156+
egress_only_gateway_id = lookup(route.value, "egress_only_gateway_id", null)
157+
gateway_id = lookup(route.value, "gateway_id", null)
158+
instance_id = lookup(route.value, "instance_id", null)
159+
nat_gateway_id = lookup(route.value, "nat_gateway_id", null)
160+
network_interface_id = lookup(route.value, "network_interface_id", null)
161+
transit_gateway_id = lookup(route.value, "transit_gateway_id", null)
162+
vpc_endpoint_id = lookup(route.value, "vpc_endpoint_id", null)
163+
vpc_peering_connection_id = lookup(route.value, "vpc_peering_connection_id", null)
164+
}
155165
}
156166
tags = merge(
157167
module.labels.tags,
@@ -166,7 +176,7 @@ resource "aws_default_route_table" "default" {
166176
#Description : Provides a VPC DHCP Options resource.
167177
####--------------------------------------------------------------
168178
resource "aws_vpc_dhcp_options" "vpc_dhcp" {
169-
count = var.vpc_enabled && var.enable_dhcp_options ? 1 : 0
179+
count = var.enable && var.enable_dhcp_options ? 1 : 0
170180

171181
domain_name = var.dhcp_options_domain_name
172182
domain_name_servers = var.dhcp_options_domain_name_servers
@@ -181,7 +191,7 @@ resource "aws_vpc_dhcp_options" "vpc_dhcp" {
181191
)
182192
}
183193
resource "aws_vpc_dhcp_options_association" "this" {
184-
count = var.vpc_enabled && var.enable_dhcp_options ? 1 : 0
194+
count = var.enable && var.enable_dhcp_options ? 1 : 0
185195

186196
vpc_id = join("", aws_vpc.default.*.id)
187197
dhcp_options_id = join("", aws_vpc_dhcp_options.vpc_dhcp.*.id)
@@ -193,7 +203,7 @@ resource "aws_vpc_dhcp_options_association" "this" {
193203
# it create and control the cryptographic keys that are used to protect your data.
194204
####--------------------------------------------------------------
195205
resource "aws_kms_key" "kms" {
196-
count = var.enable_flow_log == true ? 1 : 0
206+
count = var.enable && var.enable_flow_log ? 1 : 0
197207

198208
deletion_window_in_days = 10
199209
}
@@ -204,13 +214,12 @@ resource "aws_kms_key" "kms" {
204214
# S3 bucket is a public cloud storage resource available in AWS.
205215
####------------------------------------------------------------------------------
206216
resource "aws_s3_bucket" "mybucket" {
207-
count = var.enable_flow_log == true ? 1 : 0
217+
count = var.enable && var.enable_flow_log && var.flow_log_destination_type == "s3" ? 1 : 0
208218
bucket = var.flow_logs_bucket_name
209-
#acl = "private"
210219
}
211220

212221
resource "aws_s3_bucket_ownership_controls" "example" {
213-
count = var.enable_flow_log == true ? 1 : 0
222+
count = var.enable && var.enable_flow_log && var.flow_log_destination_type == "s3" ? 1 : 0
214223

215224
bucket = join("", aws_s3_bucket.mybucket.*.id)
216225
rule {
@@ -219,16 +228,14 @@ resource "aws_s3_bucket_ownership_controls" "example" {
219228
}
220229

221230
resource "aws_s3_bucket_acl" "example" {
222-
count = var.enable_flow_log == true ? 1 : 0
223-
231+
count = var.enable && var.enable_flow_log && var.flow_log_destination_type == "s3" ? 1 : 0
224232
depends_on = [aws_s3_bucket_ownership_controls.example]
225-
226-
bucket = join("", aws_s3_bucket.mybucket.*.id)
227-
acl = "private"
233+
bucket = join("", aws_s3_bucket.mybucket.*.id)
234+
acl = "private"
228235
}
229236

230237
resource "aws_s3_bucket_public_access_block" "example" {
231-
count = var.enable_flow_log == true ? 1 : 0
238+
count = var.enable && var.enable_flow_log && var.flow_log_destination_type == "s3" ? 1 : 0
232239

233240
bucket = aws_s3_bucket.mybucket[0].id
234241
block_public_acls = true
@@ -242,7 +249,7 @@ resource "aws_s3_bucket_public_access_block" "example" {
242249
# Description : Provides a S3 bucket server-side encryption configuration resource.
243250
####-------------------------------------------------------------------------------
244251
resource "aws_s3_bucket_server_side_encryption_configuration" "example" {
245-
count = var.enable_flow_log == true ? 1 : 0
252+
count = var.enable && var.enable_flow_log && var.flow_log_destination_type == "s3" ? 1 : 0
246253

247254
bucket = aws_s3_bucket.mybucket[0].id
248255
rule {
@@ -259,13 +266,24 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "example" {
259266
# specific network interface, subnet, or VPC. Logs are sent to S3 Bucket.
260267
##---------------------------------------------------------------------------------------------
261268
resource "aws_flow_log" "vpc_flow_log" {
262-
count = var.vpc_enabled && var.enable_flow_log == true ? 1 : 0
269+
count = var.enable && var.enable_flow_log == true ? 1 : 0
270+
log_destination_type = var.flow_log_destination_type
271+
log_destination = var.flow_log_destination_arn
272+
log_format = var.flow_log_log_format
273+
iam_role_arn = var.flow_log_iam_role_arn
274+
traffic_type = var.flow_log_traffic_type
275+
vpc_id = join("", aws_vpc.default.*.id)
276+
max_aggregation_interval = var.flow_log_max_aggregation_interval
277+
dynamic "destination_options" {
278+
for_each = var.flow_log_destination_type == "s3" ? [true] : []
263279

264-
log_destination = join("", aws_s3_bucket.mybucket.*.arn)
265-
log_destination_type = "s3"
266-
traffic_type = var.traffic_type
267-
vpc_id = join("", aws_vpc.default.*.id)
268-
tags = module.labels.tags
280+
content {
281+
file_format = var.flow_log_file_format
282+
hive_compatible_partitions = var.flow_log_hive_compatible_partitions
283+
per_hour_partition = var.flow_log_per_hour_partition
284+
}
285+
}
286+
tags = module.labels.tags
269287
}
270288

271289
##----------------------------------------------------------------------------------------------------
@@ -274,23 +292,35 @@ resource "aws_flow_log" "vpc_flow_log" {
274292
## similar to your security groups in order to add an additional layer of security to your VPC.
275293
##-------------------------------------------------------------------------------------------------------
276294
resource "aws_default_network_acl" "default" {
277-
count = var.vpc_enabled && var.aws_default_network_acl ? 1 : 0
295+
count = var.enable && var.aws_default_network_acl ? 1 : 0
278296
default_network_acl_id = aws_vpc.default[0].default_network_acl_id
279-
ingress {
280-
protocol = -1
281-
rule_no = 100
282-
action = "allow"
283-
cidr_block = "0.0.0.0/0"
284-
from_port = 0
285-
to_port = 0
297+
dynamic "ingress" {
298+
for_each = var.default_network_acl_ingress
299+
content {
300+
action = ingress.value.action
301+
cidr_block = lookup(ingress.value, "cidr_block", null)
302+
from_port = ingress.value.from_port
303+
icmp_code = lookup(ingress.value, "icmp_code", null)
304+
icmp_type = lookup(ingress.value, "icmp_type", null)
305+
ipv6_cidr_block = lookup(ingress.value, "ipv6_cidr_block", null)
306+
protocol = ingress.value.protocol
307+
rule_no = ingress.value.rule_no
308+
to_port = ingress.value.to_port
309+
}
286310
}
287-
egress {
288-
protocol = -1
289-
rule_no = 100
290-
action = "allow"
291-
cidr_block = "0.0.0.0/0"
292-
from_port = 0
293-
to_port = 0
311+
dynamic "egress" {
312+
for_each = var.default_network_acl_egress
313+
content {
314+
action = egress.value.action
315+
cidr_block = lookup(egress.value, "cidr_block", null)
316+
from_port = egress.value.from_port
317+
icmp_code = lookup(egress.value, "icmp_code", null)
318+
icmp_type = lookup(egress.value, "icmp_type", null)
319+
ipv6_cidr_block = lookup(egress.value, "ipv6_cidr_block", null)
320+
protocol = egress.value.protocol
321+
rule_no = egress.value.rule_no
322+
to_port = egress.value.to_port
323+
}
294324
}
295325
tags = merge(
296326
module.labels.tags,

0 commit comments

Comments
 (0)