Skip to content

Commit 366ff89

Browse files
Arnel Jan SarmientoArJSarmiento
authored andcommitted
feat: Add VPC flow log configuration and outputs to AWS VPC module, including IAM role and policy for log management
1 parent d83a37b commit 366ff89

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

modules/aws/vpc/main.tf

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ resource "aws_subnet" "public" {
5454
vpc_id = aws_vpc.this.id
5555
cidr_block = var.public_subnet_cidrs[each.value]
5656
availability_zone = each.key
57-
map_public_ip_on_launch = true
57+
map_public_ip_on_launch = var.map_public_ip_on_launch
5858

5959
tags = {
6060
Name = "${var.name}-public-${each.key}"
@@ -149,3 +149,67 @@ resource "aws_route" "private_nat" {
149149
aws_nat_gateway.this[var.availability_zones[0]].id
150150
) : aws_nat_gateway.this[each.key].id
151151
}
152+
153+
# ------------------------------------------------------------------------------
154+
# VPC Flow Logs
155+
# ------------------------------------------------------------------------------
156+
157+
resource "aws_cloudwatch_log_group" "flow_log" {
158+
name = "/aws/vpc/${var.name}/flow-logs"
159+
retention_in_days = var.flow_log_retention_days
160+
161+
tags = {
162+
Name = "${var.name}-flow-logs"
163+
}
164+
}
165+
166+
resource "aws_iam_role" "flow_log" {
167+
name = "${var.name}-vpc-flow-log"
168+
169+
assume_role_policy = jsonencode({
170+
Version = "2012-10-17"
171+
Statement = [{
172+
Effect = "Allow"
173+
Principal = {
174+
Service = "vpc-flow-logs.amazonaws.com"
175+
}
176+
Action = "sts:AssumeRole"
177+
}]
178+
})
179+
180+
tags = {
181+
Name = "${var.name}-vpc-flow-log"
182+
}
183+
}
184+
185+
resource "aws_iam_role_policy" "flow_log" {
186+
name = "vpc-flow-log-publish"
187+
role = aws_iam_role.flow_log.id
188+
189+
policy = jsonencode({
190+
Version = "2012-10-17"
191+
Statement = [{
192+
Effect = "Allow"
193+
Action = [
194+
"logs:CreateLogGroup",
195+
"logs:CreateLogStream",
196+
"logs:PutLogEvents",
197+
"logs:DescribeLogGroups",
198+
"logs:DescribeLogStreams",
199+
]
200+
Resource = "${aws_cloudwatch_log_group.flow_log.arn}:*"
201+
}]
202+
})
203+
}
204+
205+
resource "aws_flow_log" "this" {
206+
vpc_id = aws_vpc.this.id
207+
traffic_type = var.flow_log_traffic_type
208+
log_destination = aws_cloudwatch_log_group.flow_log.arn
209+
log_destination_type = "cloud-watch-logs"
210+
iam_role_arn = aws_iam_role.flow_log.arn
211+
212+
tags = {
213+
Name = "${var.name}-flow-log"
214+
}
215+
}

modules/aws/vpc/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,13 @@ output "private_route_table_ids" {
4747
description = "List of private route table IDs."
4848
value = [for rt in aws_route_table.private : rt.id]
4949
}
50+
51+
output "flow_log_id" {
52+
description = "ID of the VPC flow log."
53+
value = aws_flow_log.this.id
54+
}
55+
56+
output "flow_log_group_arn" {
57+
description = "ARN of the CloudWatch log group for VPC flow logs."
58+
value = aws_cloudwatch_log_group.flow_log.arn
59+
}

modules/aws/vpc/variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ variable "private_subnet_cidrs" {
2323
type = list(string)
2424
}
2525

26+
variable "map_public_ip_on_launch" {
27+
description = "Auto-assign public IPv4 addresses to instances launched in public subnets."
28+
type = bool
29+
default = false
30+
}
31+
2632
variable "enable_nat_gateway" {
2733
description = "Create a NAT gateway for private subnet internet access."
2834
type = bool
@@ -34,3 +40,15 @@ variable "single_nat_gateway" {
3440
type = bool
3541
default = true
3642
}
43+
44+
variable "flow_log_traffic_type" {
45+
description = "Type of traffic to capture in VPC flow logs (ACCEPT, REJECT, or ALL)."
46+
type = string
47+
default = "ALL"
48+
}
49+
50+
variable "flow_log_retention_days" {
51+
description = "Number of days to retain VPC flow logs in CloudWatch."
52+
type = number
53+
default = 30
54+
}

0 commit comments

Comments
 (0)