Skip to content

AgentGino/aws-profiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AWS Profiler

License: MIT Python Version

A command-line tool to list all AWS profiles, check their credential status, and refresh IAM user access keys.

Features

  • πŸ“‹ Lists all AWS profiles from ~/.aws/credentials and ~/.aws/config
  • πŸ” Retrieves account information for each profile
  • βœ… Checks if credentials are active or expired
  • ⏱️ Shows credential age and expiration time
  • πŸ”„ Automatically refreshes IAM user access keys
  • πŸ’Ύ Backs up old credentials before rotation
  • πŸ—‘οΈ Optionally deletes old keys from AWS
  • πŸ“Š Displays results in a formatted table
  • πŸ“ˆ Provides summary statistics

Prerequisites

  • Python 3.8, 3.9, 3.10, 3.11, or 3.12
  • AWS CLI configured (v2 recommended for SSO support)
  • Required Python packages:
    • boto3 >= 1.26.0
    • tabulate >= 0.9.0
    • python-dateutil

Installation

pip install aws-profiler

Usage

List all profiles and check status

aws-profiler

The tool will scan your AWS configuration files and display a table with:

  • Profile name
  • Account ID
  • User/Role name
  • Credential type (User/Role)
  • Status (Active/Expired/Error)
  • Credential age (how old the credentials are)
  • Expiration time (for temporary credentials)

Refresh specific profile

Refresh credentials for a specific IAM user or SSO profile:

aws-profiler --refresh myprofile

For IAM Users, this will:

  1. Verify the profile is an IAM user (not a role)
  2. Create a new access key
  3. Backup the old credentials to ~/.aws/backups/
  4. Update the credentials file with the new key
  5. Keep the old key active in AWS (unless --delete is used)

For SSO Profiles, this will:

  1. Detect the profile is SSO-configured
  2. Run aws sso login --profile <name>
  3. Open your browser for authentication
  4. Complete the SSO flow through AWS CLI

Refresh and delete old key (IAM users only)

To also delete the old access key from AWS after creating the new one:

aws-profiler --refresh myprofile --delete

⚠️ Warning: This will permanently delete the old access key from AWS. You'll be prompted for confirmation.

Refresh all eligible profiles

Automatically refresh credentials for ALL IAM users and SSO profiles:

aws-profiler --refresh --all

This will:

  1. Scan all profiles and identify eligible ones (IAM users and SSO)
  2. Show summary of what will be refreshed
  3. Ask for confirmation
  4. Refresh all IAM user keys (with backups)
  5. Trigger SSO login for all SSO profiles

To also delete old keys from AWS for all IAM users:

aws-profiler --refresh --all --delete

Example Output

Listing profiles

πŸ” AWS Profile Status Checker
================================================================================

πŸ“‹ Found 3 profile(s)

   Checking default... [βœ“ Active]
   Checking staging... [βœ“ Active]
   Checking production... [βœ— Expired]

╒═════════════╀══════════════╀════════════╀════════╀═══════════╀═══════╀════════════╕
β”‚ Profile     β”‚ Account ID   β”‚ User/Role  β”‚ Type   β”‚ Status    β”‚ Age   β”‚ Expires In β”‚
β•žβ•β•β•β•β•β•β•β•β•β•β•β•β•β•ͺ══════════════β•ͺ════════════β•ͺ════════β•ͺ═══════════β•ͺ═══════β•ͺ════════════║
β”‚ default     β”‚ 123456789012 β”‚ admin      β”‚ User   β”‚ βœ“ Active  β”‚ 15d 3hβ”‚ Permanent  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ staging     β”‚ 234567890123 β”‚ dev-role   β”‚ Role   β”‚ βœ“ Active  β”‚ 2h    β”‚ 10h 45m    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ production  β”‚ N/A          β”‚ N/A        β”‚ N/A    β”‚ βœ— Expired β”‚ N/A   β”‚ Expired    β”‚
β•˜β•β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•§β•β•β•β•β•β•β•β•β•β•β•β•β•›

πŸ“Š Summary: βœ“ 2 active  |  βœ— 1 expired  |  ⚠ 0 error/no credentials

Refreshing a single IAM user

πŸ”„ AWS Credential Refresh
================================================================================

πŸ”‘ Refreshing credentials for profile: myprofile

