Apply Terraform to All Accounts in Org

I am trying to determine if there are any tools or processes to deploy a Terraform template across all accounts in an AWS org. This would be 100+ accounts.

More or less we are tying to see if there is a way to replicate some of the functionality of StackSets.

We have a client that would prefer to remain in TF only but wants some of the multi account capabilities.

What have folks done before, if anything?


Related discussions:

This is one of the few use-cases where I resort to CFn Stacksets instead of TF. I believe with TF that would only be possible by having some wrapper apply it to each account.

Agree. So far I haven’t found anything. We use StackSets for this internally as well but the client we have is asking what’s possible.

Certainly going to put StackSets on the recommendation list but want to be prepared with other options if they exist.

It’s a bit gross but you can use TF to create stacksets in your master account. That way you are technically TF only lol.

Oh I have certainly considered that path :)!!

Not gross at all, we’re doing that and it works nicely =)

We do this across many accounts. They are just multiple providers. The annoying thing though is each provider pulls in its own dependencies and that can get really big really fast. But it certainly is easy enough, I deploy to multiple accounts and multiple regions all the time plus touch things that aren’t the aws provider and that are not in aws at all.

Someone correct me if I am wrong here but it really is a drain on storage to have to pull in the same aws provider 100 times at 300+mb. But it works.

All of our multi-account or multi-region stuff just specifies what it needs from providers.tf.

Deploy it from the management account by assuming the OrganizationAccountAccessRole

Provider binary can be cached: https://developer.hashicorp.com/terraform/cli/config/config-file#provider-plugin-cache

Not strictly the same as StackSets but you could maybe use Service Catalog for Terraform?

https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted-Terraform.html

There are ways to orchestrate the provisioning across the accounts (service catalog puppet etc) which should help too.

This is exactly what Terragrunt is made for, no?

https://terragrunt.gruntwork.io

Been a while since I’ve done TF but couldn’t you use a data resource like aws_organizations_organizational_unit_descendant_accounts to list all accounts in the org, then using the `OrganizationAccountAccessRole` loop through each account and perform your terraform actions?

So 1 command to deploy across all the accounts? Or are you applying each one individually?

Yep…we use terragrunt for provisioning guardrails across our accounts
We use an initial stackset to inject a terraform role in each account.
We have a central aws account that we log into using AWS SSo and assume the terraform role in each account using terragrunt

One ring to rule them all :).

But yes, for infrastructure the terraform touches all accounts. For applications we do region by region, account by account. Still trying, discussing, arguing about best practice there especially when you factor in disaster recovery. And depends on how you have your accounts set up and IAM roles created, some people build them with the application, some people keep them in the infrastructure. Some people let you make your own security groups by project, some people like to keep them centralized and no one can make them they must come from infrastructure controlled and reviewed by that team. Some people like to have an “allow ssh” and “allow http” security groups and anything else is special, some people like to have an “$application_name security group” that does everything approved for the app. No real right way honestly. Consistency is king. Sprinkle in a little bit of corporate auditors used to cisco though and that might push you in one direction over the other.

But to make sure I answered your question yes, we have projects that touch every account in every region.

Have you looked at AWS Account Factory for Terraform.

Every account needs it’s own provider instance, so it’s not quite that easy. But if you know the accounts and have a role to assume in each account, you could put the resources in a module and repeat that module consumption for each account, each one for a uniquely aliased provider, you could do it.

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)
}
}

My question is why 100+ aws accounts?
That feels way to crazy to manage

GitHub actions. I rolled this out at my previous company