Lesson 3 of 9 · 21m
Roles Beat Access Keys Almost Everywhere
Temporary credentials, instance profiles, OIDC federation, and the few places long-lived keys still appear.
If I could delete one habit from new AWS teams, it would be creating IAM users with access keys because “the app needs credentials.” Long-lived keys leak into shell history, CI logs, laptop backups, screenshots, and old dotfiles. AWS's better pattern is boring: identities assume roles, roles issue temporary credentials, and nothing valuable sits in a file for months.
A role is a permission set plus trust policy
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}]
}That trust policy says EC2 may assume the role. Attach it to an instance profile and the application retrieves short-lived credentials through the instance metadata service. The app does not know or care whether the underlying keys rotated five minutes ago.
aws sts get-caller-identity
aws ec2 describe-instances --region us-east-1If those commands work on an EC2 instance, they are using instance-profile credentials. Do not “fix” missing permissions by writing keys to /home/ec2-user/.aws/credentials; attach the correct permissions to the role.
Federation removes user keys too
For CI, prefer OIDC federation over stored cloud keys. GitHub Actions can assume an AWS role directly:
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/github-deploy-prod
aws-region: us-east-1The trust policy should constrain the repository, branch, and audience. A role any repository in your GitHub organization can assume is not a security boundary; it is a shared secret with extra JSON.
Where long-lived keys still appear
You will find them in legacy vendor integrations, on-premises systems without OIDC, and break-glass operational tooling. Treat them as radioactive inventory: store them in a secrets manager, scope them tightly, rotate them, alarm on use, and write down the owner. “We don't know where this key is used” means it is already incident material.
Field notes
IMDSv2should be required on EC2 instances; it blocks several SSRF-to-credential theft paths that still matter.- CloudTrail events for
AssumeRoleare often more useful than the final API call when reconstructing who did what. - Never put a private key in user data, a Docker image, or a Terraform output. All three have a long history of accidental disclosure.
- Break-glass users exist for emergencies, but they need MFA, monitoring, and a documented procedure — not daily convenience use.
Was this lesson helpful?