Skip to content

Commit

Permalink
Feature: add region explicitly in aws provider to avoid regressions (#15
Browse files Browse the repository at this point in the history
)

<!-- markdownlint-disable-next-line first-line-heading -->
## Description

This addresses the regression observed in certain configurations as
described in issue #14.

<!-- Describe your changes in detail. -->

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

<!-- Why is this change required? What problem does it solve? -->

Resolves regressions observed in #14.

## Type of changes

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply. -->

- [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

<!-- Go over all the following points, and put an `x` in all the boxes
that apply. -->

- [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 <thomas.judd-cooper1@nhs.net>
Co-authored-by: Thomas Judd-Cooper <thomas.judd-cooper1@nhs.net>
  • Loading branch information
andylamp and Tomdango authored Jan 26, 2024
1 parent aa210a7 commit f8e81a7
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["*"],
Expand Down
20 changes: 17 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = "~> 1.5"
required_version = ">= 1.5"

required_providers {
aws = {
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/cloudfront-logs/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = "~> 1.5"
required_version = ">= 1.5"

required_providers {
aws = {
Expand All @@ -15,6 +15,7 @@ terraform {
}

provider "aws" {
region = var.region
default_tags {
tags = var.default_tags
}
Expand Down
5 changes: 5 additions & 0 deletions modules/cloudfront-logs/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/opennext-assets/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = "~> 1.5"
required_version = ">= 1.5"

required_providers {
aws = {
Expand All @@ -10,6 +10,7 @@ terraform {
}

provider "aws" {
region = var.region
default_tags {
tags = var.default_tags
}
Expand Down
5 changes: 4 additions & 1 deletion modules/opennext-assets/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/opennext-cloudfront/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = "~> 1.5"
required_version = ">= 1.5"

required_providers {
aws = {
Expand All @@ -10,6 +10,7 @@ terraform {
}

provider "aws" {
region = var.region
default_tags {
tags = var.default_tags
}
Expand Down
15 changes: 15 additions & 0 deletions modules/opennext-cloudfront/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/opennext-lambda/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = "~> 1.5"
required_version = ">= 1.5"

required_providers {
aws = {
Expand All @@ -14,6 +14,7 @@ terraform {
}

provider "aws" {
region = var.region
default_tags {
tags = var.default_tags
}
Expand Down
5 changes: 4 additions & 1 deletion modules/opennext-lambda/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions modules/opennext-revalidation-queue/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ terraform {
}

provider "aws" {
region = var.region
default_tags {
tags = var.default_tags
}
Expand Down
5 changes: 5 additions & 0 deletions modules/opennext-revalidation-queue/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
**/
Expand Down Expand Up @@ -317,6 +327,7 @@ variable "cloudfront" {
override = bool
value = string
})))
price_class = optional(string)
geo_restriction = optional(object({
restriction_type = string
locations = list(string)
Expand Down

0 comments on commit f8e81a7

Please sign in to comment.