List of ec2 instances attached to one of iam role with cloudshell,cli

1.4k Views Asked by At

I want view details of IAM Role to how many instances it is attached to with Cloudshell, cli which commands should use give example.

lets assume I have IAM Role TestRole I want to know to how ec2 instances TestRole is attached to.

1

There are 1 best solutions below

1
On

You can use aws ec2 describe-iam-instance-profile-associations.

It lists Amazon EC2 instances and their associated IAM Roles (the relationship is known as an Instance Profile).

Documentation: describe-instances — AWS CLI Command Reference

Example output:

{
    "IamInstanceProfileAssociations": [
        {
            "AssociationId": "iip-assoc-0c406f0e0208b90e6",
            "InstanceId": "i-1234abcd",
            "IamInstanceProfile": {
                "Arn": "arn:aws:iam::<Account>:instance-profile/role1",
                "Id": "AIPAxxx"
            },
            "State": "associated"
        },
        {
            "AssociationId": "iip-assoc-035f9e94b2bb6f283",
            "InstanceId": "i-abcd1234",
            "IamInstanceProfile": {
                "Arn": "arn:aws:iam::<Account>:instance-profile/role2",
                "Id": "AIPAxxx"
            },
            "State": "associated"
        }
    ]

I would recommend using this command to list all associations, and then check the IamInstanceProfile to determine whether it is using the IAM Role of interest.