AWS CLI: List All IAM Users in an Account

To list all IAM users in an AWS account from the command line, run aws iam list-users. Add --query to pick the fields you want and --output table to make it readable. You need the AWS CLI installed and signed in (on Windows or on Ubuntu) with permission to read IAM.

List the Users

PowerShell
# every user, with the name, ARN and creation date, as a table
aws iam list-users --query "Users[*].{Name:UserName, ARN:Arn, Created:CreateDate}" --output table
aws iam list-users with --query returning user names, ARNs and creation dates

Leave off --output table to get JSON, which is what you want if another script reads it. The CLI pages through the results for you, so a large account still comes back complete.

Just the names, one per line, is handy for a loop:

PowerShell
aws iam list-users --query "Users[].[UserName]" --output text

Who Has Not Logged In Lately

list-users includes PasswordLastUsed for users who sign in to the console. Listing it is a quick way to spot old accounts:

PowerShell
aws iam list-users --query "Users[].{Name:UserName, LastConsoleLogin:PasswordLastUsed}" --output table

Users who have never signed in to the console show None there. Access key use is not in this list at all: for a full picture of passwords and keys, AWS’s IAM credential report is the tool for that job.

More IAM Commands

PowerShell
# Change AdminUser to the user you are checking
aws iam get-user --user-name AdminUser                     # one user
aws iam list-groups-for-user --user-name AdminUser         # their groups
aws iam list-attached-user-policies --user-name AdminUser  # policies attached to them
aws iam list-access-keys --user-name AdminUser             # their access keys

aws iam list-groups                                        # every group
aws iam list-roles --query "Roles[].[RoleName]" --output text  # every role

The full list is in the AWS CLI IAM command reference.


Related

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *