From f8e81a74a2c4f3516c0e58d946f0a0e2a31a0245 Mon Sep 17 00:00:00 2001 From: andylamp Date: Fri, 26 Jan 2024 14:52:05 +0200 Subject: [PATCH] Feature: add region explicitly in aws provider to avoid regressions (#15) ## Description This addresses the regression observed in certain configurations as described in issue #14. The PR refactors the project to take the `region` from variables but only _if and only if provided explicitly_. If not, the current behavior is maintained. This refactor means that all modules within the project take the provider region as a parameter which is used for all non aliased (_i.e._ non-global) ones. ## Context Resolves regressions observed in #14. ## Type of changes - [x] Refactoring (non-breaking change) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would change existing functionality) - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist - [x] I am familiar with the [contributing guidelines](../docs/CONTRIBUTING.md) - [x] I have followed the code style of the project - [ ] I have added tests to cover my changes - [ ] I have updated the documentation accordingly - [ ] This PR is a result of pair or mob programming --- ## Sensitive Information Declaration To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including [PII (Personal Identifiable Information) / PID (Personal Identifiable Data)](https://digital.nhs.uk/data-and-information/keeping-data-safe-and-benefitting-the-public) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter. - [x] I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes. --------- Signed-off-by: Thomas Judd-Cooper Co-authored-by: Thomas Judd-Cooper --- locals.tf | 1 + main.tf | 20 ++++++++++++++++--- modules/cloudfront-logs/main.tf | 3 ++- modules/cloudfront-logs/variables.tf | 5 +++++ modules/opennext-assets/main.tf | 3 ++- modules/opennext-assets/variables.tf | 5 ++++- modules/opennext-cloudfront/main.tf | 3 ++- modules/opennext-cloudfront/variables.tf | 15 ++++++++++++++ modules/opennext-lambda/main.tf | 3 ++- modules/opennext-lambda/variables.tf | 5 ++++- modules/opennext-revalidation-queue/main.tf | 1 + .../opennext-revalidation-queue/variables.tf | 5 +++++ variables.tf | 11 ++++++++++ 13 files changed, 71 insertions(+), 9 deletions(-) diff --git a/locals.tf b/locals.tf index a261816..6099b7d 100644 --- a/locals.tf +++ b/locals.tf @@ -16,6 +16,7 @@ locals { restriction_type = "none" locations = [] }) + price_class = coalesce(try(var.cloudfront.price_class, null), "PriceClass_All") cors = merge({ allow_credentials = false, allow_headers = ["*"], diff --git a/main.tf b/main.tf index 8dc0651..1f44aa8 100644 --- a/main.tf +++ b/main.tf @@ -1,5 +1,5 @@ terraform { - required_version = "~> 1.5" + required_version = ">= 1.5" required_providers { aws = { @@ -12,11 +12,16 @@ terraform { data "aws_caller_identity" "current" {} data "aws_region" "current" {} +locals { + aws_region = var.region != null ? var.region : data.aws_region.current.name +} + /** * Assets & Cache S3 Bucket **/ module "assets" { source = "./modules/opennext-assets" + region = local.aws_region default_tags = var.default_tags prefix = "${var.prefix}-assets" @@ -31,6 +36,7 @@ module "assets" { **/ module "server_function" { source = "./modules/opennext-lambda" + region = local.aws_region default_tags = var.default_tags prefix = "${var.prefix}-nextjs-server" @@ -67,6 +73,7 @@ module "server_function" { **/ module "image_optimization_function" { source = "./modules/opennext-lambda" + region = local.aws_region default_tags = var.default_tags prefix = "${var.prefix}-nextjs-image-optimization" @@ -101,6 +108,7 @@ module "image_optimization_function" { **/ module "revalidation_function" { source = "./modules/opennext-lambda" + region = local.aws_region default_tags = var.default_tags prefix = "${var.prefix}-nextjs-revalidation" @@ -136,6 +144,7 @@ module "revalidation_function" { module "revalidation_queue" { source = "./modules/opennext-revalidation-queue" prefix = "${var.prefix}-revalidation-queue" + region = local.aws_region default_tags = var.default_tags aws_account_id = data.aws_caller_identity.current.account_id @@ -148,6 +157,7 @@ module "revalidation_queue" { module "warmer_function" { source = "./modules/opennext-lambda" + region = local.aws_region default_tags = var.default_tags prefix = "${var.prefix}-nextjs-warmer" @@ -184,6 +194,7 @@ module "warmer_function" { **/ module "cloudfront_logs" { source = "./modules/cloudfront-logs" + region = local.aws_region default_tags = var.default_tags log_group_name = "${var.prefix}-cloudfront-logs" @@ -197,16 +208,19 @@ module "cloudfront_logs" { module "cloudfront" { source = "./modules/opennext-cloudfront" prefix = "${var.prefix}-cloudfront" + region = local.aws_region default_tags = var.default_tags + price_class = local.cloudfront.price_class + comment = local.cloudfront.comment logging_bucket_domain_name = module.cloudfront_logs.logs_s3_bucket.bucket_regional_domain_name assets_origin_access_identity = module.assets.cloudfront_origin_access_identity.cloudfront_access_identity_path origins = { assets_bucket = module.assets.assets_bucket.bucket_regional_domain_name - server_function = "${module.server_function.lambda_function_url.url_id}.lambda-url.${data.aws_region.current.name}.on.aws" - image_optimization_function = "${module.image_optimization_function.lambda_function_url.url_id}.lambda-url.${data.aws_region.current.name}.on.aws" + server_function = "${module.server_function.lambda_function_url.url_id}.lambda-url.${local.aws_region}.on.aws" + image_optimization_function = "${module.image_optimization_function.lambda_function_url.url_id}.lambda-url.${local.aws_region}.on.aws" } aliases = local.cloudfront.aliases diff --git a/modules/cloudfront-logs/main.tf b/modules/cloudfront-logs/main.tf index a63a64f..4c9296b 100644 --- a/modules/cloudfront-logs/main.tf +++ b/modules/cloudfront-logs/main.tf @@ -1,5 +1,5 @@ terraform { - required_version = "~> 1.5" + required_version = ">= 1.5" required_providers { aws = { @@ -15,6 +15,7 @@ terraform { } provider "aws" { + region = var.region default_tags { tags = var.default_tags } diff --git a/modules/cloudfront-logs/variables.tf b/modules/cloudfront-logs/variables.tf index c95a0e9..0ac05ab 100644 --- a/modules/cloudfront-logs/variables.tf +++ b/modules/cloudfront-logs/variables.tf @@ -4,6 +4,11 @@ variable "default_tags" { default = {} } +variable "region" { + type = string + description = "The deployment region to be used by the AWS provider." +} + variable "log_group_name" { type = string diff --git a/modules/opennext-assets/main.tf b/modules/opennext-assets/main.tf index 2e5485c..9b66b22 100644 --- a/modules/opennext-assets/main.tf +++ b/modules/opennext-assets/main.tf @@ -1,5 +1,5 @@ terraform { - required_version = "~> 1.5" + required_version = ">= 1.5" required_providers { aws = { @@ -10,6 +10,7 @@ terraform { } provider "aws" { + region = var.region default_tags { tags = var.default_tags } diff --git a/modules/opennext-assets/variables.tf b/modules/opennext-assets/variables.tf index 4049bd5..ff701de 100644 --- a/modules/opennext-assets/variables.tf +++ b/modules/opennext-assets/variables.tf @@ -9,7 +9,10 @@ variable "default_tags" { default = {} } - +variable "region" { + type = string + description = "The deployment region to be used by the AWS provider." +} variable "assets_path" { type = string diff --git a/modules/opennext-cloudfront/main.tf b/modules/opennext-cloudfront/main.tf index 04b13ab..ca45e91 100644 --- a/modules/opennext-cloudfront/main.tf +++ b/modules/opennext-cloudfront/main.tf @@ -1,5 +1,5 @@ terraform { - required_version = "~> 1.5" + required_version = ">= 1.5" required_providers { aws = { @@ -10,6 +10,7 @@ terraform { } provider "aws" { + region = var.region default_tags { tags = var.default_tags } diff --git a/modules/opennext-cloudfront/variables.tf b/modules/opennext-cloudfront/variables.tf index 1ab6fe3..ac899b2 100644 --- a/modules/opennext-cloudfront/variables.tf +++ b/modules/opennext-cloudfront/variables.tf @@ -9,6 +9,11 @@ variable "default_tags" { default = {} } +variable "region" { + type = string + description = "The deployment region to be used by the AWS provider." +} + variable "comment" { type = string description = "Comment to add to the CloudFront distribution" @@ -18,6 +23,16 @@ variable "acm_certificate_arn" { type = string } +variable "price_class" { + type = string + description = "The price class to use for the distribution" + validation { + condition = contains(["PriceClass_200", "PriceClass_100", "PriceClass_All"], var.price_class) + error_message = "Valid values for price_class are: `PriceClass_200`, `PriceClass_100` and `PriceClass_All`." + } + default = "PriceClass_All" +} + variable "origins" { type = object({ assets_bucket = string diff --git a/modules/opennext-lambda/main.tf b/modules/opennext-lambda/main.tf index a7b2f7e..2e2768d 100644 --- a/modules/opennext-lambda/main.tf +++ b/modules/opennext-lambda/main.tf @@ -1,5 +1,5 @@ terraform { - required_version = "~> 1.5" + required_version = ">= 1.5" required_providers { aws = { @@ -14,6 +14,7 @@ terraform { } provider "aws" { + region = var.region default_tags { tags = var.default_tags } diff --git a/modules/opennext-lambda/variables.tf b/modules/opennext-lambda/variables.tf index 40c3422..5afac3e 100644 --- a/modules/opennext-lambda/variables.tf +++ b/modules/opennext-lambda/variables.tf @@ -12,7 +12,10 @@ variable "default_tags" { default = {} } - +variable "region" { + type = string + description = "The deployment region to be used by the AWS provider." +} /** * Create Toggles diff --git a/modules/opennext-revalidation-queue/main.tf b/modules/opennext-revalidation-queue/main.tf index 2e5485c..c44a71b 100644 --- a/modules/opennext-revalidation-queue/main.tf +++ b/modules/opennext-revalidation-queue/main.tf @@ -10,6 +10,7 @@ terraform { } provider "aws" { + region = var.region default_tags { tags = var.default_tags } diff --git a/modules/opennext-revalidation-queue/variables.tf b/modules/opennext-revalidation-queue/variables.tf index 3ace808..7e5b736 100644 --- a/modules/opennext-revalidation-queue/variables.tf +++ b/modules/opennext-revalidation-queue/variables.tf @@ -3,6 +3,11 @@ variable "prefix" { description = "Prefix for created resource IDs" } +variable "region" { + type = string + description = "The deployment region to be used by the AWS provider." +} + variable "default_tags" { type = map(string) description = "Default tags to apply to all created resources" diff --git a/variables.tf b/variables.tf index c11dbac..1b73f07 100644 --- a/variables.tf +++ b/variables.tf @@ -14,6 +14,16 @@ variable "default_tags" { default = {} } +/** + * AWS Provider Variables + **/ +variable "region" { + type = string + description = "The deployment region to be used by the AWS provider." + default = null +} + + /** * Route53 (DNS) Variables **/ @@ -317,6 +327,7 @@ variable "cloudfront" { override = bool value = string }))) + price_class = optional(string) geo_restriction = optional(object({ restriction_type = string locations = list(string)