Skip to content

Commit 1fe4111

Browse files
authored
feat: Backport cloudposse/cloudfront-cdn/aws improvements (#340)
* chore: Add missing origin_keepalive_timeout and origin_read_timeout variables Backported from cloudposse/terraform-aws-cloudfront-cdn#140 * fix: Configure CORS rules only when there's at least one origin defined * fix: Define sane defaults for module variables * fix: Remove redundant lookup() calls fix: Remove redundant lookup() call * fix: Simplify origin_access_control_id assignment The `for_each` inside the `dynamic "s3_origin_config"` block already checks for `local.origin_access_identity_enabled`, so there's no need to check it again here. * fix: Simplify origin_access_control_id assignment The `local.origin_access_control_enabled` value used to be checked twice; now it's evaluated only once (the logic remains the same). * fix: Sync `variables.tf` defaults with the old `lookup()` func ones * fix: Remove more redundant `lookup()` calls All variables references by `local.website_config` come with default values (see `variables.tf`) which make the func call redundant * feat: Define sane defaults for `ordered_cache` variable * fix: Remove unused `versioning_enabled` variable * docs: Re-generate docs * chore: Add a minimal module instance to the example dir * fix(lambda@edge): Add support for doc auto-generation with atmos * docs: Keep original submodule description * chore: Keep both atmos.yaml files in sync * docs: Fix README title * fix: Update misleading comments * chore: Add missing space * docs: Regenerate Lambda@Edge README.md * feat(custom_origins): Enable shield configuration * fix: Simplify custom_origin_config and origin_shield variables There's no point in wrapping them in the `optional(<type>, <default>)` function - each field has a default value assigned, so given object is always valid * fix: Update error_caching_min_ttl and response_code types * fix: Update OAC variable reference OAC variable is located outside of the `s3_origin_config` block * docs: Re-generate README.md * feat: Enable shield configuration for custom S3 origins * fix: Make origin_shield block optional * feat: Add gRPC support to custom origins * chore: Update default origin_ssl_protocols value * chore: Update default origin_protocol_policy value * fix: `whitelisted_names` param makes sense only when `forward=whitelist` Forwarding cookies to an S3 bucket would be pointless, however in some (rare) cases it may be handy (e.g. Lambda in front of a bucket that reads the cookies) * chore: Remove redundant new line * fix: Reference label vars in the minimal module to enable its deployment * fix: Run init prior to sanity checks * fix: Remove no longer available `-get-plugins=true` flag `-get-plugins` flag was removed in 0.15.0: https://github.com/hashicorp/terraform/blob/v0.15/CHANGELOG.md * fix: Pass context to minimal module instance * fix: Distinguish minimal s3-cdn module instance from the primary one There are 2 s3-cdn module instances - in `examples/complete/main.tf` and `examples/complete/minimal.tf` files respectively. To avoid name collisions, let's add an attribute that differentiates them * fix: Remove unused `.tfvars` file * fix: Prefer secure origin communication by default
1 parent 879e6ec commit 1fe4111

File tree

7 files changed

+160
-85
lines changed

7 files changed

+160
-85
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111

1212
.build-harness
1313
build-harness
14+
15+
.atmos

README.md

Lines changed: 8 additions & 6 deletions
Large diffs are not rendered by default.

examples/complete/minimal.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module "minimal" {
2+
source = "../../"
3+
4+
namespace = var.namespace
5+
stage = var.stage
6+
name = var.name
7+
8+
// This is required to distinguish this module instance from the one in main.tf and to prevent S3 bucket name collisions
9+
attributes = concat(var.attributes, ["minimal"])
10+
11+
cloudfront_access_logging_enabled = false
12+
13+
context = module.this.context
14+
}

main.tf

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ locals {
9898
"me-south-1" = "ap-south-1"
9999
}
100100
origin_shield_region = local.enabled ? lookup(local.origin_shield_region_fallback_map, data.aws_region.current[0].name, data.aws_region.current[0].name) : "this string is never used"
101+
102+
cors_origins = distinct(compact(concat(var.cors_allowed_origins, var.aliases, var.external_aliases)))
101103
}
102104

103105
## Make up for deprecated template_file and lack of templatestring
@@ -318,15 +320,14 @@ resource "aws_s3_bucket" "origin" {
318320
dynamic "website" {
319321
for_each = var.website_enabled ? local.website_config[var.redirect_all_requests_to == "" ? "default" : "redirect_all"] : []
320322
content {
321-
error_document = lookup(website.value, "error_document", null)
322-
index_document = lookup(website.value, "index_document", null)
323-
redirect_all_requests_to = lookup(website.value, "redirect_all_requests_to", null)
324-
routing_rules = lookup(website.value, "routing_rules", null)
323+
error_document = website.value.error_document
324+
index_document = website.value.index_document
325+
redirect_all_requests_to = website.value.redirect_all_requests_to
326+
routing_rules = website.value.routing_rules
325327
}
326328
}
327329
}
328330

