Sub topic 11
Putting it together: a deployment workflow
This chapter assumes you have read the earlier chapters. You should know the OIDC token claims (iss, aud, sub). You should have an IAM OIDC provider for token.actions.githubusercontent.com and a role whose trust policy matches your repository. You should also be comfortable with basic GitHub Actions syntax and the AWS CLI.
Here you will wire the pieces into one working deployment. You will follow the workflow step by step. You will also see what happens at runtime, which permissions each side needs, and where the common mistakes occur.
The pieces and who owns them
A working deployment needs four things to agree with each other.
| Piece | Lives in | What it must say |
|---|---|---|
| OIDC provider | AWS IAM | Trusts `token.actions.githubusercontent.com` with audience `sts.amazonaws.com` |
| Trust policy | The IAM role | Allows `sts:AssumeRoleWithWebIdentity` for the right `aud` and `sub` |
| Permissions policy | The IAM role | Lists only the actions the deployment needs |
| Workflow | `.github/workflows/*.yml` | Requests a token with `id-token: write` and names the role ARN |
If any one of these disagrees with the others, role assumption fails. Most failures come from a mismatch between the sub claim the workflow produces and the sub pattern in the trust policy.
What happens when the job runs
The sequence below shows the exchange. No AWS secret is stored in GitHub. The only value the workflow carries is the role ARN, which is an identifier and not a credential.
The aws-actions/configure-aws-credentials action performs the first three arrows for you. It then exports the temporary credentials as environment variables, so every later step in the job can use the AWS CLI or an SDK without extra setup.
The complete workflow
This is the workflow from the plan. It deploys a static site to S3 whenever code is pushed to main.
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
- name: Show the assumed identity
run: aws sts get-caller-identity
- name: Deploy site
run: aws s3 sync ./site s3://my-example-bucket --deleteThe permissions block
id-token: write lets the job request an OIDC token from GitHub. It does not grant any write access to your repository or to AWS. Without it, the action fails because there is no token to exchange.
contents: read is listed explicitly because declaring a permissions block sets every unlisted permission to none. actions/checkout needs contents: read to fetch the code. If you omit it in a private repository, the checkout step fails.
You can set permissions at the workflow level, as above, or per job. Per-job placement is safer when a workflow has several jobs and only one of them talks to AWS.
Checkout
Checkout comes first only because the later step needs the site directory. The credentials step does not depend on it. Order matters only for what each step needs.
Assuming the role
The action reads the ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN values that GitHub provides when id-token: write is set. It requests a JWT with audience sts.amazonaws.com, which is the action's default. It then calls AssumeRoleWithWebIdentity with the role ARN. If you use a different audience, set the audience input to match the aud condition in your trust policy.
Verifying the identity
aws sts get-caller-identity is a cheap check that the role was assumed. A successful run prints an ARN of the form arn:aws:sts::123456789012:assumed-role/github-deploy/GitHubActions. The last segment is the session name. If this step prints an IAM user or fails with "Unable to locate credentials", the assumption did not happen.
Keep this step while you build the pipeline. You can remove it later, but it costs almost nothing and makes logs easier to read.
Deploying
aws s3 sync ... --delete copies the files and removes objects in the bucket that no longer exist locally. The --delete flag is destructive. If you point the command at the wrong bucket or the wrong directory, it removes data. The permissions policy in the next section limits how far that damage can reach.
The matching permissions policy
The role's permissions policy decides what the temporary credentials can do. For the sync above, the role needs to list the bucket and to read, write, and delete objects in it.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-example-bucket"
},
{
"Sid": "ManageObjects",
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::my-example-bucket/*"
}
]
}Note the two resource forms. s3:ListBucket applies to the bucket ARN. The object actions apply to bucket/*. Mixing them up is a frequent cause of AccessDenied errors during sync.
Reading the result
A healthy run shows the following in the logs.
- The credentials step prints the assumed role and does not print any secret values.
get-caller-identityshowsassumed-role/github-deploy/...with account123456789012.s3 synclists uploaded and deleted files.
The account ID and role name in the ARN are not secret, but they tell you which role actually ran. This helps when a repository can assume more than one role.
Edge cases and failure modes
- Pull requests from forks. Fork pull requests do not receive an OIDC token with write capability to your AWS role in the usual way, and the
subclaim for pull request runs has the formrepo:my-org/my-repo:pull_request. The trust policy in this chapter allows onlyref:refs/heads/main, so pull request runs cannot assume the role. This is intended. Do deploy previews with a separate, narrower role. - Branch mismatch. If you change the trigger to
developbut leave the trust policy onmain, thesubclaim will not match. STS rejects the call. - Job uses an environment. When a job declares
environment: production, thesubclaim becomesrepo:my-org/my-repo:environment:production, not the branch form. A branch-based trust policy then fails. The next chapter covers environments. - Region.
aws-regionsets the region for later CLI calls. STS calls and S3 bucket location are separate concerns. A bucket in another region still needs--regionor a matchingaws-region. - Third-party actions. Every step after the credentials step can read the temporary credentials. Pin actions you do not control to a full commit SHA, or limit credentials to jobs that only run trusted steps.
- Credential lifetime. The credentials last one hour by default. A job that runs longer must set
role-duration-secondswithin the role's maximum session duration, or it must assume the role again.
A short debugging routine
When the credentials step fails, check in this order.
- Does the job or workflow have
permissions: id-token: write? A missing permission produces an error about the token request URL. - Is the role ARN correct and in the account you expect?
- Does the trust policy
audmatch the token audience,sts.amazonaws.comby default? - Does the
subpattern match the real claim for this trigger? Compare the branch, environment, or pull request form. - Does the OIDC provider exist in the same account as the role?
Most failures are resolved by steps 1 and 4.
Exercise
A workflow with permissions: id-token: write and contents: read runs on a push to main, but its job declares environment: production. The role's trust policy allows sub equal to repo:my-org/my-repo:ref:refs/heads/main. Will AssumeRoleWithWebIdentity succeed? Explain, and give the fix.
Show answer
No. When a job declares an environment, the sub claim is repo:my-org/my-repo:environment:production, not the branch form. It does not match the trust policy pattern, so STS denies the call. Fix it by changing the trust policy sub condition to repo:my-org/my-repo:environment:production, or by adding that value alongside the branch pattern if both kinds of jobs need the role.
Exercise
aws s3 sync fails with AccessDenied on listing the bucket, although the policy allows s3:ListBucket on arn:aws:s3:::my-example-bucket/*. What is wrong?
Show answer
s3:ListBucket is a bucket-level action, so its Resource must be the bucket ARN arn:aws:s3:::my-example-bucket. The /* form matches only objects. Move the list permission to a statement scoped to the bucket ARN.