Skip to content

Commit 36c5121

Browse files
Add matrixing to prod workflow (#5)
* Add matrixing to prod workflow * Try without intermediate hcl files * Fix indices
1 parent 56aa24b commit 36c5121

File tree

6 files changed

+78
-20
lines changed

6 files changed

+78
-20
lines changed

.github/workflows/release-published.yml

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,74 @@ permissions:
1010

1111

1212
jobs:
13+
build-matrix:
14+
runs-on: ubuntu-latest
15+
outputs:
16+
matrix: ${{ steps.build-matrix.outputs.matrix }}
17+
steps:
18+
- uses: actions/checkout@09d2acae674a48949e3602304ab46fd20ae0c42f
19+
- name: Build Environment Matrix
20+
id: build-matrix
21+
shell: python
22+
env:
23+
PLATFORM_ENVIRONMENT: "production"
24+
run: |
25+
from pathlib import Path
26+
import os
27+
import json
28+
29+
def discover_environments(platform_path: Path = Path("platform")) -> list[Path]:
30+
return [d for d in platform_path.iterdir() if d.is_dir()]
31+
32+
def discover_regions(environment_path: Path) -> list[Path]:
33+
return [d for d in environment_path.iterdir() if d.is_dir()]
34+
35+
def discover_instances(region_path: Path) -> list[Path]:
36+
return [d for d in region_path.iterdir() if d.is_dir()]
37+
38+
try:
39+
ENVIRONMENT = os.environ['PLATFORM_ENVIRONMENT']
40+
except KeyError as ke:
41+
raise ValueError("Environment variable named PLATFORM_ENVIRONMENT was not found. This variable must be supplied so that a matrix of environments can be built!")
42+
43+
if len(ENVIRONMENT) == 0:
44+
raise ValueError("Environment variable PLATFORM_ENVIRONMENT was empty. This variable must be supplied so that a matrix of environments can be built!")
45+
46+
all_environments = discover_environments()
47+
matrix = {"terragrunt_environment": []}
48+
try:
49+
selected_environment = list(filter(lambda x: x.name == ENVIRONMENT, all_environments))[0]
50+
except Exception:
51+
raise ValueError(f"Expected environment '{ENVIRONMENT}' not found in {all_environments}")
52+
53+
regions = discover_regions(environment_path=selected_environment)
54+
55+
for region_path in regions:
56+
region_instances = discover_instances(region_path=region_path)
57+
for instance in region_instances:
58+
matrix["terragrunt_environment"].append({"environment": selected_environment.name, "region": region_path.name, "instance": instance.name})
59+
60+
print("Generated the following environment matrix:")
61+
print(json.dumps(matrix, indent=4))
62+
63+
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
64+
f.write(f"matrix={json.dumps(matrix, separators=(',', ':'))}")
65+
1366
call-terragrunt-deploy:
67+
needs: build-matrix
1468
permissions:
1569
contents: read
1670
id-token: write
71+
strategy:
72+
fail-fast: false
73+
matrix: ${{ fromJson(needs.build-matrix.outputs.matrix) }}
74+
1775
uses: ./.github/workflows/terragrunt-deploy.yml
1876
with:
1977
tf_version: '1.5.5'
2078
tg_version: '0.54.11'
21-
environment: sandbox
22-
region: us-east-2
23-
env_id: '001'
79+
environment: ${{ matrix.terragrunt_environment.environment }}
80+
region: ${{ matrix.terragrunt_environment.region }}
81+
env_id: ${{ matrix.terragrunt_environment.instance }}
82+
2483
secrets: inherit
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
create_package = true
2+
create = true
3+
handler = "app.lambda_handler"
4+
cors = { allow_origins = ["*"] }
5+
source_path = "../../../../../../../src/function/"
6+
name = "platform-sample-lambda-function"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include "root" {
2+
path = find_in_parent_folders()
3+
}
4+
5+
terraform {
6+
source = "git::https://github.com/launchbynttdata/tf-aws-module_primitive-lambda_function//.?ref=1.0.3"
7+
}

platform/sandbox/account.hcl

Lines changed: 0 additions & 4 deletions
This file was deleted.

platform/sandbox/us-east-2/region.hcl

Lines changed: 0 additions & 3 deletions
This file was deleted.

terragrunt.hcl

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11

22
locals {
33
naming_prefix = "sample_lambda"
4-
5-
# Loads the account related details like account name, id etc.
6-
account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))
7-
8-
# Loads the aws region information
9-
region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
10-
11-
account_name = local.account_vars.locals.account_name
12-
region = local.region_vars.locals.region
13-
144
relative_path = path_relative_to_include()
5+
path_parts = split("/", local.relative_path)
6+
account_name = local.path_parts[1]
7+
region = local.path_parts[2]
158
environment_instance = basename(local.relative_path)
169
bucket = "${replace(local.naming_prefix, "_", "-")}-${local.region}-${local.account_name}-${local.environment_instance}-tfstate"
1710
dynamodb_table = "${local.naming_prefix}-${local.region}-${local.account_name}-${local.environment_instance}-tflocks"

0 commit comments

Comments
 (0)