What is exactly Assume a role in AWS?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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:
Related reading
- What is hyperkube?
- What is meant by Security Groups are stateful?
- What is partition key in AWS Kinesis all about?
- What is Spring Cloud Config Server consistent model?
- What is Sid attribute use for in key policies?
- What is the aws command to verify my login credentials are correct? AKA whoami for aws-cli
- What is the best way to check if table exists in DynamoDB?
- What is the best way to pass AWS credentials to a Docker container?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.