First-class support for AssumeRole in sessions
See original GitHub issueThis is similar to my request for https://github.com/boto/boto/issues/3381, but for the botocore credentials/session system.
When I first looked for this, I got my hopes up because I saw the AssumeRoleProvider in credentials.py, but then it turned out to be fairly awkward to use programmatically with dynamically specified role metadata, as it seemed to assume fairly deeply that you wanted to use it the way aws-cli does, via a config file and static credentials.
What I’d really like to see is composable/fully programmatic solution to this. It would probably use much of the same logic that’s already in AssumeRoleProvider, except with fewer assumptions about where the AssumeRole metadata information is coming from, and an API that makes it easy to create new assumed sessions from existing ones.
For example, here’s an API I might enjoy using:
# Gives me some default session using default credentials that have power
# to AssumeRole into other accounts
session = botocore.session.get_session()
session1 = session.assume_role_session('arn:aws:iam::1234567890:role/JumpRole',
role_session_name='hostile-takeover')
session2 = session.assume_role_session('arn:aws:iam::1111111111:role/SomeOtherJumpRole',
role_session_name='hostile-takeover')
# arn:aws:iam::1234567890:role/JumpRole has the power to itself assume a role
session3 = session1.assume_role_session('arn:aws:iam::222222222:role/JumpRole',
role_session_name='hostile-takeover')
# Now we wait for a few hours
time.sleep(3600 * 5)
client3 = session3.create_client('ec2', region_name='us-west-2')
# This should work, and transparently refresh credentials as needed up the
# stack (in this case, two credential refreshes would be needed since we're
# two AssumeRoles deep)
print client3.describe_instances()
I don’t really care much about the API specifics, but I do want the entire AssumeRole session information to be (at least optionally) programmatic, and not implicitly loaded from some config file. The current AssumeRoleProvider also expects a source_profile which makes it hard to stack these things as I show above. Ideally, this would also work nicely with other AssumeRole variants, but that’s far less pressing for me.
cc @jamesls who I think wrote (or at least ported) AssumeRoleProvider in botocore.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:30
- Comments:24 (10 by maintainers)

Top Related StackOverflow Question
FWIW, the code required to assume role has simplified over time. Here’s an example from one of my apps:
+1 for this.