From ded3d6bd4329c9f946ff8be68d3fe2ee1be7219c Mon Sep 17 00:00:00 2001 From: Chris Taylor Date: Mon, 16 Jun 2025 12:31:03 -0500 Subject: [PATCH 1/3] Add matrixing to prod workflow --- .github/workflows/release-published.yml | 65 +++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release-published.yml b/.github/workflows/release-published.yml index aaf915e..b15896e 100644 --- a/.github/workflows/release-published.yml +++ b/.github/workflows/release-published.yml @@ -10,15 +10,74 @@ permissions: jobs: + build-matrix: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.build-matrix.outputs.matrix }} + steps: + - uses: actions/checkout@09d2acae674a48949e3602304ab46fd20ae0c42f + - name: Build Environment Matrix + id: build-matrix + shell: python + env: + PLATFORM_ENVIRONMENT: "production" + run: | + from pathlib import Path + import os + import json + + def discover_environments(platform_path: Path = Path("platform")) -> list[Path]: + return [d for d in platform_path.iterdir() if d.is_dir()] + + def discover_regions(environment_path: Path) -> list[Path]: + return [d for d in environment_path.iterdir() if d.is_dir()] + + def discover_instances(region_path: Path) -> list[Path]: + return [d for d in region_path.iterdir() if d.is_dir()] + + try: + ENVIRONMENT = os.environ['PLATFORM_ENVIRONMENT'] + except KeyError as ke: + raise ValueError("Environment variable named PLATFORM_ENVIRONMENT was not found. This variable must be supplied so that a matrix of environments can be built!") + + if len(ENVIRONMENT) == 0: + raise ValueError("Environment variable PLATFORM_ENVIRONMENT was empty. This variable must be supplied so that a matrix of environments can be built!") + + all_environments = discover_environments() + matrix = {"terragrunt_environment": []} + try: + selected_environment = list(filter(lambda x: x.name == ENVIRONMENT, all_environments))[0] + except Exception: + raise ValueError(f"Expected environment '{ENVIRONMENT}' not found in {all_environments}") + + regions = discover_regions(environment_path=selected_environment) + + for region_path in regions: + region_instances = discover_instances(region_path=region_path) + for instance in region_instances: + matrix["terragrunt_environment"].append({"environment": selected_environment.name, "region": region_path.name, "instance": instance.name}) + + print("Generated the following environment matrix:") + print(json.dumps(matrix, indent=4)) + + with open(os.environ["GITHUB_OUTPUT"], "a") as f: + f.write(f"matrix={json.dumps(matrix, separators=(',', ':'))}") + call-terragrunt-deploy: + needs: build-matrix permissions: contents: read id-token: write + strategy: + fail-fast: false + matrix: ${{ fromJson(needs.build-matrix.outputs.matrix) }} + uses: ./.github/workflows/terragrunt-deploy.yml with: tf_version: '1.5.5' tg_version: '0.54.11' - environment: sandbox - region: us-east-2 - env_id: '001' + environment: ${{ matrix.terragrunt_environment.environment }} + region: ${{ matrix.terragrunt_environment.region }} + env_id: ${{ matrix.terragrunt_environment.instance }} + secrets: inherit \ No newline at end of file From 7b091e34b9bbbea7d8c4d01417a95657e6224704 Mon Sep 17 00:00:00 2001 From: Chris Taylor Date: Mon, 16 Jun 2025 13:04:15 -0500 Subject: [PATCH 2/3] Try without intermediate hcl files --- platform/production/us-east-2/000/terraform.tfvars | 6 ++++++ platform/production/us-east-2/000/terragrunt.hcl | 7 +++++++ platform/sandbox/account.hcl | 4 ---- platform/sandbox/us-east-2/region.hcl | 3 --- terragrunt.hcl | 13 +++---------- 5 files changed, 16 insertions(+), 17 deletions(-) create mode 100644 platform/production/us-east-2/000/terraform.tfvars create mode 100644 platform/production/us-east-2/000/terragrunt.hcl delete mode 100644 platform/sandbox/account.hcl delete mode 100644 platform/sandbox/us-east-2/region.hcl diff --git a/platform/production/us-east-2/000/terraform.tfvars b/platform/production/us-east-2/000/terraform.tfvars new file mode 100644 index 0000000..9edc1f4 --- /dev/null +++ b/platform/production/us-east-2/000/terraform.tfvars @@ -0,0 +1,6 @@ +create_package = true +create = true +handler = "app.lambda_handler" +cors = { allow_origins = ["*"] } +source_path = "../../../../../../../src/function/" +name = "platform-sample-lambda-function" diff --git a/platform/production/us-east-2/000/terragrunt.hcl b/platform/production/us-east-2/000/terragrunt.hcl new file mode 100644 index 0000000..d9aaa73 --- /dev/null +++ b/platform/production/us-east-2/000/terragrunt.hcl @@ -0,0 +1,7 @@ +include "root" { + path = find_in_parent_folders() +} + +terraform { + source = "git::https://github.com/launchbynttdata/tf-aws-module_primitive-lambda_function//.?ref=1.0.3" +} \ No newline at end of file diff --git a/platform/sandbox/account.hcl b/platform/sandbox/account.hcl deleted file mode 100644 index 2bdb99f..0000000 --- a/platform/sandbox/account.hcl +++ /dev/null @@ -1,4 +0,0 @@ - -locals { - account_name = "sandbox" -} \ No newline at end of file diff --git a/platform/sandbox/us-east-2/region.hcl b/platform/sandbox/us-east-2/region.hcl deleted file mode 100644 index 7e1c44a..0000000 --- a/platform/sandbox/us-east-2/region.hcl +++ /dev/null @@ -1,3 +0,0 @@ -locals { - region = "us-east-2" -} \ No newline at end of file diff --git a/terragrunt.hcl b/terragrunt.hcl index f8b2c49..d9ce2ad 100644 --- a/terragrunt.hcl +++ b/terragrunt.hcl @@ -1,17 +1,10 @@ locals { naming_prefix = "sample_lambda" - - # Loads the account related details like account name, id etc. - account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl")) - - # Loads the aws region information - region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl")) - - account_name = local.account_vars.locals.account_name - region = local.region_vars.locals.region - relative_path = path_relative_to_include() + path_parts = split("/", local.relative_path) + account_name = local.path_parts[0] + region = local.path_parts[1] environment_instance = basename(local.relative_path) bucket = "${replace(local.naming_prefix, "_", "-")}-${local.region}-${local.account_name}-${local.environment_instance}-tfstate" dynamodb_table = "${local.naming_prefix}-${local.region}-${local.account_name}-${local.environment_instance}-tflocks" From c3de825e4bde8f8268973a35b043768065c3800d Mon Sep 17 00:00:00 2001 From: Chris Taylor Date: Mon, 16 Jun 2025 13:06:35 -0500 Subject: [PATCH 3/3] Fix indices --- terragrunt.hcl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terragrunt.hcl b/terragrunt.hcl index d9ce2ad..51a78cb 100644 --- a/terragrunt.hcl +++ b/terragrunt.hcl @@ -3,8 +3,8 @@ locals { naming_prefix = "sample_lambda" relative_path = path_relative_to_include() path_parts = split("/", local.relative_path) - account_name = local.path_parts[0] - region = local.path_parts[1] + account_name = local.path_parts[1] + region = local.path_parts[2] environment_instance = basename(local.relative_path) bucket = "${replace(local.naming_prefix, "_", "-")}-${local.region}-${local.account_name}-${local.environment_instance}-tfstate" dynamodb_table = "${local.naming_prefix}-${local.region}-${local.account_name}-${local.environment_instance}-tflocks"