Sub topic 18
Deploying across multiple AWS accounts
This chapter assumes you have a working OIDC setup for one account. That means an IAM OIDC provider, a role with a trust policy, and a workflow that uses aws-actions/configure-aws-credentials. It also assumes you know basic IAM and GitHub Actions syntax.
Why one role per account
Most teams run separate AWS accounts for dev, staging, and production. An IAM role lives in one account, and so does the OIDC provider that role trusts. To deploy to three accounts, you create the provider and a role in each one.
This keeps the blast radius small. A role in the dev account cannot touch production, whatever the workflow does.
The pattern
Each account gets the same three pieces:
- An OIDC provider for
token.actions.githubusercontent.com. - A deployment role whose trust policy checks the
audandsubclaims. - A permissions policy scoped to what that account's deployment needs.
The trust policies differ in the sub condition. Dev can accept any branch, while production accepts only the production environment.
| Account | Account ID | Role | Allowed `sub` |
|---|---|---|---|
| dev | 111111111111 | github-deploy-dev | `repo:my-org/my-repo:ref:refs/heads/*` |
| prod | 222222222222 | github-deploy-prod | `repo:my-org/my-repo:environment:production` |
Deploying with a matrix
A job can deploy to several accounts by using a matrix. Each matrix entry names a role ARN and a GitHub environment. The environment's protection rules run before the job starts, and the environment name appears in the sub claim.
name: deploy-multi
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
include:
- environment: dev
role: arn:aws:iam::111111111111:role/github-deploy-dev
- environment: production
role: arn:aws:iam::222222222222:role/github-deploy-prod
environment: ${{ matrix.environment }}
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ matrix.role }}
aws-region: us-east-1
- name: Show the assumed identity
run: aws sts get-caller-identity
- name: Deploy site
run: aws s3 sync ./site s3://my-example-bucket --deleteSetting max-parallel: 1 makes dev deploy before production. If the dev job fails, GitHub cancels the remaining jobs by default.
In this example the dev role must allow sub values of the form repo:my-org/my-repo:environment:dev, because the job declares an environment. When a job uses an environment, the sub claim uses the environment form, not the branch form. A trust policy that only allows ref:refs/heads/main will reject it.
{
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:environment:dev"
}
}Common mistakes
- Reusing one role ARN for every account. A role ARN contains an account ID, so each account needs its own.
- Forgetting to create the provider in a new account. The error is an
AssumeRoleWithWebIdentityfailure, because the provider ARN in the trust policy does not exist. - Writing a
subpattern for branches when the job uses an environment.
Keeping accounts consistent
Copying trust policies by hand invites drift. Define the provider and role in a CloudFormation template, as in the earlier chapter on infrastructure as code. Then deploy that template to each account, for example with CloudFormation StackSets. Parameterize the account-specific values, such as the sub pattern.
Exercise
Your production role trusts only repo:my-org/my-repo:ref:refs/heads/main. You add environment: production to the deploy job, and role assumption now fails. Why, and how do you fix it?
Show answer
When a job references an environment, the sub claim becomes repo:my-org/my-repo:environment:production, which no longer matches the branch pattern. Change the trust policy's sub condition to repo:my-org/my-repo:environment:production.