Roles, STS and Federation

Topics Covered

Roles and the Security Token Service

What a role is

The Security Token Service

Session duration

Why roles beat access keys

Revoking role sessions

Source identity and session tags

Service roles and service-linked roles

Passing roles to services

Session policies

Both sides must allow cross-account calls

Regional STS endpoints

A note on the root user

The previous lesson showed how IAM decides whether a request is allowed. This lesson is about who makes the request and how they get credentials. The exam's answer to almost every credential question is the same: use a role and temporary credentials, not a long lived access key. Understanding how roles work, and the specific mechanism for each situation, lets you pick the right option quickly. For an engineering view of the same ideas, see STS, roles and workload identity in the AWS in Practice course.

What a role is

An IAM role is an identity with permissions policies but no permanent credentials. Nobody signs in as a role with a password, and a role has no access keys of its own. Instead, a trusted principal assumes the role and receives temporary security credentials: an access key ID, a secret access key and a session token, together with an expiry time. Requests signed with those credentials are evaluated against the role's permissions, as if the role had made them.

Every role has two kinds of policy. The trust policy says who may assume it: an AWS service such as EC2 or Lambda, an IAM user or role in the same or another account, a federated identity provider, or a combination. The permissions policies say what the role can do. Both must be right for the role to be useful. A role whose trust policy does not name the caller cannot be assumed, however generous its permissions policy.

The Security Token Service

AWS Security Token Service, or STS, issues temporary credentials. It has several API operations, and the exam expects you to recognise them. AssumeRole is used by an IAM user or role to assume another role, in the same or another account. AssumeRoleWithSAML is used after a sign-in through a SAML 2.0 identity provider. AssumeRoleWithWebIdentity is used with tokens from an OpenID Connect provider, such as the token a CI system issues to a pipeline run. GetSessionToken returns temporary credentials for an IAM user, typically to add MFA to API calls. GetFederationToken returns credentials for a federated user through a proxy application, an older pattern that is rarely the best answer today.

AssumeRole swaps an identity for short lived credentials.

Session duration

Temporary credentials expire. For AssumeRole, the default session lasts one hour, and a role can be configured with a maximum session duration between one and twelve hours; the caller may request any duration up to that maximum. When one assumed role uses its credentials to assume another role, called role chaining, the session is limited to one hour regardless of the maximum. When credentials expire, the caller assumes the role again. For services such as EC2 and Lambda, AWS and the SDKs handle that refresh automatically, so your code never sees an expired credential.

Short sessions are a security feature. If credentials leak, they stop working soon. Compare that with an access key, which works until someone notices and deletes it.

Why roles beat access keys

Long lived access keys are the most common source of AWS security incidents. They are copied into code, configuration files, CI settings and laptops, and they leak through public repositories, logs and backups. Rotating them requires coordination, so in practice they are rarely rotated. Roles remove the secret entirely: credentials are issued on demand, scoped to the role and short lived. When an exam question describes credentials embedded in code or configuration, the answer almost always replaces them with a role suited to the compute service in use.

Revoking role sessions

Because temporary credentials remain valid until they expire, revoking a leaked session needs a specific action. In the IAM console, revoke active sessions attaches an inline policy to the role that denies every action for sessions issued before the current time, using the aws:TokenIssueTime condition key. Sessions created after that time work normally. Deleting the role also stops its sessions, but that breaks every legitimate user too. For a leaked access key, deactivate and delete the key, then investigate in CloudTrail what it was used for.

Source identity and session tags

When many people or processes assume the same role, CloudTrail shows the role session name, which the caller chooses. To keep track of who really acted, a caller can set a source identity when assuming the role. It is recorded in CloudTrail and carried through role chaining, and the role's trust policy can require it. Session tags are key value pairs passed when assuming a role, which can be used in attribute based access control policies, for example to tag each session with the user's department. Both are useful in audit oriented exam scenarios.

Service roles and service-linked roles

Many services assume roles to act on your behalf. A service role is created by you and trusted by the service, such as the role AWS CodeBuild uses to fetch source and push images, or the role AWS Backup uses to take snapshots. A service-linked role is created and managed by the service itself with a fixed set of permissions, such as the role Elastic Load Balancing uses to manage network interfaces. You can view a service-linked role but not change its permissions. When a scenario asks why a service cannot perform an action, the cause is often a missing permission in the service role, and the fix is to update that role, not to create an IAM user for the service.

Passing roles to services

To let a service use a role, the person configuring the service needs permission to pass the role, through the iam:PassRole action. For example, a developer launching an EC2 instance with an instance profile needs iam:PassRole on that role. This prevents privilege escalation: without the PassRole restriction, anyone who could launch instances could attach an administrator role to an instance and use it. Policies normally limit PassRole to specific role ARNs and, with the iam:PassedToService condition key, to specific services. When a scenario says a developer can create Lambda functions but gets an error when choosing an execution role, a missing iam:PassRole permission is the likely cause.

Session policies

When a principal assumes a role, it can pass a session policy with the request. The session's effective permissions are then the intersection of the role's permissions policies and the session policy: the session policy can only narrow what the role allows, never widen it. This is useful when one role serves many callers and each caller should get only a slice of it. A broker application might assume a broad data access role but pass a session policy naming a single bucket prefix for each user it acts for. Session policies are also used by federation, because the credentials of a federated user are scoped the same way. On the exam, if a scenario asks how to give a temporary session less access than the role has, without creating another role, a session policy is the answer.

Both sides must allow cross-account calls

Within one account, either the trust policy or the caller's identity policy can be enough for some actions, but assuming a role in another account always needs two permissions. The target role's trust policy must name the caller, and the caller's own identity policy must allow sts:AssumeRole on the target role's ARN. If either is missing, the call is denied. Service control policies in either account can also deny it. When a cross-account assume fails, check the trust policy first, the caller's policy second and SCPs third.

Regional STS endpoints

STS has a global endpoint and regional endpoints in each Region. AWS recommends using regional endpoints, which reduce latency, keep working if another Region has a problem and issue session tokens that are valid in all Regions. The SDKs now default to regional endpoints. You will rarely need this detail, but it appears occasionally in questions about resilience of authentication.

A note on the root user

The root user of an account is not a role and cannot be restricted by IAM policies in that account. It should have MFA enabled, no access keys and should be used only for the handful of tasks that require it, such as changing the account's support plan or closing the account. In AWS Organizations, member accounts can have root credentials removed entirely and managed centrally. Any exam option that uses the root user for routine work, or creates root access keys, is wrong.

Role chaining is capped at one hour

When a role session is used to assume another role, the new session can last at most one hour, regardless of the role's configured maximum. Long running jobs that chain roles must refresh credentials.