Skip to content

Commit 98998b5

Browse files
committed
[NDR-13] Add access logging to load balancer logs (#279)
1 parent d98b841 commit 98998b5

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

infrastructure/cloudfront.tf

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
module "cloudfront_firewall_waf_v2" {
2+
source = "./modules/firewall_waf_v2"
3+
cloudfront_acl = true
4+
5+
environment = var.environment
6+
owner = var.owner
7+
count = local.is_sandbox ? 0 : 1
8+
providers = {aws = aws.us_east_1}
9+
}
10+
111
module "cloudfront-distribution-lg" {
2-
source = "./modules/cloudfront/"
12+
source = "./modules/cloudfront"
313
bucket_domain_name = "${terraform.workspace}-${var.lloyd_george_bucket_name}.s3.eu-west-2.amazonaws.com"
414
bucket_id = module.ndr-lloyd-george-store.bucket_id
515
qualifed_arn = module.edge-presign-lambda.qualified_arn
616
depends_on = [module.edge-presign-lambda.qualified_arn, module.ndr-lloyd-george-store.bucket_id, module.ndr-lloyd-george-store.bucket_domain_name]
7-
}
17+
web_acl_id = try(module.cloudfront_firewall_waf_v2[0].arn, "")
18+
}

infrastructure/firewall.tf

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
module "firewall_waf_v2" {
22
source = "./modules/firewall_waf_v2"
3+
cloudfront_acl = false
34

45
environment = var.environment
56
owner = var.owner
6-
count = (terraform.workspace == "ndra" ||
7-
terraform.workspace == "ndrb" ||
8-
terraform.workspace == "ndrc" ||
9-
terraform.workspace == "ndrd") ? 0 : 1
7+
count = local.is_sandbox ? 0 : 1
108
}
119

1210
resource "aws_wafv2_web_acl_association" "web_acl_association" {
@@ -21,4 +19,5 @@ resource "aws_wafv2_web_acl_association" "web_acl_association" {
2119
module.ndr-ecs-fargate-app,
2220
module.firewall_waf_v2[0]
2321
]
24-
}
22+
}
23+

infrastructure/modules/cloudfront/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ resource "aws_cloudfront_distribution" "distribution" {
3636
locations = ["GB"]
3737
}
3838
}
39+
web_acl_id = var.web_acl_id
3940
}
4041

4142
resource "aws_cloudfront_origin_request_policy" "viewer_policy" {
@@ -90,4 +91,5 @@ resource "aws_cloudfront_cache_policy" "nocache" {
9091
query_string_behavior = "none"
9192
}
9293
}
93-
}
94+
}
95+

infrastructure/modules/cloudfront/variable.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ variable "bucket_id" {
1111
variable "qualifed_arn" {
1212
type = string
1313
description = "Lambda@Edge function association"
14-
}
14+
}
15+
16+
variable "web_acl_id" {
17+
type = string
18+
description = "Web ACL to associate this Cloudfront distribution with"
19+
}
20+

infrastructure/modules/firewall_waf_v2/main.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
resource "aws_wafv2_web_acl" "waf_v2_acl" {
2-
name = "${terraform.workspace}-fw-waf-v2"
2+
name = "${terraform.workspace}-${var.cloudfront_acl ? "cloudwatch" : ""}-fw-waf-v2"
33
description = "A WAF to secure the Repo application."
4-
scope = "REGIONAL"
4+
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
55

66
default_action {
77
allow {}
@@ -50,6 +50,9 @@ resource "aws_wafv2_web_acl" "waf_v2_acl" {
5050
for_each = rule.value["excluded_rules"]
5151
content {
5252
name = excluded_rule.value
53+
action_to_use {
54+
allow {}
55+
}
5356
}
5457
}
5558

@@ -97,4 +100,5 @@ resource "aws_wafv2_web_acl" "waf_v2_acl" {
97100
Environment = var.environment
98101
Workspace = terraform.workspace
99102
}
100-
}
103+
}
104+

infrastructure/modules/firewall_waf_v2/regex.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
resource "aws_wafv2_regex_pattern_set" "large_body_uri" {
22
name = "${terraform.workspace}-fw-waf-body-size"
33
description = "A set of regex to allow specific pages to bypass the large body check"
4-
scope = "REGIONAL"
4+
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
55

66
# Allow pages involving images
77
regular_expression {
@@ -24,7 +24,7 @@ resource "aws_wafv2_regex_pattern_set" "large_body_uri" {
2424
resource "aws_wafv2_regex_pattern_set" "xss_body_uri" {
2525
name = "${terraform.workspace}-fw-waf-body-xss"
2626
description = "A regex to allow specific pages to bypass XSS checks on body"
27-
scope = "REGIONAL"
27+
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
2828

2929
# Allow pages involving images
3030
regular_expression {
@@ -42,7 +42,7 @@ resource "aws_wafv2_regex_pattern_set" "xss_body_uri" {
4242
resource "aws_wafv2_regex_pattern_set" "exclude_cms_uri" {
4343
name = "${terraform.workspace}-fw-waf-cms-exclude"
4444
description = "A regex to allow CMS calls to bypass firewalls"
45-
scope = "REGIONAL"
45+
scope = var.cloudfront_acl ? "CLOUDFRONT" : "REGIONAL"
4646

4747
# Allow pages involving images
4848
regular_expression {

infrastructure/modules/firewall_waf_v2/variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ variable "environment" {
44

55
variable "owner" {
66
type = string
7-
}
7+
}
8+
9+
variable "cloudfront_acl" {
10+
type = bool
11+
}
12+

0 commit comments

Comments
 (0)