How do I assume an IAM role using the AWS CLI

0 votes
Can you show me how to assume an IAM role through the AWS CLI? I'm trying to figure it out?
Oct 23, 2024 in AWS by Priyanka
• 4,380 points
1,750 views

1 answer to this question.

0 votes

I was trying to assume an IAM role with the AWS CLI, but I missed setting it up right in the prerequisites. Make sure to check that first, then move forward.

  • AWS CLI Setup: Ensure the AWS CLI is installed and configured with your credentials.

  • IAM Role Configuration: The role must exist with a trust policy allowing your user to assume it and appropriate permissions attached to it.

  • Permission Checks:  Your IAM user or group must have sts: AssumeRole permission, and the role must have the necessary policies for the actions you need to perform.

Follow these steps to Assume an IAM Role:

  • To create your IAM policy, run the command and Make sure to replace policy.json with the path to your policy document. image

  • Find the ARN of the IAM role you want to use. It looks like this:

    image

  • Run this aws sts assume-role command to assume the role:

         image

         image

Replace <account-id> and <role-name> with your role's details and <session-name> with a name for your session (this can be any identifier).

  • The command returns JSON with AccessKeyId, SecretAccessKey, and SessionToken.

   image

  • Set Environment Variables (Optional): To use the temporary credentials, you can export them as environment variables:

    image

  • To verify that you've assumed the role successfully, you can run.

      image

answered Oct 26, 2024 by anonymous

Related Questions In AWS

0 votes
2 answers

How do I define the principal for an AWS policy statement?

Check with AWS Policy Documentations once, rest ...READ MORE

answered Jun 26, 2018 in AWS by Cloud gunner
• 4,670 points

edited Jun 26, 2018 by Cloud gunner 2,903 views
0 votes
1 answer

How do I create an alarm for my load balancer using the Amazon EC2 console?

Follow these steps: Open the Amazon EC2 console On ...READ MORE

answered May 17, 2019 in AWS by Rhea
2,839 views
0 votes
1 answer

How do I change the Root Volume to persist at launch time using the CLI?

add the following option to your run-instances command: --block-device-mappings file://mapping.json Specify ...READ MORE

answered Oct 21, 2019 in AWS by Pearl
1,772 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
9,010 views
+2 votes
1 answer
0 votes
1 answer

What does AWS STS get caller identity do?

Let me tell you what AWS STS ...READ MORE

answered Oct 26, 2024 in AWS by anonymous
2,029 views
0 votes
1 answer

What are the differences between AWS CloudHSM and AWS KMS?

AWS CloudHSM and AWS KMS are both ...READ MORE

answered Oct 26, 2024 in AWS by anonymous
1,374 views