AWS
Assume Role
IAM
Cloud Security
Identity Management

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.

Practice system design

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

  1. 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.
  2. Assume Role:
    • From a trusted entity, call the sts:AssumeRole API, 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.
  3. 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 .

  1. In Account B , create a role named CrossAccountAccessRole with necessary permissions and specify Account A as the trusted entity in the trust policy.
  2. A user in Account A calls sts:AssumeRole with the ARN of CrossAccountAccessRole .
  3. The user receives temporary credentials, allowing them to access resources in Account B as 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

FeatureDescription
SecurityTemporary credentials reduce risk by eliminating long-term exposure.
Cross-AccountAssume a role from another account to access its resources.
Session DurationTemporary credentials can last between 15 minutes to a maximum of 12 hours.
Role ChainingAllows consecutive role assumptions, but limits session time windows and increases complexity.
Identity FederationAllows 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.