329-
330331
resource "aws_s3_bucket_versioning" "origin" {
331332
count = local.create_s3_origin_bucket ? 1 : 0
332333

@@ -350,12 +351,12 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "origin" {
350351
}
351352

352353
resource "aws_s3_bucket_cors_configuration" "origin" {
353-
count = local.create_s3_origin_bucket ? 1 : 0
354+
count = local.create_s3_origin_bucket && length(local.cors_origins) > 0 ? 1 : 0
354355

355356
bucket = one(aws_s3_bucket.origin).id
356357

357358
dynamic "cors_rule" {
358-
for_each = distinct(compact(concat(var.cors_allowed_origins, var.aliases, var.external_aliases)))
359+
for_each = local.cors_origins
359360
content {
360361
allowed_headers = var.cors_allowed_headers
361362
allowed_methods = var.cors_allowed_methods
@@ -524,10 +525,12 @@ resource "aws_cloudfront_distribution" "default" {
524525
dynamic "custom_origin_config" {
525526
for_each = var.website_enabled ? [1] : []
526527
content {
527-
http_port = 80
528-
https_port = 443
529-
origin_protocol_policy = "http-only"
530-
origin_ssl_protocols = var.origin_ssl_protocols
528+
http_port = 80
529+
https_port = 443
530+
origin_protocol_policy = "http-only"
531+
origin_ssl_protocols = var.origin_ssl_protocols
532+
origin_keepalive_timeout = var.origin_keepalive_timeout
533+
origin_read_timeout = var.origin_read_timeout
531534
}
532535
}
533536
dynamic "custom_header" {
@@ -553,22 +556,32 @@ resource "aws_cloudfront_distribution" "default" {
553556
content {
554557
domain_name = origin.value.domain_name
555558
origin_id = origin.value.origin_id
556-
origin_path = lookup(origin.value, "origin_path", "")
557-
origin_access_control_id = lookup(origin.value, "origin_access_control_id", null)
559+
origin_path = origin.value.origin_path
560+
origin_access_control_id = origin.value.origin_access_control_id
561+
558562
dynamic "custom_header" {
559-
for_each = lookup(origin.value, "custom_headers", [])
563+
for_each = origin.value.custom_headers
560564
content {
561565
name = custom_header.value["name"]
562566
value = custom_header.value["value"]
563567
}
564568
}
569+
565570
custom_origin_config {
566-
http_port = lookup(origin.value.custom_origin_config, "http_port", 80)
567-
https_port = lookup(origin.value.custom_origin_config, "https_port", 443)
568-
origin_protocol_policy = lookup(origin.value.custom_origin_config, "origin_protocol_policy", "https-only")
569-
origin_ssl_protocols = lookup(origin.value.custom_origin_config, "origin_ssl_protocols", ["TLSv1.2"])
570-
origin_keepalive_timeout = lookup(origin.value.custom_origin_config, "origin_keepalive_timeout", 60)
571-
origin_read_timeout = lookup(origin.value.custom_origin_config, "origin_read_timeout", 60)
571+
http_port = origin.value.custom_origin_config.http_port
572+
https_port = origin.value.custom_origin_config.https_port
573+
origin_protocol_policy = origin.value.custom_origin_config.origin_protocol_policy
574+
origin_ssl_protocols = origin.value.custom_origin_config.origin_ssl_protocols
575+
origin_keepalive_timeout = origin.value.custom_origin_config.origin_keepalive_timeout
576+
origin_read_timeout = origin.value.custom_origin_config.origin_read_timeout
577+
}
578+
579+
dynamic "origin_shield" {
580+
for_each = origin.value.origin_shield != null ? [origin.value.origin_shield] : []
581+
content {
582+
enabled = origin.value.origin_shield.enabled
583+
origin_shield_region = origin.value.origin_shield.region
584+
}
572585
}
573586
}
574587
}
@@ -578,15 +591,31 @@ resource "aws_cloudfront_distribution" "default" {
578591
content {
579592
domain_name = origin.value.domain_name
580593
origin_id = origin.value.origin_id
581-
origin_path = lookup(origin.value, "origin_path", "")
594+
origin_path = origin.value.origin_path
582595
# the following enables specifying the origin_access_control used by the origin created by this module, prior to the module's creation:
583-
origin_access_control_id = local.origin_access_control_enabled && try(length(origin.value.s3_origin_config.origin_access_control_id), 0) > 0 ? origin.value.s3_origin_config.origin_access_control_id : local.origin_access_control_enabled ? aws_cloudfront_origin_access_control.default[0].id : null
596+
origin_access_control_id = local.origin_access_control_enabled ? (
597+
try(length(origin.value.origin_access_control_id), 0) > 0
598+
? origin.value.origin_access_control_id
599+
: aws_cloudfront_origin_access_control.default[0].id
600+
) : null
584601

585602
dynamic "s3_origin_config" {
586603
for_each = local.origin_access_identity_enabled ? var.s3_origins : []
587604
content {
588605
# the following enables specifying the origin_access_identity used by the origin created by this module, prior to the module's creation:
589-
origin_access_identity = local.origin_access_identity_enabled && try(length(origin.value.s3_origin_config.origin_access_identity), 0) > 0 ? origin.value.s3_origin_config.origin_access_identity : local.origin_access_identity_enabled ? local.cf_access.path : ""
606+
origin_access_identity = (
607+
try(length(origin.value.s3_origin_config.origin_access_identity), 0) > 0
608+
? origin.value.s3_origin_config.origin_access_identity
609+
: local.cf_access.path
610+
)
611+
}
612+
}
613+
614+
dynamic "origin_shield" {
615+
for_each = origin.value.origin_shield_enabled ? [1] : []
616+
content {
617+
enabled = true
618+
origin_shield_region = local.origin_shield_region
590619
}
591620
}
592621
}
@@ -620,7 +649,8 @@ resource "aws_cloudfront_distribution" "default" {
620649
headers = var.forward_header_values
621650

622651
cookies {
623-
forward = var.forward_cookies
652+
forward = var.forward_cookies
653+
whitelisted_names = var.forward_cookies == "whitelist" ? var.forward_cookies_whitelisted_names : null
624654
}
625655
}
626656
}
@@ -636,7 +666,7 @@ resource "aws_cloudfront_distribution" "default" {
636666
for_each = var.lambda_function_association
637667
content {
638668
event_type = lambda_function_association.value.event_type
639-
include_body = lookup(lambda_function_association.value, "include_body", null)
669+
include_body = lambda_function_association.value.include_body
640670
lambda_arn = lambda_function_association.value.lambda_arn
641671
}
642672
}
@@ -667,6 +697,13 @@ resource "aws_cloudfront_distribution" "default" {
667697
origin_request_policy_id = ordered_cache_behavior.value.origin_request_policy_id
668698
realtime_log_config_arn = ordered_cache_behavior.value.realtime_log_config_arn
669699

700+
dynamic "grpc_config" {
701+
for_each = ordered_cache_behavior.value.grpc_config != null ? [ordered_cache_behavior.value.grpc_config] : []
702+
content {
703+
enabled = grpc_config.value.enabled
704+
}
705+
}
706+
670707
dynamic "forwarded_values" {
671708
# If a cache policy or origin request policy is specified, we cannot include a `forwarded_values` block at all in the API request
672709
for_each = (ordered_cache_behavior.value.cache_policy_id != null || ordered_cache_behavior.value.origin_request_policy_id != null) ? [] : [true]
@@ -676,7 +713,7 @@ resource "aws_cloudfront_distribution" "default" {
676713

677714
cookies {
678715
forward = ordered_cache_behavior.value.forward_cookies
679-
whitelisted_names = ordered_cache_behavior.value.forward_cookies_whitelisted_names
716+
whitelisted_names = ordered_cache_behavior.value.forward_cookies == "whitelist" ? ordered_cache_behavior.value.forward_cookies_whitelisted_names : null
680717
}
681718
}
682719
}
@@ -691,7 +728,7 @@ resource "aws_cloudfront_distribution" "default" {
691728
for_each = try(ordered_cache_behavior.value.lambda_function_association, [])
692729
content {
693730
event_type = lambda_function_association.value.event_type
694-
include_body = lookup(lambda_function_association.value, "include_body", null)
731+
include_body = lambda_function_association.value.include_body
695732
lambda_arn = lambda_function_association.value.lambda_arn
696733
}
697734
}
@@ -716,10 +753,10 @@ resource "aws_cloudfront_distribution" "default" {
716753
dynamic "custom_error_response" {
717754
for_each = var.custom_error_response
718755
content {
719-
error_caching_min_ttl = lookup(custom_error_response.value, "error_caching_min_ttl", null)
756+
error_caching_min_ttl = custom_error_response.value.error_caching_min_ttl
720757
error_code = custom_error_response.value.error_code
721-
response_code = lookup(custom_error_response.value, "response_code", null)
722-
response_page_path = lookup(custom_error_response.value, "response_page_path", null)
758+
response_code = custom_error_response.value.response_code
759+
response_page_path = custom_error_response.value.response_page_path
723760
}
724761
}
725762

test/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ all: module examples/complete
3434

3535
## Run basic sanity checks against the module itself
3636
module: export TESTS ?= installed lint get-modules module-pinning get-plugins provider-pinning validate terraform-docs input-descriptions output-descriptions
37-
module: deps
37+
module: init deps
3838
$(call RUN_TESTS, ../)
3939

4040
## Run tests against example
4141
examples/complete: export TESTS ?= installed lint get-modules get-plugins validate
42-
examples/complete: deps
42+
examples/complete: init deps
4343
$(call RUN_TESTS, ../$@)

test/src/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export TF_CLI_ARGS_init ?= -get-plugins=true
21
export TERRAFORM_VERSION ?= $(shell curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r -M '.current_version' | cut -d. -f1-2)
32

43
.DEFAULT_GOAL : all

0 commit comments

Comments
 (0)