Sub topic 3
Putting It Together: Assuming an AWS Role
Assume you know basic GitHub Actions syntax, repository secrets, JSON, and IAM roles. The workflow requests a short-lived OIDC token. AWS validates that token and returns temporary credentials for an IAM role.
Assuming an AWS Role
The workflow needs id-token: write. The AWS action exchanges the GitHub token for temporary role credentials.
name: deploy
on:
push:
branches: [main]
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1
- run: aws sts get-caller-identityThe role's trust policy checks the token's issuer, audience, and subject. The sub claim identifies the requesting repository and branch.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789012: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:octo-org/octo-repo:ref:refs/heads/main"
}
}
}]
}Scoping Trust Safely
Allow only the repositories, branches, tags, environments, or reusable workflows that need access. Require aud to match the cloud service. Avoid wildcard subjects unless the role is intentionally broad.
Troubleshooting Common Failures
- Missing permission: Add
id-token: writeat workflow or job scope. - Audience mismatch: Make the requested audience match the trust policy.
- Subject mismatch: Check the exact
subformat for branches, tags, and environments. - Invalid issuer or expiry: Confirm the provider URL and system time.
- AWS access denied: Separate authentication failure from missing permissions in the role's identity policy.
Other Providers and Reusable Workflows
Azure, Google Cloud, and HashiCorp Vault use the same pattern: register GitHub's issuer, validate the JWT, restrict claims, and exchange it for temporary credentials.
For reusable workflows, inspect job_workflow_ref. Trust can require a specific reusable workflow instead of trusting every caller repository.
Customizing the Subject Claim
GitHub can customize sub through environment or organization settings. Update the cloud trust policy at the same time. Treat the resulting format as an interface: document it, test it, and change it deliberately.
Exercise
A deployment fails because AWS reports an audience mismatch. What should you compare?
Show answer
Compare the token audience requested by the workflow with the aud value required by the IAM trust policy.