Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find role being used on server from AWS CLI

I'm on an EC2 instance that has an IAM role attached to it, and would like to be able to verify that I am indeed using this role from the AWS CLI.

I'm imagining being able to call something like this (but can't find anything like it in the CLI docs):

$ aws get-current-role-details 

Does this functionality exist?

like image 860
ryantuck Avatar asked Nov 15 '17 17:11

ryantuck


People also ask

How do you check where IAM role is being used?

To view role-last-used information in the IAM Console, select Roles in the IAM navigation pane, then look for the Last activity column (see Figure 1 below). This displays the number of days that have passed since each role made an AWS service request. AWS records last-used information for the trailing 400 days.

How do I see what roles are assigned to AWS?

Under the AWS Management Console section, choose the role you want to view. On the Selected role page, under Manage users and groups for this role, you can view the users and groups assigned to the role.

How do I find my EC2 instance role?

Open the Amazon EC2 console, and then choose Instances. Choose the instance that you want to attach an IAM role to. Check the IAM role under the Details pane to confirm if an IAM role is attached to the Amazon EC2 instance.

How do I get Arn role in AWS?

AWS service console: Go to the relevant AWS service console, locate the resource and find the ARN in the details for the resource.


1 Answers

Use the AWS STS command get-caller-identity.

Returns details about the IAM identity whose credentials are used to call the API.

$ aws sts get-caller-identity {     "UserId": "AIDAxxx",     "Account": "xxx",     "Arn": "arn:aws:iam::xxx:user/Tyrone321" } 

You can then take the role name, and query IAM for the role details using both iam list-role-policies for inline policies and iam-list-attached-role-policies for attached managed policies (thanks to @Dimitry K for the callout).

$ aws iam list-attached-role-policies --role-name Tyrone321 {   "AttachedPolicies": [   {     "PolicyName": "SomePolicy",     "PolicyArn": "arn:aws:iam::aws:policy/xxx"   },   {     "PolicyName": "AnotherPolicy",     "PolicyArn": "arn:aws:iam::aws:policy/xxx"   } ] } 

To get the actual IAM permissions, use aws iam get-policy to get the default policy version ID, and then aws iam get-policy-version with the version ID to retrieve the actual policy statements. If the IAM principal is a user, the commands are aws iam list-attached-user-policies and aws iam get-user-policy. See the AWS IAM CLI reference for more information.

like image 114
Tyrone321 Avatar answered Oct 14 '22 03:10

Tyrone321