Volume 3 · Chapter 3 — Identity, Security and Compliance
Roles, STS, and Temporary Credentials
3.1 Introduction
The previous chapters established that roles - assumed for temporary credentials - are the preferred way to grant access. The engine behind that is the Security Token Service (STS), which issues the short-lived credentials when a role is assumed. This chapter goes deeper into how role assumption, STS, and federation actually work, because “how do workloads and users get credentials without storing long-lived keys?” is one of the most important security questions in AWS, and the answer - temporary credentials via STS - underpins secure workload identity, cross-account access, and single sign-on.
3.2 The Security Token Service
STS is the service that issues temporary, limited-privilege credentials. When an entity assumes a role, STS returns a set of credentials - an access key, a secret, and a session token - that carry the role’s permissions and expire after a set duration (from minutes to hours). Because they’re short-lived, the damage from a leaked temporary credential is naturally bounded - it stops working when it expires - unlike a long-lived access key that remains valid until someone notices and revokes it. STS is what makes the whole role model work: it turns “assume this role” into a concrete set of credentials the caller uses for the session, then discards.
3.3 How Role Assumption Works
Assuming a role involves a trust relationship. Every role has a trust policy defining who is allowed to assume it - a specific service (so EC2 or Lambda can), a specific account or identity (for cross-account or user access), or a federated identity provider. When an allowed principal calls assume-role, STS checks the trust policy, and if permitted, issues temporary credentials carrying the role’s permissions. So two things govern a role: the trust policy (who may assume it) and the permission policies (what it can do once assumed). This separation is powerful - you control both who can become the role and what the role can do, independently.
sequenceDiagram autonumber participant P as Principal (user/service/IdP) participant STS as AWS STS participant Role as IAM Role participant AWS as AWS Service P->>STS: AssumeRole (request) STS->>Role: check trust policy - is P allowed? Role-->>STS: allowed STS-->>P: temporary credentials (expire soon) P->>AWS: call API with temp credentials AWS->>AWS: authorize via role's permission policies AWS-->>P: response
3.4 Workload Identity and Instance Roles
For workloads, role assumption is automatic and invisible. When you attach a role to an EC2 instance (through an instance profile), to a Lambda function, or to a container task, the runtime automatically obtains and refreshes temporary credentials for that role and the SDK uses them transparently - your code simply calls AWS and it works, with no keys anywhere. On Kubernetes (EKS), the equivalent is associating a Kubernetes service account with an IAM role so each pod gets its own scoped credentials. This is the mechanism that lets applications call AWS services securely without embedded secrets, and it’s why “the app uses a role, not access keys” is the correct answer to how a workload should authenticate.
3.5 Federation and Cross-Account Access
STS also powers federation and cross-account access. For people, identity federation lets users authenticate with an external identity provider - the corporate directory via single sign-on - and then assume an AWS role, receiving temporary credentials, so there are no IAM users or long-lived credentials for employees at all. For cross-account access, a role in one account has a trust policy allowing a principal in another account to assume it, so access between accounts is granted by assuming a role and getting temporary credentials, rather than by sharing keys. Both cases rest on the same foundation: authenticate somehow, then assume a role, then get short-lived credentials from STS - which is the consistent, secure pattern across humans, workloads, and accounts.
3.6 Banking Production Scenario
A bank’s engineers never hold AWS credentials. They sign in through the corporate identity provider with MFA, which federates them into AWS where they assume a role scoped to their team and environment, receiving temporary credentials that expire at the end of the session. Every application - on EC2, Lambda, and EKS - runs under an attached role, automatically receiving and refreshing temporary credentials, so no access keys exist in any codebase or config. Access to the shared services account and the central security account is granted through cross-account role assumption governed by trust policies. Because every credential in the environment is short-lived and issued by STS on assumption, there are effectively no long-lived secrets to steal, and any leaked session credential expires quickly.
3.7 Troubleshooting: Assume-Role and Credential Issues
Role-assumption failures have specific causes. “Not authorized to perform sts:AssumeRole” means the role’s trust policy doesn’t permit the calling principal, or the caller’s identity policy doesn’t allow assuming it - both the trust policy (who may assume) and the caller’s permission to assume must align. Expired -credential errors are expected behaviour for temporary credentials and mean the SDK should refresh them - which it does automatically for instance and task roles, so seeing this in an application usually means something is caching credentials incorrectly rather than using the provided role. If a workload unexpectedly has no credentials, the role may not be attached (missing instance profile, function role, or service account association). For cross-account issues, verify the trust policy names the correct external account or role and that the caller is permitted to assume. The diagnostic pattern is to check the trust policy first (who’s allowed to assume), then the caller’s assume permission, then whether the role is actually attached.
How do temporary credentials and STS work, and why are they more secure than long-lived access keys?
STS - the Security Token Service - is the service that issues temporary, limited-privilege credentials, and it’s the engine behind AWS’s whole role model. When an entity assumes a role, STS returns a set of credentials - an access key, a secret key, and a session token - that carry the role’s permissions and expire after a set duration ranging from minutes to hours. The assumption itself is governed by the role’s trust policy, which defines who is allowed to assume the role - a particular AWS service, a specific account or identity, or a federated identity provider. So when an allowed principal calls assume-role, STS checks the trust policy and, if permitted, hands back temporary credentials carrying the role’s permissions, which the caller uses for the session and then discards. This is how workloads and people get access without storing long-lived keys: a workload with an attached role automatically obtains and refreshes these temporary credentials transparently, and a person federated through single sign-on assumes a role to get them. Why is this more secure than long-lived access keys? Because the credentials are short-lived, the damage from a leak is naturally and severely bounded - a temporary credential stops working when it expires, often within an hour, so even if it’s captured it’s useless shortly after, whereas a long-lived access key remains valid indefinitely until someone notices it’s compromised and manually revokes it, giving an attacker durable access. There’s also nothing permanent to steal in the first place: with roles and STS, there are no static keys embedded in code, configuration, or CI systems - the most common source of breaches - because credentials are minted on demand and expire. Additionally, each assumption is scoped to exactly the role’s permissions and can be constrained further, and the trust policy tightly controls who can even obtain them. So the security advantages compound: no stored long-lived secrets to leak, automatic rotation, a short window of validity that bounds any compromise, and controlled issuance via trust policies. This is exactly why the correct answer to “how should a workload authenticate to AWS?” is “assume a role and use the temporary credentials STS provides, never embedded access keys,” and why mature organizations federate people through SSO to assume roles rather than issuing them permanent credentials - the entire model replaces durable secrets with ephemeral, controlled ones.
How should an application running on AWS authenticate to call other AWS services, and how does cross-account access work?
An application running on AWS should authenticate by assuming an IAM role attached to its compute, never by using embedded access keys. Concretely, you attach a role to the EC2 instance (via an instance profile), to the Lambda function, or to the container task - and on EKS you associate a Kubernetes service account with an IAM role so each pod gets its own identity. The runtime then automatically obtains temporary credentials for that role from STS and refreshes them before they expire, and the AWS SDK picks them up transparently, so the application code simply makes API calls and they’re authorized by the role’s permissions, with no keys stored anywhere in the code, configuration, or environment. This is the single most important workload security practice, because it eliminates long-lived stored credentials - the most common breach source - and gives each workload a tightly-scoped, least-privilege identity whose credentials are short-lived and auto-rotating. Each application gets its own role granting only the services it needs, so a compromise of one service doesn’t hand over access to others. Cross-account access works on the same foundation of role assumption and temporary credentials. You create a role in the target account whose trust policy explicitly allows a principal from the source account to assume it, and you grant that source principal permission to call assume-role on it. When access is needed, the source principal assumes the role in the target account, STS checks the trust policy, and if allowed issues temporary credentials carrying the target role’s permissions - so the source operates in the target account with a short-lived, scoped identity rather than anyone sharing static keys across accounts. This is how a bank grants, say, its central logging or security account access into workload accounts, or lets a workload account reach a shared-services account: through cross-account roles with trust policies, using temporary credentials. Resource-based policies complement this for specific resources - a bucket or KMS key policy can grant another account access directly. So the consistent pattern across both questions is: authenticate by assuming a role (attached for workloads, or via a trust policy for cross-account), receive temporary credentials from STS, and never store long-lived keys - which gives secure, scoped, auto-rotating identity for applications and a clean, auditable mechanism for sharing access between accounts.
Chapter Revision Notes
- STS issues temporary, expiring credentials when a role is assumed - the engine behind the role model.
- A role has a trust policy (who may assume it) and permission policies (what it can do) - controlled independently.
- Workloads get roles via instance profiles, function roles, or EKS service-account associations - credentials obtained and refreshed automatically, no stored keys.
- Federation (SSO → assume role) gives people temporary credentials with no IAM users; cross-account access uses a trust policy allowing another account to assume a role.
- Temporary credentials bound the damage of a leak (they expire) and remove durable secrets - the core security win.