Long-lived AWS access keys are one of the most common causes of cloud security incidents: they leak through repositories, CI logs, laptops, and chat messages, and they keep working until someone notices. Whether you are giving a CI pipeline, a partner, a consultancy, or your own tooling access to an AWS account, there is almost always a way to do it with temporary credentials instead. This guide covers the patterns we use — including for our own client work, where we never hold permanent keys to a client's account.
The building block: IAM roles and STS
An IAM role is an identity with permissions but no permanent credentials. A trusted principal calls AWS STS (AssumeRole) and receives temporary credentials — an access key, secret key, and session token — that expire after a configured duration (one hour by default, up to the role's maximum session duration).
Two policies define a role:
- The trust policy says who may assume it.
- The permissions policies say what the role may do.
Temporary credentials mean that a leaked session expires on its own, every assumption is recorded in AWS CloudTrail with the caller's identity, and access can be revoked instantly by changing the trust policy or deleting the role.
Pattern 1: cross-account role for a tool or partner
When a principal in account A needs access to account B:
- In account B, create a role whose trust policy names account A (or a specific role in it).
- Attach only the permissions needed — for read-only analysis, AWS managed policies such as
ReadOnlyAccessor narrower custom policies. - The principal in account A assumes the role and works with the temporary credentials.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:role/AnalysisRunner" },
"Action": "sts:AssumeRole",
"Condition": { "StringEquals": { "sts:ExternalId": "a-unique-value-per-customer" } }
}
]
}Why the external ID matters
If a third party assumes roles in many customers' accounts, an attacker could trick it into using their role ARN pointing at your account — the confused deputy problem. An external ID that the third party generates uniquely per customer, and that the trust policy requires, prevents that. Any vendor that assumes roles in your account should ask you to configure one.
Separate read and write
Give read access and change access through different roles. A read-only role can be granted broadly for discovery and monitoring; a deployment role should be narrower, used only when a change has been approved, and ideally limited to the resources and services it deploys. Distributing both through a reviewed CloudFormation template makes the grant transparent and easy to remove.
Pattern 2: CI/CD with OIDC federation
CI systems such as GitHub Actions, GitLab CI, and others can issue OpenID Connect tokens describing the job that is running. AWS IAM can trust that identity provider directly, so the pipeline assumes a role with no stored secret at all.
- Create an IAM OIDC identity provider for your CI system (for GitHub:
token.actions.githubusercontent.com). - Create a deploy role whose trust policy allows
sts:AssumeRoleWithWebIdentityfrom that provider. - Restrict the subject claim to your repository and, for production, to a protected branch or environment.
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:my-org/my-repo:environment:production"
}
}Without the sub condition, any repository on the platform could assume the role — the most common and most dangerous misconfiguration of this pattern.
For AWS CDK pipelines, give the CI role permission only to assume the CDK bootstrap roles (cdk-*-deploy-role, cdk-*-file-publishing-role) in the target account, rather than broad administrative permissions.
Pattern 3: people with IAM Identity Center
Engineers should sign in through AWS IAM Identity Center (formerly AWS SSO), connected to your identity provider, and receive short-lived credentials per account and permission set. The AWS CLI supports this directly (aws sso login or aws login), so local tools work without access keys on disk.
Pattern 4: workloads inside AWS
Code running on AWS — Lambda functions, ECS tasks, EC2 instances, CodeBuild projects — gets temporary credentials from its execution role automatically. There is never a reason to put access keys in a function's environment variables. For cross-account calls, the execution role assumes a role in the other account.
Hardening the patterns
- Least privilege: start from specific actions and resources; use IAM Access Analyzer to generate policies from CloudTrail activity and to flag external access.
- Session duration: keep maximum session durations short for powerful roles.
- Conditions: restrict by source account (
aws:SourceAccount), organization (aws:PrincipalOrgID), or tags where relevant. - Permission boundaries and SCPs: cap what roles can do even if a policy is too broad.
- Monitoring: alert on
AssumeRolecalls from unexpected principals and on use of any remaining access keys. - Find and remove old keys: the IAM credential report lists access keys and their last use. Rotate, replace with roles, and delete.
Summary
Use roles with STS everywhere: cross-account roles with external IDs for tools and partners, OIDC federation for CI, IAM Identity Center for people, and execution roles for workloads. Keep read and write access separate, scope trust policies tightly, and treat any remaining access key as technical debt.
Need a review of how your accounts are accessed today? It is part of our architecture review and AWS CDK and DevOps services.