Sub topic 2
Requesting a Token and Assuming the Role from a Workflow
Assume you know GitHub Actions jobs and YAML, AWS IAM roles, and JSON Web Tokens (JWTs). OpenID Connect (OIDC) lets a workflow request a short-lived token instead of storing an AWS access key as a secret.
Enable Token Requests
The job needs id-token: write. The AWS credentials action exchanges the GitHub token for temporary credentials by calling AWS Security Token Service (STS).
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/example-deploy-role
aws-region: us-east-1
- run: aws sts get-caller-identityThe role's trust policy still decides whether the token is acceptable. Workflow permissions only allow the job to request a token.
Choose Exact or Wildcard Matching
StringEquals requires one precise value. Use it when you know the complete repository and branch or environment.
StringLike supports wildcards. It is useful for a controlled group of branches, but every matching value becomes trusted.
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/release/*"
}
}
}The wildcard above trusts every branch under release/. Never use a wildcard for the repository name unless all matching repositories are intentionally trusted.
Allow Only the Main Branch
For a branch-specific role, match both the audience (aud) and subject (sub) claims exactly:
{
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:ref:refs/heads/main"
}
}
}The sub value identifies the repository and branch. A pull request or tag produces a different subject, so it does not match this policy.
Allow Only a Deployment Environment
An environment-specific job uses a different subject format:
jobs:
deploy:
environment: productionMatch that environment in the trust policy:
{
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/example-repo:environment:production"
}
}Exercise
Why should a production role usually match an environment subject instead of a wildcard branch subject?
Show answer
The environment subject can be combined with environment approvals and branch rules, while a wildcard branch subject may trust more workflows than intended.