Skip to content

Commit

Permalink
Support multiple AWS regions in Terragrunt
Browse files Browse the repository at this point in the history
Some of our Terraform modules deploy resources to different regions
simultaneously. This is done by defining multiple `provider` blocks for
AWS with different regions and different aliases, which can then be
references in the Terraform resources.

With Terragrunt, we are dynamically creating the `provider` block and
overwriting any additional providers that are defined in Terraform
files. Our script has therefore been extended to accept multiple regions
in the `account.json` file and expand them to the same structure that we
were using before with Terraform.
  • Loading branch information
jdno committed May 3, 2024
1 parent b9d73b1 commit d7674a1
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 18 deletions.
6 changes: 5 additions & 1 deletion terragrunt/accounts/bors-prod/account.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"aws": {
"profile": "bors-prod",
"region": "us-east-2"
"regions": [
{
"region": "us-east-2"
}
]
}
}
6 changes: 5 additions & 1 deletion terragrunt/accounts/bors-staging/account.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"aws": {
"profile": "bors-staging",
"region": "us-east-2"
"regions": [
{
"region": "us-east-2"
}
]
}
}
6 changes: 5 additions & 1 deletion terragrunt/accounts/crates-io-prod/account.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"aws": {
"profile": "crates-io-prod",
"region": "us-west-1"
"regions": [
{
"region": "us-west-1"
}
]
}
}
10 changes: 9 additions & 1 deletion terragrunt/accounts/crates-io-staging/account.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"aws": {
"profile": "crates-io-staging",
"region": "us-west-1"
"regions": [
{
"region": "us-west-1"
},
{
"region": "us-east-1",
"alias": "us-east-1"
}
]
}
}
6 changes: 5 additions & 1 deletion terragrunt/accounts/dev-desktops-prod/account.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"aws": {
"profile": "dev-desktops-prod",
"region": "us-east-1"
"regions": [
{
"region": "us-east-1"
}
]
}
}
6 changes: 5 additions & 1 deletion terragrunt/accounts/legacy/account.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"aws": {
"profile": null,
"region": "us-west-1"
"regions": [
{
"region": "us-west-1"
}
]
}
}
6 changes: 5 additions & 1 deletion terragrunt/accounts/root/account.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"aws": {
"profile": "rust-root",
"region": "us-east-1"
"regions": [
{
"region": "us-east-1"
}
]
}
}
6 changes: 5 additions & 1 deletion terragrunt/accounts/sync-team-prod/account.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"aws": {
"profile": "sync-team-prod",
"region": "us-east-2"
"regions": [
{
"region": "us-east-2"
}
]
}
}
25 changes: 15 additions & 10 deletions terragrunt/terragrunt-locals.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,25 @@ def calculate_remote_state_key(account_json_file, terragrunt_dir):


def calculate_providers_content(account_json):
if account_json["aws"]["profile"] is not None:
return f"""
provider "aws" {{
profile = "{account_json["aws"]["profile"]}"
region = "{account_json["aws"]["region"]}"
}}
"""
else:
return f"""
providers = ""

for region in account_json["aws"]["regions"]:
body = f' region = "{region["region"]}"'

if account_json["aws"]["profile"] is not None:
body += f'\n profile = "{account_json["aws"]["profile"]}"'

if "alias" in region:
body += f'\n alias = "{region["alias"]}"'

providers += f"""
provider "aws" {{
region = "{account_json["aws"]["region"]}"
{body}
}}
"""

return providers


def profile_args(account_json):
if account_json["aws"]["profile"] is not None:
Expand Down

0 comments on commit d7674a1

Please sign in to comment.