How Do You Filter the Output of an AWS CLI Command?

Problem scenario
You want to return only the JSON of the output that is related to a specific role -- not the entire list of an "aws iam list-roles" command. What do you do?

You must have the AWS CLI installed. If you need assistance, see this posting.

Use the "--query" flag. Often the syntax to structure a query in the results is the word to the right of the "list-" in an AWS CLI command. Such is the case for "aws iam list-roles":

Here is an example (just replace the "123456:role/foobar" with the Arn value right of the "arn:aws:iam" string):

aws iam list-roles --query 'Roles[?Arn==arn:aws:iam::123456:role/foobar]'

aws ec2 describe-subnets --query 'Subnets[?SubnetId==subnet-0a1234efgh]'

Leave a comment

Your email address will not be published. Required fields are marked *