HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to see when an AWS IAM role was last used?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
iamlasthowusedseeroleawswhenwas

Problem

In AWS in the GUI you can select a role and then click on delete, then when you are prompted to approve or deny the deletion it shows you when the role was last used.

I would like to be able to know when a role was last used without pretend deleting it via the GUI.Ideally if I could get that data from the AWS CLI or a Boto3 script that would be great. Is there a way to accomplish that?

Solution

Amazon CloudTrail tracks all API use

In CloudTrail create a new trail

You can configure each trail to send log events to CloudWatch: Edit the trail and choose to send Logs to CloudWatch. It will offer you to create a Role for this

https://docs.aws.amazon.com/awscloudtrail/latest/userguide/monitor-cloudtrail-log-files-with-cloudwatch-logs.html

Then, run some calls on your role and wait 5 minutes.

In Cloudwatch, go to your log trail and search for "AssumedRole". Your events will be shown here

There is also documentation on searching Cloudwatch logs with filters through the api https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SearchDataFilterPattern.html

Context

StackExchange DevOps Q#5848, answer score: 5

Revisions (0)

No revisions yet.