Sub topic 2
Writing the AWS IAM Role Trust Policy
Assume you know basic GitHub Actions jobs, AWS IAM roles and policies, JSON, and YAML. A trust policy controls who may assume a role. A permissions policy controls what the role may do after it is assumed.
Trust the right GitHub workflow
GitHub Actions issues an OpenID Connect (OIDC) token to a workflow that requests one. AWS Security Token Service (STS) checks that token against the IAM OIDC provider and the role's trust policy.
flowchart LR
A[GitHub Actions job] --> B[OIDC token]
B --> C[AWS STS]
C --> D{Trust policy checks}
D -->|allowed| E[Temporary AWS credentials]
D -->|denied| F[Assumption failure]{
"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"
},
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:ref:refs/heads/main"
}
}
}
]
}The aud condition requires AWS STS to be the token audience. The sub condition restricts access to the main branch of one repository. Avoid a broad value such as repo:my-org/*; it could allow unintended repositories.
To require a GitHub environment instead, match its subject claim:
{
"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:environment:production"
}
}Environment protection rules can then require approvals or restrict deployment branches before the job receives AWS access.
Let the workflow request and use the role
The workflow needs id-token: write. This grants permission to request a GitHub OIDC token; it does not grant AWS permissions.
permissions:
id-token: write
contents: read
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 action exchanges the OIDC token for temporary credentials. If assumption fails, check the provider URL, audience, subject pattern, role ARN, and id-token: write permission first.
Exercise
Which trust-policy condition restricts a role to the main branch of one repository?
Show answer
token.actions.githubusercontent.com:sub with the value repo:org/repo:ref:refs/heads/main.