Skip to content

Commit

Permalink
Generate code for creating modules across all regions but only instan…
Browse files Browse the repository at this point in the history
…tiate them on enabled regions
  • Loading branch information
cristim committed Feb 27, 2023
1 parent 8cebbbc commit 89100cb
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 44 deletions.
10 changes: 3 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ module "label" {
enabled = true
}

data "aws_regions" "current" {
all_regions = true
}
data "aws_regions" "current" {}

locals {
all_regions = data.aws_regions.current.names
all_usable_regions = setsubtract(local.all_regions, var.unsupported_regions)
regions = var.autospotting_regions_enabled == [] ? local.all_usable_regions : var.autospotting_regions_enabled
all_regions = data.aws_regions.current.names
regions = var.autospotting_regions_enabled == [] ? local.all_regions : var.autospotting_regions_enabled
}

output "regions" {
Expand Down Expand Up @@ -69,7 +66,6 @@ module "regional" {
autospotting_lambda_arn = module.aws_lambda_function.arn
label_context = module.label.context
regions = local.regions
unsupported_regions = var.unsupported_regions
}

resource "aws_lambda_permission" "cloudwatch_events_permission" {
Expand Down
11 changes: 4 additions & 7 deletions modules/regional/generate_regional_code.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
data "aws_regions" "current" {
all_regions = true
}
data "aws_regions" "current" {}

locals {
all_regions = data.aws_regions.current.names
all_usable_regions = setsubtract(local.all_regions, var.unsupported_regions)
all_regions = data.aws_regions.current.names
}

resource "local_file" "providers_tf" {
content = templatefile("${path.module}/providers.tmpl", { regions = local.all_usable_regions })
content = templatefile("${path.module}/providers.tmpl", { regions = local.all_regions })
filename = "${path.module}/providers.tf"
}

resource "local_file" "regional_tf" {
content = templatefile("${path.module}/regional.tmpl", { regions = var.regions })
content = templatefile("${path.module}/regional.tmpl", { regions = local.all_regions })
filename = "${path.module}/regional.tf"
}
5 changes: 5 additions & 0 deletions modules/regional/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ provider "aws" {
region = "ap-northeast-2"
}

provider "aws" {
alias = "ap-northeast-3"
region = "ap-northeast-3"
}

provider "aws" {
alias = "ap-south-1"
region = "ap-south-1"
Expand Down
45 changes: 45 additions & 0 deletions modules/regional/regional.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# To update it just execute "terraform apply" again after changing the "regions" parameter.

module "regional_resources_ap-northeast-1" {
count = contains(var.regions,"ap-northeast-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -13,6 +15,8 @@ module "regional_resources_ap-northeast-1" {
}
}
module "regional_resources_ap-northeast-2" {
count = contains(var.regions,"ap-northeast-2") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -23,7 +27,22 @@ module "regional_resources_ap-northeast-2" {
aws = aws.ap-northeast-2
}
}
module "regional_resources_ap-northeast-3" {
count = contains(var.regions,"ap-northeast-3") ? 1 : 0

source = "./resources"
label_context = var.label_context

autospotting_lambda_arn = var.autospotting_lambda_arn
lambda_iam_role = aws_iam_role.regional_role

providers = {
aws = aws.ap-northeast-3
}
}
module "regional_resources_ap-south-1" {
count = contains(var.regions,"ap-south-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -35,6 +54,8 @@ module "regional_resources_ap-south-1" {
}
}
module "regional_resources_ap-southeast-1" {
count = contains(var.regions,"ap-southeast-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -46,6 +67,8 @@ module "regional_resources_ap-southeast-1" {
}
}
module "regional_resources_ap-southeast-2" {
count = contains(var.regions,"ap-southeast-2") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -57,6 +80,8 @@ module "regional_resources_ap-southeast-2" {
}
}
module "regional_resources_ca-central-1" {
count = contains(var.regions,"ca-central-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -68,6 +93,8 @@ module "regional_resources_ca-central-1" {
}
}
module "regional_resources_eu-central-1" {
count = contains(var.regions,"eu-central-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -79,6 +106,8 @@ module "regional_resources_eu-central-1" {
}
}
module "regional_resources_eu-north-1" {
count = contains(var.regions,"eu-north-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -90,6 +119,8 @@ module "regional_resources_eu-north-1" {
}
}
module "regional_resources_eu-west-1" {
count = contains(var.regions,"eu-west-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -101,6 +132,8 @@ module "regional_resources_eu-west-1" {
}
}
module "regional_resources_eu-west-2" {
count = contains(var.regions,"eu-west-2") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -112,6 +145,8 @@ module "regional_resources_eu-west-2" {
}
}
module "regional_resources_eu-west-3" {
count = contains(var.regions,"eu-west-3") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -123,6 +158,8 @@ module "regional_resources_eu-west-3" {
}
}
module "regional_resources_sa-east-1" {
count = contains(var.regions,"sa-east-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -134,6 +171,8 @@ module "regional_resources_sa-east-1" {
}
}
module "regional_resources_us-east-1" {
count = contains(var.regions,"us-east-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -145,6 +184,8 @@ module "regional_resources_us-east-1" {
}
}
module "regional_resources_us-east-2" {
count = contains(var.regions,"us-east-2") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -156,6 +197,8 @@ module "regional_resources_us-east-2" {
}
}
module "regional_resources_us-west-1" {
count = contains(var.regions,"us-west-1") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand All @@ -167,6 +210,8 @@ module "regional_resources_us-west-1" {
}
}
module "regional_resources_us-west-2" {
count = contains(var.regions,"us-west-2") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand Down
2 changes: 2 additions & 0 deletions modules/regional/regional.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

%{ for region in regions ~}
module "regional_resources_${region}" {
count = contains(var.regions,"${region}") ? 1 : 0

source = "./resources"
label_context = var.label_context

Expand Down
2 changes: 1 addition & 1 deletion modules/regional/resources/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,6 @@ resource "aws_cloudwatch_log_group" "log_group" {

# retain log group on stack update and delete
lifecycle {
prevent_destroy = true
prevent_destroy = false
}
}
7 changes: 4 additions & 3 deletions modules/regional/variables.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

variable "autospotting_lambda_arn" {}
variable "regions" {}
variable "unsupported_regions" {}

variable "regions" {
description = "Regions to handle"
type = list(string)
}

# Label configuration
variable "label_context" {
Expand Down
26 changes: 0 additions & 26 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -391,32 +391,6 @@ variable "lambda_tags" {
}
}

variable "unsupported_regions" {
description = <<EOF
List of relatively recently launched/announced regions that are currently
not supported by AutoSpotting.
This list is expected to evolve over time as new regious are announced or
made available to AWS customers.
In case you notice errors mentioning providers.tf, such as reported in
https://github.com/AutoSpotting/terraform-aws-autospotting/issues/38
it's usually a sign that this list needs to be updated. The line in
providers.tf which may be mentioned in the error message is a good way
to see which region needs to be added. On another hand if you're trying
to run AutoSpotting in a recently launched region mentioned below, you
can always override this variable to remove that region. If the region
actually works fine, please also send a pull request deleting it from
this list to add support for this new region.
EOF
type = list(string)
default = [
"af-south-1",
"ap-east-1",
"ap-northeast-3",
"eu-south-1",
"me-south-1",
]
}

# Label configuration
variable "label_context" {
description = "Used to pass in label module context"
Expand Down

0 comments on commit 89100cb

Please sign in to comment.