Sphere
Log in Join

Rotating an exposed AWS key without downtime: IAM credentials and GitHub Actions secrets

Someone ran a secret scan across our repos and found an AWS access key in a commit from eleven months ago. It was in a ci-deploy IAM user, which had write access to two S3 buckets and could update one ECS service. Not catastrophic, but not something to leave alone. We had a release going out in two hours, so there was no question of just deleting the key.

## Make a second credential first

The user can hold two access keys. I created a new one and left the old one active:

bash
aws iam list-access-keys --user-name ci-deploy
aws iam create-access-key --user-name ci-deploy --output json > /tmp/newkey.json

jq -r '.AccessKey.AccessKeyId' /tmp/newkey.json and the secret key give you the two values. Don't echo them into a shared terminal or paste them into a ticket. We put them in a temporary file with 600 permissions and deleted it at the end.

One thing I nearly skipped: I checked the old key's last used date with aws iam get-access-key-last-used --user-name ci-deploy --access-key-id AKIAOLDKEYEXAMPLE before touching it. It had been used that morning, which confirmed the credential was genuinely live and that this couldn't wait for a quieter day.

## Rolling the secrets through GitHub

Our workflows live in acme/shipper and use environment secrets for production:

bash
gh secret set AWS_ACCESS_KEY_ID --repo acme/shipper --env production < /tmp/new_id.txt
gh secret set AWS_SECRET_ACCESS_KEY --repo acme/shipper --env production < /tmp/new_secret.txt

Using stdin rather than --body keeps the secret out of shell history. Environment secrets override repository secrets with the same name, which mattered because both existed. I checked with gh secret list --repo acme/shipper --env production and then again without --env to see which values would actually win.

## The verification step that saved us

Before disabling anything, I triggered a deploy with only the new secret available by running the same aws sts get-caller-identity step the workflow uses:

bash
AWS_ACCESS_KEY_ID="$NEW_ID" AWS_SECRET_ACCESS_KEY="$NEW_SECRET" \
aws sts get-caller-identity --region us-east-1

That returned the ci-deploy ARN. Only then did I disable the old key:

bash
aws iam update-access-key --access-key-id AKIAOLDKEYEXAMPLE \
--status Inactive --user-name ci-deploy

Disabled rather than deleted, so we could flip it back in seconds. We deleted it a week later after two clean releases.

## The bit that still bit us

Dependabot has its own secret store. Its PRs kept failing with:

An error occurred (InvalidClientTokenId) when calling the GetCallerIdentity operation: The security token included in the request is invalid.

gh secret set only writes Actions secrets by default. Dependabot doesn't inherit them, so it picked up the old value from its own store. Fixed with gh secret set AWS_ACCESS_KEY_ID --repo acme/shipper --dependabot, and the same for the secret key. Annoying, and easy to miss if your PR checks don't use AWS.

The key itself is still in Git history. Rotating it removed the useful credential; scrubbing history is a separate job we're doing with git filter-repo because someone linked to an old tag. Until that's done, treat it as exposed and keep the permissions narrow.