How to assume an AWS role from another AWS role?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Assuming an AWS role from another AWS role involves temporary access to resources across accounts by leveraging AWS Identity and Access Management (IAM). This capability is crucial for organizations that utilize multiple AWS accounts and need to manage cross-account permissions effectively. We will explore the necessary steps, configurations, best practices, and potential pitfalls in assuming roles across AWS accounts, with technical explanations and examples.
Understanding AWS Roles and Trust Relationships
AWS roles are used to delegate access to resources without using a long-term security credential. Roles are intra-account, but they can also be configured to allow access from other accounts.
Trust Relationship
A trust relationship defines which AWS accounts can assume the role. Each role contains a policy document known as a "trust policy," which specifies the trusted entities and conditions needed to assume the role.
Session Duration
Upon assuming a role, AWS provides temporary security credentials valid for a specific duration. By default, session duration is set to one hour and can be adjusted between 15 minutes and 12 hours.
Step-by-Step: Assuming a Role from Another AWS Role
1. Preparing the Source Role
Assuming an AWS role requires three things:
- Source Role: The existing role from which you are attempting to assume another role.
- Target Role: The role you want to assume.
- Trust Policy: Policy that must allow the source role to assume the target role.
2. Configuring the Target Role
Create or modify the trust relationship policy of the target role. This sets up the permission for a role from another AWS account to assume the target role.
3. Assuming the Role Programmatically
Using the AWS SDK or AWS CLI, you can assume the target role by calling the sts:AssumeRole API action. Below is an example using the AWS CLI:
This command returns temporary security credentials (Access Key, Secret Key, and Session Token).
4. Using Temporary Credentials
Once you receive temporary security credentials, configure them in your AWS CLI environment to access resources:
5. Cleanup and Best Practices
- Monitor Role Usage: Use AWS CloudTrail to monitor the roles being assumed and their usage across accounts.
- Rotate External IDs: Regularly change external IDs for enhancing security.
- Session Duration: Limit session duration based on necessity to reduce risk exposure.
- Scoped Permissions: Principle of least privilege by tightening role policies only to necessary actions and resources.
Key Points Table
| Feature | Description |
| Trust Relationship | Determines which roles/accounts can assume a given role |
| AssumeRole API | AWS API for assuming roles and obtaining temporary credentials |
| External ID | A unique identifier to enhance security in cross-account access |
| Temporary Credentials | Time-bound credentials issued after successfully assuming a role |
| Session Duration | Validity period of the assumed role session, default to 1 hour |
| Monitoring | Use CloudTrail for auditing and accountability |
Conclusion
Cross-account role assumption in AWS enhances flexibility and security postures for complex cloud architectures. By understanding and applying trust policies, secure identifications such as External IDs, and diligently monitoring, organizations can leverage cross-account access efficiently. Always adhere to best practices to ensure secure and manageable role assumptions, adapting configurations as necessary for your organization's security and operational needs.
Related reading
- How to attach multiple IAM policies to IAM roles using Terraform?
- How to auto-scale Kubernetes Pods based on number of tasks in celery task queue?
- How to auto scale Amazon DynamoDB throughput?
- How to automatically scale up and scale down of micro services instances built using Spring Boot and Spring cloud?
- How to avoid having to enter image version in deployment yaml and use most recent image from azure container registry
- How to best run Apache Airflow tasks on a Kubernetes cluster?
- How to bootstrap installation of Python modules on Amazon EMR?
- How to build an image classification dataset in Azure?

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.