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?
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.
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.
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.
AWS service console: Go to the relevant AWS service console, locate the resource and find the ARN in the details for the resource.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With