Skip to content

Commit 9cf7b47

Browse files
committed
feat!: Make origin ALB the default.
1 parent cb88d97 commit 9cf7b47

File tree

8 files changed

+136
-83
lines changed

8 files changed

+136
-83
lines changed

README.md

Lines changed: 61 additions & 42 deletions
Large diffs are not rendered by default.

data.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
data "aws_acm_certificate" "imported" {
22
for_each = var.certificate_imported ? toset(["this"]) : toset([])
33

4-
domain = var.certificate_domain != "" ? var.certificate_domain : local.fqdn
4+
domain = coalesce(var.certificate_domain, local.fqdn)
55
statuses = ["ISSUED"]
66
types = ["IMPORTED"]
77
most_recent = true
@@ -25,13 +25,13 @@ data "aws_cloudfront_response_headers_policy" "policy" {
2525
}
2626

2727
data "aws_lb" "origin" {
28-
for_each = var.origin_alb_arn != null ? toset(["this"]) : toset([])
28+
for_each = var.use_custom_origin ? toset([]) : toset(["this"])
2929

3030
arn = var.origin_alb_arn
3131
}
3232

3333
data "aws_lb_listener" "origin" {
34-
for_each = var.origin_alb_arn != null ? toset(["this"]) : toset([])
34+
for_each = var.use_custom_origin ? toset([]) : toset(["this"])
3535

3636
load_balancer_arn = var.origin_alb_arn
3737
port = 443

dns.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ resource "aws_acm_certificate_validation" "validation" {
5050
resource "aws_lb_listener_certificate" "origin" {
5151
# If the origin is an ALB, we need to attach our certificate to its listener
5252
# so that it properly negotiates TLS with the CloudFront "Host" header.
53-
for_each = var.origin_alb_arn != null ? toset(["this"]) : toset([])
53+
for_each = var.use_custom_origin ? toset([]) : toset(["this"])
5454

5555
listener_arn = data.aws_lb_listener.origin["this"].arn
5656
certificate_arn = aws_acm_certificate.subdomain.arn

locals.tf

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
locals {
22
fqdn = join(".", compact([local.subdomain, var.domain]))
3-
subdomain = var.subdomain != null ? var.subdomain : var.environment
4-
# If an origin ALB ARN is provided, use its DNS name; otherwise, use the
5-
# provided origin domain or construct one.
6-
origin_domain = (var.origin_alb_arn != null
7-
? data.aws_lb.origin["this"].dns_name
8-
: (var.origin_domain != "" ? var.origin_domain : join(".", compact(["origin", local.subdomain, var.domain])))
3+
subdomain = coalesce(var.subdomain, var.environment)
4+
prefix = join("-", compact([var.project, var.environment]))
5+
tags = merge(var.tags, { domain : local.fqdn })
6+
7+
# When using a custom origin, use the provided domain or construct one.
8+
# Otherwise, use the DNS name of the origin ALB.
9+
origin_domain = (var.use_custom_origin
10+
? coalesce(var.origin_domain, join(".", compact(["origin", local.subdomain, var.domain])))
11+
: data.aws_lb.origin["this"].dns_name
912
)
10-
prefix = "${var.project}-${var.environment}"
11-
tags = merge(var.tags, { domain : local.fqdn })
1213
}

main.tf

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ resource "aws_cloudfront_distribution" "waf" {
2222
}
2323

2424
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 == null ? toset(["this"]) : toset([])
25+
for_each = var.use_custom_origin ? toset(["this"]) : toset([])
2726

2827
content {
2928
http_port = 80
@@ -36,8 +35,7 @@ resource "aws_cloudfront_distribution" "waf" {
3635
}
3736

3837
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 != null ? toset(["this"]) : toset([])
38+
for_each = var.use_custom_origin ? toset([]) : toset(["this"])
4139

4240
content {
4341
origin_keepalive_timeout = 5
@@ -93,7 +91,7 @@ resource "terraform_data" "origin_alb" {
9391
}
9492

9593
resource "aws_cloudfront_vpc_origin" "this" {
96-
for_each = var.origin_alb_arn != null ? toset(["this"]) : toset([])
94+
for_each = var.use_custom_origin ? toset([]) : toset(["this"])
9795

9896
vpc_origin_endpoint_config {
9997
name = local.prefix
@@ -133,7 +131,7 @@ resource "aws_wafv2_web_acl" "waf" {
133131
dynamic "rule" {
134132
for_each = var.ip_set_rules
135133
content {
136-
name = rule.value.name != "" ? rule.value.name : "${local.prefix}-${rule.key}"
134+
name = coalesce(rule.value.name, join("-", [local.prefix, rule.key]))
137135
priority = rule.value.priority != null ? rule.value.priority : index(var.ip_set_rules, rule.key)
138136

139137
action {
@@ -206,7 +204,7 @@ resource "aws_wafv2_web_acl" "waf" {
206204
dynamic "rule" {
207205
for_each = var.rate_limit_rules
208206
content {
209-
name = rule.value.name != "" ? rule.value.name : "${local.prefix}-rate-${rule.key}"
207+
name = coalesce(rule.value.name, join("-", [local.prefix, "rate", rule.key]))
210208
priority = rule.value.priority != null ? rule.value.priority : index(var.ip_set_rules, rule.key) + length(var.ip_set_rules) + 1
211209

212210
action {

variables.tf

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ variable "domain" {
55

66
variable "certificate_domain" {
77
type = string
8-
description = "Domain for the imported certificate, if different from the endpoint. Used in conjunction with certificate_imported."
9-
default = ""
8+
description = <<EOF
9+
Domain for the imported certificate, if different from the endpoint. Used in
10+
conjunction with `certificate_imported`.
11+
EOF
12+
default = null
1013
}
1114

1215
variable "certificate_imported" {
1316
type = bool
14-
description = "Look up an imported certificate instead of creating a managed one."
17+
description = <<EOF
18+
Look up an imported certificate instead of creating a managed one.
19+
EOF
1520
default = false
1621
}
1722

@@ -29,7 +34,7 @@ variable "environment" {
2934

3035
variable "ip_set_rules" {
3136
type = map(object({
32-
name = optional(string, "")
37+
name = optional(string, null)
3338
action = optional(string, "allow")
3439
priority = optional(number, null)
3540
arn = string
@@ -50,19 +55,28 @@ variable "log_group" {
5055

5156
variable "origin_alb_arn" {
5257
type = string
53-
description = "ARN of the Application Load Balancer this deployment will point to. If set, origin_domain is ignored."
58+
description = <<EOF
59+
ARN of the Application Load Balancer this deployment will point to. Required
60+
unless `use_custom_origin` is set to `true`.
61+
EOF
5462
default = null
5563
}
5664

5765
variable "origin_domain" {
5866
type = string
59-
description = "Origin domain this deployment will point to. Defaults to origin.subdomain.domain."
60-
default = ""
67+
description = <<EOF
68+
Optional custom origin domain to point to. Defaults to
69+
`origin.subdomain.domain`. Only used if `use_custom_origin` is set to
70+
`true`.
71+
EOF
72+
default = null
6173
}
6274

6375
variable "passive" {
6476
type = bool
65-
description = "Enable passive mode for the WAF, counting all requests rather than blocking."
77+
description = <<EOF
78+
Enable passive mode for the WAF, counting all requests rather than blocking.
79+
EOF
6680
default = false
6781
}
6882

@@ -73,7 +87,7 @@ variable "project" {
7387

7488
variable "rate_limit_rules" {
7589
type = map(object({
76-
name = optional(string, "")
90+
name = optional(string, null)
7791
action = optional(string, "block")
7892
limit = optional(number, 10)
7993
window = optional(number, 60)
@@ -98,7 +112,10 @@ variable "request_policy" {
98112
"Elemental-MediaTailor-PersonalizedManifests",
99113
"UserAgentRefererHeaders"
100114
], var.request_policy)
101-
error_message = "Invalid request policy. See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-origin-request-policies.html"
115+
error_message = <<EOF
116+
Invalid request policy. See
117+
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-origin-request-policies.html
118+
EOF
102119
}
103120
}
104121

@@ -125,10 +142,22 @@ variable "upload_paths" {
125142

126143
variable "upload_rules_capacity" {
127144
type = number
128-
description = "Capacity for the upload rules group. Attempts to determine the capacity if left empty."
145+
description = <<EOF
146+
Capacity for the upload rules group. Attempts to determine the capacity if
147+
left empty.
148+
EOF
129149
default = null
130150
}
131151

152+
variable "use_custom_origin" {
153+
type = bool
154+
description = <<EOF
155+
Use a custom origin configuration instead of an ALB. If set to `true`,
156+
`origin_alb_arn` must also be set.
157+
EOF
158+
default = false
159+
}
160+
132161
variable "webhooks" {
133162
type = map(object({
134163
paths = list(object({
@@ -150,12 +179,18 @@ variable "webhooks" {
150179

151180
variable "webhooks_priority" {
152181
type = number
153-
description = "Priority for the webhooks rule group. By default, an attempt is made to place it before other rules that block traffic."
182+
description = <<EOF
183+
Priority for the webhooks rule group. By default, an attempt is made to
184+
place it before other rules that block traffic.
185+
EOF
154186
default = null
155187
}
156188

157189
variable "webhook_rules_capacity" {
158190
type = number
159-
description = "Capacity for the webhook rules group. Attempts to determine the capacity if left empty."
191+
description = <<EOF
192+
Capacity for the webhook rules group. Attempts to determine the capacity if
193+
left empty.
194+
EOF
160195
default = null
161196
}

versions.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
terraform {
2-
required_version = ">= 1.6"
2+
required_version = ">= 1.10"
33

44
required_providers {
55
aws = {
6-
version = ">= 5.44"
6+
version = ">= 6.0"
77
source = "hashicorp/aws"
88
}
99
}

webhooks.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
resource "aws_wafv2_rule_group" "webhooks" {
22
for_each = length(var.webhooks) > 0 ? toset(["this"]) : toset([])
33

4-
name_prefix = "${local.prefix}-webhooks-"
4+
name_prefix = join("-", [local.prefix, "webhooks"])
55
scope = "CLOUDFRONT"
66
capacity = var.webhook_rules_capacity == null ? 50 : var.webhook_rules_capacity
77

88
visibility_config {
99
cloudwatch_metrics_enabled = true
10-
metric_name = "${local.prefix}-webhooks"
10+
metric_name = join("-", [local.prefix, "webhooks"])
1111
sampled_requests_enabled = true
1212
}
1313

@@ -16,11 +16,11 @@ resource "aws_wafv2_rule_group" "webhooks" {
1616
for_each = var.webhooks
1717

1818
content {
19-
name = "${local.prefix}-webhooks-${rule.key}-label"
19+
name = join("-", [local.prefix, "webhooks", rule.key, "label"])
2020
priority = index(keys(var.webhooks), rule.key)
2121

2222
rule_label {
23-
name = "webhook:${rule.key}"
23+
name = join(":", ["webhook", rule.key])
2424
}
2525

2626
action {
@@ -29,7 +29,7 @@ resource "aws_wafv2_rule_group" "webhooks" {
2929

3030
visibility_config {
3131
cloudwatch_metrics_enabled = true
32-
metric_name = "${local.prefix}-webhooks-${rule.key}-label"
32+
metric_name = join("-", [local.prefix, "webhooks", rule.key, "label"])
3333
sampled_requests_enabled = true
3434
}
3535

@@ -91,7 +91,7 @@ resource "aws_wafv2_rule_group" "webhooks" {
9191
for_each = var.webhooks
9292

9393
content {
94-
name = "${local.prefix}-webhooks-${rule.key}"
94+
name = join("-", [local.prefix, "webhooks", rule.key])
9595
priority = index(keys(var.webhooks), rule.key) + length(keys(var.webhooks))
9696

9797
action {
@@ -113,7 +113,7 @@ resource "aws_wafv2_rule_group" "webhooks" {
113113

114114
visibility_config {
115115
cloudwatch_metrics_enabled = true
116-
metric_name = "${local.prefix}-webhooks-${rule.key}"
116+
metric_name = join("-", [local.prefix, "webhooks", rule.key])
117117
sampled_requests_enabled = true
118118
}
119119

0 commit comments

Comments
 (0)