Skip to main content

Posts

Showing posts with the label Amazon Web Services

List all AWS IAM roles with their last used date

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. import boto3 import time from tabulate import tabulate Once you've imported the boto3, time and tabulate module, let's setup the AWS session using the AWS config profile and region name. session = boto3 . Session ( profile_name = profile , region_name = region ) iam_client = session . client ( 'iam' ) # use paginator if you have long list of IAM roles paginator = iam_client .get_paginator( 'list_roles' ) iterator = paginator .paginate() The following lines will help to setup the header row of the table in the output. In

Python - Getting AWS account ID

Here is the Python script that helps to find out the AWS account id. # The script helps you to find out AWS account number import boto3 # Configure AWS access key and secret access key to access AWS resources access_key_id = "test-key" secret_access_key_id = "secret-key" sts = boto3 . client (     "sts" , aws_access_key_id = access_key_id , aws_secret_access_key = secret_access_key_id , ) account_id = sts .get_caller_identity()[ "Account" ] # Print account number print ( account_id ) # The following code helps you to return AWS account number when IAM role based permissions are used account_id = boto3 . client ( "sts" ).get_caller_identity()[ "Account" ] # Print account number print ( account_id ) 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 da