Skip to content

Commit 1012c79

Browse files
committed
feat: Use a VPC origin if we have an ALB.
1 parent fa5e50a commit 1012c79

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

main.tf

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,29 @@ resource "aws_cloudfront_distribution" "waf" {
2121
}
2222
}
2323

24-
custom_origin_config {
25-
http_port = 80
26-
https_port = 443
27-
origin_keepalive_timeout = 5
28-
origin_protocol_policy = "https-only"
29-
origin_read_timeout = 30
30-
origin_ssl_protocols = ["TLSv1.2"]
24+
dynamic "custom_origin_config" {
25+
# If we don't have an ALB origin, we need to set up a custom config.
26+
for_each = var.origin_alb_arn ? toset([]) : toset(["this"])
27+
28+
content {
29+
http_port = 80
30+
https_port = 443
31+
origin_keepalive_timeout = 5
32+
origin_protocol_policy = "https-only"
33+
origin_read_timeout = 30
34+
origin_ssl_protocols = ["TLSv1.2"]
35+
}
36+
}
37+
38+
dynamic "vpc_origin_config" {
39+
# If we have an ALB origin, we want to use a VPC origin to connect.
40+
for_each = var.origin_alb_arn ? toset(["this"]) : toset([])
41+
42+
content {
43+
origin_keepalive_timeout = 5
44+
origin_read_timeout = 30
45+
vpc_origin_id = aws_cloudfront_vpc_origin.this["this"].id
46+
}
3147
}
3248
}
3349

@@ -70,6 +86,23 @@ resource "aws_cloudfront_distribution" "waf" {
7086
tags = local.tags
7187
}
7288

89+
resource "aws_cloudfront_vpc_origin" "this" {
90+
for_each = var.origin_alb_arn ? toset(["this"]) : toset([])
91+
92+
vpc_origin_endpoint_config {
93+
name = local.prefix
94+
arn = var.origin_alb_arn
95+
http_port = 80
96+
https_port = 443
97+
origin_protocol_policy = "https-only"
98+
99+
origin_ssl_protocols {
100+
items = ["TLSv1.2"]
101+
quantity = 1
102+
}
103+
}
104+
}
105+
73106
resource "aws_wafv2_web_acl" "waf" {
74107
name = local.prefix
75108
description = "Web application firewall rules for ${var.project}."

0 commit comments

Comments
 (0)