Sub topic 18
Deploying across multiple AWS accounts
This chapter assumes you have finished the earlier chapters in this tutorial. You should be comfortable with an IAM OIDC identity provider, a role trust policy with sub and aud conditions, and the configure-aws-credentials action. You should also know how to run the AWS CLI against more than one account.
Why split deployments across accounts
Many teams keep staging and production in separate AWS accounts. An account is the strongest isolation boundary AWS offers. A mistake or a compromised credential in staging cannot reach production resources, and billing and audit trails stay separate.
OIDC federation fits this model well, because nothing is stored in GitHub. You only have to decide where the trust lives and how the workflow picks the right role.
What is account-scoped
Both IAM objects from earlier chapters belong to a single account:
- An IAM OIDC identity provider exists only in the account where you created it. If the production account has no provider for
token.actions.githubusercontent.com, no role there can trust GitHub tokens. - A role's trust policy names the provider by ARN, and that ARN contains the account ID.
So each target account needs its own provider and its own deployment role. The GitHub token is the same everywhere. What changes is which account's provider validates it and which role's trust policy matches its sub claim.
Pattern 1: one role per account, chosen by environment
This is the simplest pattern and the one to start with. Each account has a role that trusts a different GitHub environment:
| Account | Role | Trusted `sub` |
|---|---|---|
| Staging (`111111111111`) | `github-deploy` | `repo:my-org/my-repo:environment:staging` |
| Production (`222222222222`) | `github-deploy` | `repo:my-org/my-repo:environment:production` |
The production account's trust policy looks like this. Only the provider ARN and the sub value differ from the staging account's policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::222222222222:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:environment:production"
}
}
}
]
}Because the match is on the environment, a job that does not declare environment: production cannot get production credentials. That is true even on the main branch. The environment's protection rules, such as required reviewers, run before the job starts and before GitHub issues the token.
In GitHub, define an environment-scoped variable named AWS_ROLE_ARN in each environment, along with SITE_BUCKET. The workflow then uses the same variable name in both jobs, and each environment supplies a different value.
name: deploy-multi-account
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
staging:
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
aws-region: us-east-1
- name: Show the assumed identity
run: aws sts get-caller-identity
- name: Deploy to staging
run: aws s3 sync ./site s3://${{ vars.SITE_BUCKET }} --delete
production:
needs: staging
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
aws-region: us-east-1
- name: Show the assumed identity
run: aws sts get-caller-identity
- name: Deploy to production
run: aws s3 sync ./site s3://${{ vars.SITE_BUCKET }} --deleteThe needs: staging line makes production wait for a successful staging deployment. The get-caller-identity step prints the account ID in the ARN, which is a quick way to confirm each job landed in the right account.
Pattern 2: a hub role that assumes spoke roles
Sometimes one job must touch several accounts, for example a pipeline that publishes an artifact to a shared-services account and then deploys to a workload account. You can register GitHub as a provider in only one "hub" account. The hub role then assumes a role in each "spoke" account. This is called role chaining.
Each spoke role trusts the hub role instead of GitHub:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111111111111:role/github-hub"
},
"Action": ["sts:AssumeRole", "sts:TagSession"]
}
]
}The workflow assumes the hub role through OIDC and then assumes the spoke role with the credentials it already has:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::111111111111:role/github-hub
aws-region: us-east-1
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::333333333333:role/spoke-deploy
aws-region: us-east-1
role-chaining: trueThe action adds session tags when it calls AssumeRole, so the spoke trust policy needs sts:TagSession as well as sts:AssumeRole.
Costs of chaining
Chaining reduces the number of providers, but it has real downsides:
- AWS caps a chained session at one hour, and you cannot raise it with
role-duration-seconds. - The
subcondition protects only the hub. The spoke trusts the whole hub role, so any workflow that can assume the hub can reach every spoke it trusts, unless you add more conditions. - The spoke's CloudTrail events show the hub role as the caller. Tracing a deployment back to a repository takes extra work, using the session name and tags.
Choosing between the patterns
| Question | Per-account roles | Hub and spoke |
|---|---|---|
| Where does the GitHub trust live? | In every target account | In one account |
| Blast radius of a loose `sub` condition | One account | Every spoke the hub reaches |
| Maximum session length | Up to the role's maximum | One hour |
| Best fit | Staging and production deployments | One job that must touch several accounts |
Default to per-account roles. Use a hub only when a single job really needs several accounts, and then keep each spoke's permissions policy narrow.
Rolling out the provider to many accounts
Creating a provider and role by hand in ten accounts invites drift. Use the infrastructure as code approach from the previous chapter and deploy the same template everywhere. In AWS Organizations, a CloudFormation StackSet can push the provider and role template to every account in an organizational unit. Parameterize the values that change: the repository, the allowed sub pattern, and the role name.
If you script the setup with the CLI instead, remember that each account needs its own credentials or profile. Run the create-open-id-connect-provider command once per account. Creating the provider a second time in the same account fails with an EntityAlreadyExists error.
Common mistakes
- Pasting the staging role ARN into the production job. The assumption either fails, or it succeeds and deploys to the wrong account. Print
aws sts get-caller-identityto catch this. - Forgetting the provider in a new account. The error is
Not authorized to perform sts:AssumeRoleWithWebIdentity, and it can look like a badsubcondition. Check that the provider exists first. - Using a wildcard
subsuch asrepo:my-org/*in a production account. Any repository in the organization could then assume the role. - Putting
sts:AssumeRoleWithWebIdentityin a spoke trust policy. Spokes trust a role principal, so they usests:AssumeRole.
Exercise
Your production account returns "Not authorized to perform sts:AssumeRoleWithWebIdentity" for a job that declares environment: production, while the same workflow works in staging. The role's sub condition is repo:my-org/my-repo:environment:production. Name two things to check in the production account.
Show answer
First, check that an IAM OIDC provider for token.actions.githubusercontent.com exists in the production account, because providers are per account. Second, check that the role's trust policy names the production account's provider ARN (with the production account ID), not the staging account's. After that, confirm that the aud condition is sts.amazonaws.com and that the sub value matches the repository and environment exactly.