Day 18 –101 Days of DevOps — Rotating IAM Keys using Boto3

import boto3
from datetime import datetime, timezone
client = boto3.client("iam")
paginator = client.get_paginator('list_users')
current_date=datetime.now(timezone.utc)
max_key_age=5
for response in paginator.paginate():
for user in response['Users']:
username = user['UserName']
accesskey_id = accesskey['AccessKeyId']
key_creation_date = accesskey['CreateDate']
age = (current_date - key_creation_date).days

if age > max_key_age:
print("Deactivating Key for the following users: " + username)
client.update_access_key(UserName=username, AccessKeyId=accesskey_id, Status='Inactive')

--

--

--

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Reducing Technical Debt with Data-Driven Analysis

🎯 Dart (DartLang) Introduction: if/else conditional statement

Flink Forward Berlin 2017 — An overview

What is Identity Index?

The Christmas Sprint Retrospective

Complete YAML Course

Best of the Week — May 31/June 13

NullBooleanField is dead

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prashant Lakhera

Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

More from Medium

DevOps: What it is and Why It Matters

Learn VPC in Alibaba Cloud

Kubernetes Multiple Watches Using Threads in Python

Git Rebase — Behind the scenes