βœ… Success!
βœ“ Credentials refreshed successfully for profile "myprofile"
  New Key: AKIAIOSFODNN7EXAMPLE
  Backup: /Users/username/.aws/backups/credentials_backup_myprofile_DEXAMPLE_20251124_143022
  Old key AKIAIOSFODNN6EXAMPLE is still active in AWS. Use --delete to remove it.

Refreshing an SSO profile

πŸ”„ AWS Credential Refresh
================================================================================

πŸ”‘ Refreshing credentials for profile: sso-dev
πŸ” Initiating SSO login for profile: sso-dev
   Please follow the instructions in your browser...

Attempting to automatically open the SSO authorization page in your default browser.
If the browser does not open or you wish to use a different device to authorize this request, open the following URL:

https://device.sso.us-east-1.amazonaws.com/

Then enter the code: ABCD-EFGH

βœ… Success!
βœ“ SSO login successful for profile "sso-dev"

Refreshing all profiles

πŸ”„ AWS Credential Refresh - ALL PROFILES
================================================================================

πŸ“‹ Analyzing profiles...

   IAM Users (can refresh):    2
   SSO Profiles (can refresh): 1
   Roles (cannot refresh):     1
   Inactive/Error profiles:    0

   Will refresh 2 IAM user(s) and 1 SSO profile(s)
   Type 'yes' to continue: yes

================================================================================

πŸ”‘ Refreshing IAM User Profiles:

   β†’ dev-user...
      βœ“ Success

   β†’ prod-user...
      βœ“ Success

πŸ” Refreshing SSO Profiles:

   β†’ sso-dev...
      [SSO login flow continues...]
      βœ“ Success

================================================================================

πŸ“Š Summary: βœ“ 3 refreshed  |  βœ— 0 failed

Status Values

  • βœ“ Active: Credentials are valid and working
  • βœ— Expired: Token or credentials have expired
  • ⚠ No Creds: Profile exists but no credentials are configured
  • βœ— Invalid: Other authentication or authorization errors

Credential Types

  • User: IAM user with permanent access keys (can be refreshed)
  • Role: Assumed role with temporary credentials (cannot be refreshed)

Backup Files

When refreshing credentials, the old credentials are automatically backed up to:

~/.aws/backups/credentials_backup_<profile>_<key-suffix>_<timestamp>

Backup files are created with restricted permissions (600) for security.

IAM Permissions Required for Refresh

To use the refresh functionality, your IAM user needs these permissions:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "iam:CreateAccessKey",
        "iam:ListAccessKeys",
        "iam:DeleteAccessKey"
      ],
      "Resource": "arn:aws:iam::*:user/${aws:username}"
    }
  ]
}

Troubleshooting

"No credentials found" error

Ensure AWS credentials exist in ~/.aws/credentials or set environment variables:

export AWS_ACCESS_KEY_ID=your_key
export AWS_SECRET_ACCESS_KEY=your_secret

"Access Denied" when refreshing

Verify your IAM user has the required permissions listed above. Check your IAM policy allows iam:CreateAccessKey and iam:ListAccessKeys.

SSO login fails

  • Ensure AWS CLI v2 is installed: aws --version
  • Verify SSO configuration in ~/.aws/config:
    [profile sso-profile]
    sso_start_url = https://your-domain.awsapps.com/start
    sso_region = us-east-1
    sso_account_id = 123456789012
    sso_role_name = YourRoleName
  • Clear cached SSO tokens: rm -rf ~/.aws/sso/cache/

"Maximum number of access keys exceeded"

AWS limits IAM users to 2 access keys. Delete an old key manually or use the --delete flag when refreshing.

Backup directory permission errors

Ensure ~/.aws/backups/ directory exists and is writable. The tool will attempt to create it automatically with 700 permissions.

Security Considerations

  • ⚠️ Backup files contain plaintext credentials - secure these files appropriately
  • πŸ”’ Backup files are created with 600 permissions (owner read/write only)
  • πŸ—οΈ Old access keys remain in AWS backups - consider rotating or deleting them
  • πŸ” For production workloads, consider using AWS Secrets Manager or Systems Manager Parameter Store
  • πŸ›‘οΈ Regularly audit and rotate your access keys
  • πŸ“ Use IAM roles with temporary credentials when possible instead of long-term keys

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

A command-line tool to list all AWS profiles, check their credential status, and refresh IAM user access keys.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages