details of IAM role which is attached to ec2 instances with cloudshell

0

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.

2 Answers
1
Accepted Answer

You will need to run this against all of your active regions to get a full count.

aws ec2 describe-instances \
    --output text \
    --region us-east-1 \
    --query Reservations[*].Instances[*].InstanceId \
    --filters Name=iam-instance-profile.arn,Values=$(aws iam list-instance-profiles-for-role \
        --role-name TestRole \
        --query InstanceProfiles[*].Arn \
        --output text) | wc -l
profile pictureAWS
EXPERT
kentrad
answered 2 years ago
  • Thanks for saving so much time man. last query want view ec2 instances with no Iam role attached.

  • aws ec2 describe-instances --query 'Reservations[*].Instances[?IamInstanceProfile==null].InstanceId' --output text
    
  • Thanks again Forgot to tell I removed last thing " | wc -l " once again that this command too working great......

    aws ec2 describe-instances
    --output text
    --region us-east-1
    --query Reservations[].Instances[].InstanceId
    --filters Name=iam-instance-profile.arn,Values=$(aws iam list-instance-profiles-for-role
    --role-name TestRole
    --query InstanceProfiles[*].Arn
    --output text)

1

I am a newbie and did a little bit of research. I found this. See if it helps.

  1. Find instance profiles linked to role. https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/list-instance-profiles-for-role.html.
  2. Then find the instance profile and instance Id associations https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-iam-instance-profile-associations.html
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions