The following Python script will help to list all the AWS IAM roles with the last used date. If the role is not been used, it will show 'Never used" instead of date. You will require Python3.8 or above to run the script.
I prefer to use Tabulate to format the output in to table format. You can format the output in to HTML or even convert in to CSV file too.
Let's start the script to list all IAM role and its last used date.
Once you've imported the boto3, time and tabulate module, let's setup the AWS session using the AWS config profile and region name.
The following lines will help to setup the header row of the table in the output. In this example, I am only printing IAM role name and last used date. You can add other attributes like Role Id, Arn, created date, AssumeRolePolicyDocument and action.
Here is the main block of code where script will run in to loops to get the Roles information. From the Roles information, it will extract the RoleName. Once you've role name, it will fetch the role information like Role Id, Arn, created date, AssumeRolePolicyDocument, action and last used date.
You can format the way date and time shown in the output by modifying the values in .strftime("%d/%m/%Y %H:%M:%S").
Hope you find it useful.
Disclaimer: www.TechieTalks.co.uk does not conceal the possibility of error and shortcomings due to human or technical factors. www.TechieTalks.co.uk does not bear responsibility upon any loss or damage arising from conduct or activities related to the use of data and information contained in this blog.
Comments
Post a Comment