• DevOps
    Case Study

    How we built a resilient multi-account, multi-cloud solution for a Health Tech service provider!

    READ CASESTUDY
    icon

    24/7 DevOps as a Service

    Round-the-clock DevOps for uninterrupted efficiency.

    icon

    Infrastructure as a Code

    Crafting infrastructure with ingenious code.

    icon

    CI/CD Pipeline

    Automated CI/CD pipeline for seamless deployments.

    icon

    DevSecOps

    Integrated security in continuous DevOps practices.

    icon

    Hire DevOps Engineers

    Level up your team with DevOps visionaries.

    icon

    Consulting Services

    Navigate success with expert DevOps consulting.

  • TechOps
    Case Study

    How we built a scalable Odoo solution for a Travel Tech service provider!

    READ CASESTUDY

    WEB HOSTING SUPPORT

    icon

    HelpDesk Support

    Highly skilled 24/7 HelpDesk Support

    icon

    Product Support

    Boost your product support with our expertise.

    MANAGED SERVICES

    icon

    Server Management

    Don’t let server issues slow you down. Let us manage them for you.

    icon

    Server Monitoring

    Safeguard your server health with our comprehensive monitoring solutions.

    STAFF AUGMENTATION

    icon

    Hire an Admin

    Transform your business operations with our expert administrative support.

    icon

    Hire a Team

    Augment your workforce with highly skilled professionals from our diverse talent pool.

  • CloudOps
    Case Study

    How we helped a Private Deemed University in India, save US $3500/m on hosting charges!

    READ CASESTUDY
    icon

    AWS Well Architected Review

    Round-the-clock for uninterrupted efficiency

    icon

    Optimize

    Efficient CloudOps mastery for seamless cloud management

    icon

    Manage

    Automated CI/CD pipeline for seamless deployments

    icon

    Migrate

    Upgrade the journey, Migrate & Modernize seamlessly

    icon

    Modernize

    Simplify compliance complexities with our dedicated services

    icon

    FinOps as a Service

    FinOps as a Service

  • SecOps
    Case Study

    How we built a scalable Odoo solution for TravelTech service provider!

    READ CASESTUDY
    icon

    VAPT

    Vulnerability Assessment and Penetration Testing

    icon

    Source Code Review

    Ensuring source code security ans safe practices to reduce risks

    icon

    Security Consultation

    On demand services for improving server security

    icon

    System Hardening

    Reduced vulnerability and proactive protection

    icon

    Managed SoC

    Monitors and maintains system security. Quick response on incidents.

    icon

    Compliance as a Service

    Regulatory compliance, reduced risk

  • Insights
    Case Study

    How we helped a Private Deemed University in India, save US $3,500/m on hosting charges!

    READ CASESTUDY
    icon

    Blog

    Explore our latest articles and insights

    icon

    Case Studies

    Read about our client success stories

    icon

    Flipbook

    Explore our latest Flipbook

    icon

    Events

    Join us at upcoming events and conferences

    icon

    Webinars

    Watch our educational webinar series

  • Our Story
  • Contact Us

Interested to collaborate?

Get in touch with us!

Ready to elevate your business with certified cloud expertise? Contact us today to learn how our team can help you leverage cloud technology to drive growth, streamline operations, and enhance security.

  • AWSAWS
  • Azure CloudAzure Cloud
  • Google CloudGoogle Cloud
  • Akamai CloudAkamai Cloud
  • OVHOVH
  • Digital OceanDigital Ocean
  • HetznerHetzner
  • Kubernetes Consultancy Services
  • K8s & Cloud native Solutions
  • 24/7 Infrastructure Monitoring
  • DevOps as a Service
  • Cloud CI/CD Solutions
  • White Labeled MSP Support
  • Our story
  • Life@SupportSages
  • Insights
  • Careers
  • Events
  • Contact Us

Connect with us!


LinkedInFacebookXInstagramYouTube

aws partneraws advanced partner
SupportSages

Copyright © 2008 – 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy

Automating IAM user Audit Using Python.

Admin

  • 3 min read
Automating IAM user Audit Using Python.

Generating audio, please wait...

IAM user is an easy way of giving access to our colleagues or someone in our organization to get into our AWS account with limited or full privileges. We are all following the same IAM user method in many of our organizations and it is easy to maintain and add or revoke permissions with the help of AWS.

