Sub topic 17
Auditing deployments with CloudTrail
This chapter assumes you have a working OIDC deployment role from the earlier chapters. You should also know basic AWS CLI usage and have read-only access to CloudTrail. The goal is to answer one question after the fact: which workflow run did what in my account?
What CloudTrail records
AWS CloudTrail logs management API calls. An OIDC deployment produces two kinds of records:
- One
AssumeRoleWithWebIdentityevent when the job exchanges its token for credentials. - Many events for the calls the job then makes, such as
PutObjectorUpdateService.
Name the session after the run
The first event shows the sub claim, which identifies the repository and branch or environment. The later events only show the assumed-role ARN, which ends with the session name. By default the action uses a generic name, so every run looks the same. Set a unique name instead:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy
aws-region: us-east-1
role-session-name: gha-${{ github.run_id }}-${{ github.run_attempt }}Session names allow letters, digits, and =,.@-_, up to 64 characters. With this setting, an S3 event carries an ARN ending in assumed-role/github-deploy/gha-8123456789-1. Searching for that run ID in the GitHub UI takes you straight to the workflow run.
Find role assumptions
Query the last 90 days of management events with the CLI:
aws cloudtrail lookup-events \
--region us-east-1 \
--lookup-attributes AttributeKey=EventName,AttributeValue=AssumeRoleWithWebIdentity \
--max-results 20 \
--query 'Events[].CloudTrailEvent' \
--output text \
| jq -r '[.eventTime, .userIdentity.userName, .requestParameters.roleSessionName, (.errorCode // "ok")] | @tsv'userIdentity.userName holds the token's sub value, for example repo:my-org/my-repo:ref:refs/heads/main. The last column is ok for a success. Denied attempts show an errorCode such as AccessDenied, which helps you spot a fork or an unexpected branch trying to use the role.
Search in the Region you configured in the workflow. The action calls the regional STS endpoint, so the event is logged there.
Trace what a session did
Filter by the session name to list the calls that one run made:
aws cloudtrail lookup-events \
--region us-east-1 \
--lookup-attributes AttributeKey=Username,AttributeValue=gha-8123456789-1 \
--query 'Events[].[EventTime,EventName,EventSource]' \
--output tableExercise
A CloudTrail event shows s3:DeleteObject made by assumed-role/github-deploy/GitHubActions. Why is this hard to attribute, and how do you fix it?
Show answer
Every run uses the same default session name, so the event can't be tied to one workflow run. Set role-session-name in configure-aws-credentials to include github.run_id and github.run_attempt. Then match the session name to the earlier AssumeRoleWithWebIdentity event, whose userIdentity.userName shows the sub claim.