Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
ffd311d
Add WAF protection to application load balancers
devin-ai-integration[bot] Apr 17, 2025
7a96e57
Add WAF logging configuration and Log4j2 protection
devin-ai-integration[bot] Apr 17, 2025
8ad8e5c
Add KMS encryption to WAF CloudWatch logs
devin-ai-integration[bot] Apr 17, 2025
b920383
Fix reference to network module in service root module
devin-ai-integration[bot] Apr 17, 2025
cf4faff
Remove KMS key and add checkov skip comment for CKV_AWS_158
devin-ai-integration[bot] Apr 17, 2025
fe3b98b
Fix checkov skip comment placement for CKV_AWS_158
devin-ai-integration[bot] Apr 17, 2025
ba4c5ce
Move checkov skip comment inside resource block
devin-ai-integration[bot] Apr 17, 2025
3be8581
Fix network module reference in service root module
devin-ai-integration[bot] Apr 17, 2025
3fb68f5
Add network module import to service root module
devin-ai-integration[bot] Apr 17, 2025
1417345
Address PR feedback: replace waf_arn with enable_waf and reorganize code
devin-ai-integration[bot] Apr 17, 2025
338f06d
Add enable_waf variable to network data module
devin-ai-integration[bot] Apr 17, 2025
f01d62d
Update WAF data source to use enable_waf variable
devin-ai-integration[bot] Apr 17, 2025
0e20691
Add enable_waf variable to network resources module
devin-ai-integration[bot] Apr 17, 2025
831407d
Make WAF resources conditional based on enable_waf variable
devin-ai-integration[bot] Apr 17, 2025
c8ef7be
Revert conditional WAF creation and remove enable_waf variables from …
devin-ai-integration[bot] Apr 17, 2025
fe65d9a
Tweaks
lorenyu Apr 17, 2025
90afea0
Remove unused variable
lorenyu Apr 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions infra/app/app-config/env-config/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ variable "enable_notifications" {
default = false
}

variable "enable_waf" {
type = bool
description = "Enables Web Application Firewall (WAF) protection for the application load balancer"
default = true
}

variable "environment" {
description = "name of the application environment (e.g. dev, staging, prod)"
type = string
Expand Down
5 changes: 5 additions & 0 deletions infra/app/app-config/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ locals {
# 2. Configures email notifications using AWS SES
enable_notifications = true

# Whether or not the application should enable WAF for the load balancer.
# If enabled:
# 1. Creates an AWS WAF web ACL with AWSManagedRulesCommonRuleSet
enable_waf = true

environment_configs = {
dev = module.dev_config
staging = module.staging_config
Expand Down
4 changes: 4 additions & 0 deletions infra/app/app-config/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ output "enable_notifications" {
value = local.enable_notifications
}

output "enable_waf" {
value = local.enable_waf
}

output "shared_network_name" {
value = local.shared_network_name
}
1 change: 1 addition & 0 deletions infra/app/service/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module "service" {
domain_name = module.domain.domain_name
hosted_zone_id = module.domain.hosted_zone_id
certificate_arn = module.domain.certificate_arn
waf_arn = module.app_config.enable_waf ? module.service.network.waf_arn : null

cpu = local.service_config.cpu
memory = local.service_config.memory
Expand Down
9 changes: 9 additions & 0 deletions infra/modules/network/data/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ data "aws_security_groups" "aws_services" {
values = [data.aws_vpc.network.id]
}
}

data "aws_wafv2_web_acl" "network" {
name = module.interface.waf_acl_name
scope = "REGIONAL"
}

output "waf_arn" {
value = data.aws_wafv2_web_acl.network.arn
}
Comment thread
lorenyu marked this conversation as resolved.
Outdated
4 changes: 4 additions & 0 deletions infra/modules/network/interface/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ output "private_subnet_tags" {
output "public_subnet_tags" {
value = { subnet_type = "public" }
}

output "waf_acl_name" {
value = var.name
}
89 changes: 89 additions & 0 deletions infra/modules/network/resources/waf.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
resource "aws_wafv2_web_acl" "main" {
name = module.interface.waf_acl_name
description = "WAF to protect application load balancers in the ${var.name} network"
scope = "REGIONAL"

default_action {
allow {}
}

rule {
name = "AWS-AWSManagedRulesCommonRuleSet"
priority = 1

override_action {
none {}
}

statement {
managed_rule_group_statement {
name = "AWSManagedRulesCommonRuleSet"
vendor_name = "AWS"
}
}

visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "AWS-AWSManagedRulesCommonRuleSet"
sampled_requests_enabled = true
}
}

rule {
name = "AWS-AWSManagedRulesKnownBadInputsRuleSet"
priority = 2

override_action {
none {}
}

statement {
managed_rule_group_statement {
name = "AWSManagedRulesKnownBadInputsRuleSet"
vendor_name = "AWS"
}
}

visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "AWS-AWSManagedRulesKnownBadInputsRuleSet"
sampled_requests_enabled = true
}
}

visibility_config {
cloudwatch_metrics_enabled = true
metric_name = "${var.name}-waf"
sampled_requests_enabled = true
}

tags = {
Name = var.name
}
}

resource "aws_cloudwatch_log_group" "waf_logs" {
name = "aws-waf-logs-${var.name}"
retention_in_days = 30
kms_key_id = aws_kms_key.waf_logs.arn
}

resource "aws_kms_key" "waf_logs" {
description = "KMS key for WAF logs encryption"
deletion_window_in_days = 7
enable_key_rotation = true
}

resource "aws_kms_alias" "waf_logs" {
name = "alias/${var.name}-waf-logs"
target_key_id = aws_kms_key.waf_logs.key_id
}

resource "aws_wafv2_web_acl_logging_configuration" "main" {
log_destination_configs = [aws_cloudwatch_log_group.waf_logs.arn]
resource_arn = aws_wafv2_web_acl.main.arn
}

output "waf_arn" {
value = aws_wafv2_web_acl.main.arn
}
Comment thread
lorenyu marked this conversation as resolved.
Outdated
6 changes: 6 additions & 0 deletions infra/modules/service/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,9 @@ variable "ephemeral_write_volumes" {
description = "A set of absolute paths in the container to be mounted as writable for the life of the task. These need to be declared with `VOLUME` instructions in the container build file."
default = []
}

variable "waf_arn" {
type = string
description = "The ARN of the WAF ACL to associate with the load balancer"
default = null
}
5 changes: 5 additions & 0 deletions infra/modules/service/waf.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "aws_wafv2_web_acl_association" "main" {
count = var.waf_arn != null ? 1 : 0
resource_arn = aws_lb.alb.arn
web_acl_arn = var.waf_arn
}
Loading