What is exactly Assume a role in AWS?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Amazon Web Services (AWS), the concept of "assuming a role" is a core feature of AWS Identity and Access Management (IAM). It allows for seamless and secure sharing of access across different AWS accounts, or even within the same account, without the need for sharing long-term credentials. Understanding how to assume a role effectively can significantly enhance the security and manageability of your AWS resources.
Understanding AWS IAM Roles
What is an IAM Role?
An IAM Role in AWS is a set of permissions that define what actions are allowed and denied by an entity in the AWS environment. Unlike an IAM user, a role doesn't have any associated long-term credentials like a password or access keys. Instead, when a role is assumed, AWS dynamically provides temporary security credentials which grant the defined permissions for the duration of the session.
Benefits of Using IAM Roles
- Security: Avoid sharing long-term credentials and reduce risk exposure.
- Cross-Account Access: Facilitate secure collaboration across different AWS accounts.
- Resource Access: Grant applications or services temporary access to resources within AWS.
How to Assume a Role in AWS
Assuming a role involves using the sts:AssumeRole
API call, which returns a set of temporary security credentials (Access Key ID, Secret Access Key, and Session Token) for the assumed role.
Step-by-Step Process
- Role Creation:
- Define a role in IAM with a specific policy that grants the required permissions.
- Specify a trusted entity, which can be other AWS accounts, services, or another IAM role.
- Assume Role:
- From a trusted entity, call the
sts:AssumeRoleAPI, specifying the role ARN (Amazon Resource Name). - AWS returns temporary credentials that the entity can use to perform actions defined by the role's policy.
- Use Temporary Credentials:
- Access AWS services using these credentials.
- These credentials are valid for a specified duration, after which they expire.
Example: Cross-Account Access
Suppose an organization has two AWS accounts, Account A
and Account B
. Users from Account A
need specific access to resources in Account B
.
- In
Account B, create a role namedCrossAccountAccessRolewith necessary permissions and specifyAccount Aas the trusted entity in the trust policy. - A user in
Account Acallssts:AssumeRolewith the ARN ofCrossAccountAccessRole. - The user receives temporary credentials, allowing them to access resources in
Account Bas specified by the role.
Technical Considerations
- Session Duration: Default session duration is 1 hour, adjustable up to a maximum of 12 hours.
- Role Chaining: AWS allows a role to assume another role (chaining), but this can limit session duration.
- Session Policies: Optionally, you can further restrict permissions during an assumed role session by passing a session policy.
Key Points Summary
| Feature | Description |
| Security | Temporary credentials reduce risk by eliminating long-term exposure. |
| Cross-Account | Assume a role from another account to access its resources. |
| Session Duration | Temporary credentials can last between 15 minutes to a maximum of 12 hours. |
| Role Chaining | Allows consecutive role assumptions, but limits session time windows and increases complexity. |
| Identity Federation | Allows users outside AWS (corporate identities, etc.) to access AWS resources through roles. |
Additional Concepts
Automating Role Assumption
For developers, assuming a role programmatically is a common requirement. AWS SDKs (e.g., Boto3 for Python) simplify this process. Example in Python:

