Home > OS >  AWS CLI - List Unencrypted Volumes and Filter Results --query
AWS CLI - List Unencrypted Volumes and Filter Results --query

Time:02-19

I want to use AWS CLI to list all unencrypted volumes and the associated tag key value for the tag key 'application'. I'm having difficulty getting the -query portion correct for the desired filtering. If I run:

aws ec2 describe-volumes --filters Name=encrypted,Values=false Name=tag-key,Values=application

I get all of the volume information, which I do not want. If I add in the --query as shown below I only get the tag key values. I want the VolumeId as well but I'm unable to get the syntax for the correct after multiple attempts.

aws ec2 describe-volumes --filters Name=encrypted,Values=false Name=tag-key,Values=application --query 'Volumes[].[ [Tags[?Key==`application`].Value][0][0],[Tags[?Key==`tag_component_name`].Value][0][0] ]'

Again, I simply want to output VolumeID and tag key values for the tag key 'application' for all unencrypted volumes.

CodePudding user response:

Use --filters to limit which resources are returned and use --query to specify what data you would like displayed.

This will list only unencrypted volumes and will display the VolumeId and the value of the application tag:

aws ec2 describe-volumes --filters Name=encrypted,Values=false --query 'Volumes[].[VolumeId,Tags[?Key==`application`]|[0].Value]' --output text

Depending upon your operating system, you might need to play with the quotation marks.

  • Related