On the AWS console, I can switch between different roles (see screenshot).
I am using a Docker Image where I am running Linux. I have also a credentials file with temporary AWS credentials. I can start the docker container setting the AWS_PROFILE to one of the roles in my credentials file. Then, I would like to "switch" the role to a different one defined in IAM.
How can I do this? Is this possible?
Thanks!
2
Answers
You can assume the role. Using the CLI it would look like:
The output of the command contains an access key, secret key, and session token that you can use to authenticate to AWS:
Now you can set your environment variables to those outputs and you’ll be using the new role.
In Python it would look something like this:
You can store an IAM Role as a profile in the AWS CLI and it will automatically assume the role for you.
Here is an example from Using an IAM role in the AWS CLI – AWS Command Line Interface:
This is saying:
--profile marketingadmin
user1
AssumeRole
on the specified roleThis means you can simply call a command like this and it will assume the role and use the returned credentials automatically:
See also: AWS sts assume role in one command