Probably a very unpopular approach, but I’ve created a Makefile and a main.tf-template file. to do this. I iterate over a list of account IDs, change the main.tf-template to that account ID, then apply, repeat.
Makefile:
confirm:
@echo -n ":: Are you sure you want to proceed? [y/N] " && read ans && [ $${ans:-N} = y ]
get-account-ids: clean
@cat main.tf-template | sed s/%TEAM_ACCOUNT_NUMBER%/temp/g > main.tf
@terraform init -upgrade
@terraform apply -target null_resource.team_account_ids -auto-approve
@terraform output -json team_account_ids | jq -r ‘.’ > team_account_ids.txt
check-account-set:
@if [ -z $(account) ]; then echo “\033[0;31m>> No account var set\033[0m”; exit 1; fi
set-main-template:
@cat main.tf-template | sed s/%TEAM_ACCOUNT_NUMBER%/$(account)/g > main.tf
prep-environment:
@tfenv use
@$(call .clean)
@terraform init -upgrade
apply-all: get-account-ids
for account_id in $$(cat team_account_ids.txt); do
cat main.tf-template | sed s/%TEAM_ACCOUNT_NUMBER%/$${account_id}/g > main.tf;
$(call .clean);
tfenv use && terraform init -upgrade;
terraform apply -var team_account_number=$${account_id} -auto-approve;
done
@$(call .clean-all)
destroy-all: confirm get-account-ids prep-environment
for account_id in $$(cat team_account_ids.txt); do
cat main.tf-template | sed s/%TEAM_ACCOUNT_NUMBER%/$${account_id}/g > main.tf;
$(call .clean);
tfenv use && terraform init -upgrade;
terraform destroy -var team_account_number=$${account_id} -auto-approve;
done
@$(call .clean-all)
plan-single: check-account-set set-main-template prep-environment
terraform plan -var team_account_number=$(account) -out=$(account).tfplan
destroy-plan-single: check-account-set set-main-template prep-environment
terraform plan -var team_account_number=$(account) -destroy -out=$(account).tfplan
apply-single: plan-single
terraform apply -auto-approve $(account).tfplan
@$(call .clean-all)
destroy-single: destroy-plan-single confirm
terraform apply -destroy -auto-approve $(account).tfplan
@$(call .clean-all)
.clean = rm -Rf .terraform *.tfplan
.clean-all = rm -Rf main.tf .terraform *.tfplan
clean:
$(call .clean-all)
main.tf-template:
provider "aws" {
region = var.aws_region
assume_role {
role_arn = “arn:aws:iam::${var.team_account_number}:role/OrganizationAccountAccessRole”
}
default_tags {
tags = {
terraform_managed = “true”
}
}
}
terraform {
backend “s3” {
region = “us-east-1”
bucket = “example-terraform-states”
key = “accounts/teams/%TEAM_ACCOUNT_NUMBER%/iam/terraform.tfstate”
}
}
# Perform the following to get the list of accounts to loop through:
terraform apply -target null_resource.team_account_ids -auto-approve
terraform output -json team_account_ids | jq -r ‘.’ > team_account_ids.txt
Alternatively, for the 2nd command, to apply directly/immediately:
for account_id in $(terraform output -json team_account_ids | jq -r ‘.’); do terraform apply -var team_account_number=${account_id} -auto-approve; done
resource “null_resource” “team_account_ids” {
triggers = {
team_account_ids = length(data.terraform_remote_state.organization.outputs.team_account_ids)
}
}