Sign in
agent:

List all AWS IAM users and retrieve their last used date for passwords and access keys.

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

Lists all AWS IAM users and retrieves their last used date for passwords and access keys, handling timezone differences.

import boto3 from datetime import datetime, timezone import json # Initialize boto3 client for IAM client = boto3.client( 'iam', aws_access_key_id=getEnvVar('AWS_ACCESS_KEY_ID'), aws_secret_access_key=getEnvVar('AWS_SECRET_ACCESS_KEY'), region_name='us-east-2' ) # Get all IAM users users = client.list_users()['Users'] users_last_used_info = [] for user in users: user_info = {} user_name = user['UserName'] password_last_used = user.get('PasswordLastUsed') if password_last_used: password_last_used = password_last_used.replace(tzinfo=timezone.utc) # Check access keys access_keys = client.list_access_keys(UserName=user_name)['AccessKeyMetadata'] last_used_date = None for access_key in access_keys: access_key_id = access_key['AccessKeyId'] last_used_info = client.get_access_key_last_used(AccessKeyId=access_key_id) last_used_date = last_used_info['AccessKeyLastUsed'].get('LastUsedDate') if last_used_date: last_used_date = last_used_date.replace(tzinfo=timezone.utc) user_info['UserName'] = user_name user_info['PasswordLastUsed'] = str(password_last_used) if password_last_used else "Never" user_info['AccessKeyLastUsed'] = str(last_used_date) if last_used_date else "Never" users_last_used_info.append(user_info) print(json.dumps(users_last_used_info, indent=4, default=str))
copied