Sub topic 11
Putting it together: a deployment workflow
This chapter assembles the pieces from earlier chapters into one working deployment. It assumes you can read a GitHub Actions workflow, know what an IAM role and trust policy are, and have used the AWS CLI. You should already have the IAM OIDC provider and a role named github-deploy whose trust policy matches your repository.
The moving parts
A deployment needs four things to agree with each other:
- The IAM OIDC provider for
token.actions.githubusercontent.com. - A role trust policy that accepts tokens from your repository and branch.
- A permissions policy on the role that allows the deployment actions.
- A workflow that requests a token and assumes the role.
The workflow
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 --deleteWhat each part does
on.push.branches: [main]makes thesubclaim of the token end inref:refs/heads/main. This matches the trust policy condition from the earlier chapter.id-token: writelets the job request an OIDC token. Without it, the action fails because no token is available.contents: readis listed explicitly. Once you setpermissions, every permission you do not list is set tonone, andactions/checkoutneeds this one.configure-aws-credentialsrequests the token, callsAssumeRoleWithWebIdentity, and exports temporary credentials as environment variables for later steps.aws sts get-caller-identityconfirms which role the job is using. The output should show anassumed-role/github-deploy/...ARN.aws s3 sync --deletepublishes the files. It also removes objects in the bucket that are not in./site.
Checking the result
Run the workflow by pushing to main. In the job log, the get-caller-identity step should print the role session ARN. If the step that assumes the role fails, check these in order:
- The job has
id-token: write. - The role ARN and account ID are correct.
- The trust policy
submatches the branch that triggered the run. - The
audcondition issts.amazonaws.com.
Exercise
A job sets permissions: id-token: write and nothing else, then runs actions/checkout@v4 on a private repository. What goes wrong, and how do you fix it?
Show answer
Setting permissions sets every unlisted permission to none, so the job loses contents: read and checkout cannot read the private repository. Add contents: read under permissions.