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
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
What IAM Role permissions required to restore CMK encrypted EC2 instances ?asked 3 years ago
How can you restrict EC2 instances to assuming an IAM role based on the instance's tags?asked a year ago
Limit which IAM roles can be attached to an EC2 instance by different IAM usersasked a year ago
Powershell script to retrieve all EC2 instances across all accountsasked 3 months ago
How to use AWS cloudshell when assuming role?asked a year ago
ECS Task is not using the role attached to it?Accepted Answerasked 3 months ago
My IAM role is not listed when trying to create a new EC2 instanceasked 10 months ago
details of IAM role which is attached to ec2 instances with cloudshellAccepted Answerasked 8 months ago
Not able to use IAM role assigned to EC2 instance at startupasked 3 years ago
Using EC2 IAM role principal in SecretsManager resource policy together with autoscalingAccepted Answerasked a year ago