2 Answers
- Newest
- Most votes
- Most comments
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
1
I am a newbie and did a little bit of research. I found this. See if it helps.
- Find instance profiles linked to role. https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/list-instance-profiles-for-role.html.
- 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 3 years ago
Relevant content
- asked 3 years ago
- AWS OFFICIALUpdated 5 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
Thanks for saving so much time man. last query want view ec2 instances with no Iam role attached.
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)