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 年前1754 查看次数
2 回答
1
已接受的回答

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
专家
kentrad
已回答 2 年前
  • 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
已回答 2 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则