How do I list tags for all roles using AWS CLI

Viewed 2177

I would like to list tags for all of my roles within IAM.

aws iam list-role-tags --role-name role123 will only list tags for single role.

aws iam list-roles will list all the roles.

How do I concatenate these two cli commands to list all the tags in all roles?

2 Answers

You can do this with the following shell script:

# Get all role names as text
roles=$(aws iam list-roles \
        --query 'Roles[*].RoleName' \
        --output text)

# Loop through role names and get tags
for role in $roles
do
    aws iam list-role-tags --role-name $role
done

If the respective role's tag lists were too long and got truncated you would have to do some extra work. But I think this is a good starting point.

Or if you just want to grab Tags and single-line command then try this

for rolename in $(aws iam list-roles --query 'Roles[*].RoleName' --output text);do aws iam list-role-tags --role-name $rolename --query "Tags";done

Output

[
    {
        "Key": "Name",
        "Value": "test"
    }
]
Related