But there comes a scenario where the members relieved from the company get access to our AWS accounts right in their pocket. In order to solve this issue we need to conduct an IAM review over a frequent interval.

Let me say reviewing and Auditing for such things are pretty boring and time-consuming when it comes to AWS accounts with more users. This includes creating a list of users who haven't accessed the console for more than a particular amount of days and that should include details like whether they have console access or not, do they have active access keys, and if yes when they last accessed it, etc.

In order to overcome this time-consuming and boring process of creating an audit document for IAM I made a Python script that will do the job for us.

The script will do the following things.

  1. Communicate with AWS using Boto3.
  2. List all the IAM users.
  3. Check for their last accessed date based on the user input(minimum age of users).
  4. Check whether the user has console access or not.
  5. If they have console access the script will tell us that they last accessed the console N number of days before.
  6. Check whether they have active IAM Access keys.
  7. If they have active Access keys the script will tell us that they accessed the Access keys N number of days before.
  8. The script finally generates a document namely “IAM-Audit-Report.txt”.

The output of the script which looks for a minimum age of users above 100 will look like this

1_y4gPWT8mMAxVQFm95KS5PA (1).webp

The script is given below.

import boto3
from datetime import datetime

now = datetime.utcnow()

iam = boto3.client('iam')

list_of_users = iam.list_users()

day_threshold = int(input("Please enter the preferred min age of users: "))

file_name = open("IAM-Audit-Report.txt" , "w")
def consoleaccess(name):
if 'PasswordLastUsed' in name:
ConsoleAccess = True
C1 = "yes"
last_used = (now - (name['PasswordLastUsed']).replace(tzinfo=None)).days
else:
ConsoleAccess = False
C1 = "no"
last_used = 0 # Set to 0 if no console access

return C1, last_used

def accesskey(access_keys, user_name):
num_keys = len(access_keys)
if num_keys == 0:
num_keys_str = "0"
last_key_accessed = "N/A"
else:
num_keys_str = str(num_keys)
last_key_accessed = "N/A"
for a_key in access_keys:
access_key_id = a_key['AccessKeyId']
if a_key['Status'] == 'Active':
last_access_key_used = iam.get_access_key_last_used(AccessKeyId=access_key_id)
if 'LastUsedDate' in last_access_key_used['AccessKeyLastUsed']:
date_of_access = (last_access_key_used['AccessKeyLastUsed']['LastUsedDate']).replace(tzinfo=None)
days_of_access = (now - date_of_access).days
if last_key_accessed == "N/A" or days_of_access < last_key_accessed:
last_key_accessed = days_of_access
if last_key_accessed == "N/A":
last_key_accessed = "N/A"
else:
last_key_accessed = str(last_key_accessed)

return num_keys_str, last_key_accessed

for name in list_of_users['Users']:
user_name = name['UserName']
arn = name['Arn']
access_keys = iam.list_access_keys(UserName=user_name)['AccessKeyMetadata']
C1, last_used = consoleaccess(name)
num_keys_str, last_key_accessed = accesskey(access_keys, user_name)

if last_used > day_threshold:
file_name.write(f"User: {user_name}\nArn: {arn}\nConsole Access: {C1}\nLast Console Access: {last_used} days before\nNumber of Access Keys: {num_keys_str}\nLast Access Key Accessed: {last_key_accessed} or never used before\n\n")
print("Your report is ready")
file_name.close()

That’s all Thank you ❤

Discover streamlined AWS IAM auditing! Simplify the user access review process with our Python script, generating comprehensive reports effortlessly. Explore SupportSages for expert insights on enhancing AWS account security.

  • AWS
  • DevOps

Continue Your Journey With…

DevOps as a Service

DevOps as a Service

Let us do the heavy lifting for you

Promotional banner
Promotional banner
Automating IAM user Audit Using Python.

5 Things You Should Know About AWS Well-Architected Framework Review

5 Things You Should Know About AWS Well-Architected Framework Review
  • AWS
logo

Automating SSM Parameter store backup using Python.

Automating SSM Parameter store backup using Python.
  • AWS
  • DevOps
logo

Create new user account in Argo CD with Read Only Access

Create new user account in Argo CD with Read Only Access
  • AWS
  • DevOps
logo

Effortless S3 Bucket Access Log Activation Across Your AWS Account with Python Automation

Effortless S3 Bucket Access Log Activation Across Your AWS Account with Python Automation
  • AWS
  • DevOps
logo

Posts by Admin