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 個答案
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 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南