{"id":14402,"date":"2014-06-30T11:38:45","date_gmt":"2014-06-30T06:08:45","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14402"},"modified":"2014-06-30T11:40:12","modified_gmt":"2014-06-30T06:10:12","slug":"filter-aws-cli-output-with-the-query-option","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/filter-aws-cli-output-with-the-query-option\/","title":{"rendered":"Filter aws cli Output with the &#8211;query Option"},"content":{"rendered":"<p>The AWS CLI provides built-in output filtering capabilities with the &#8211;query option. To demonstrate how it works, I&#8217;ll start with JSON output of aws ec2 describe-volumes<\/p>\n<p>aws ec2 describe-volumes &#8211;output json<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/11.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14439\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/11.png\" alt=\"1\" width=\"1295\" height=\"709\" \/><\/a><\/p>\n<p>Suppose we have to print all availability zones,<\/p>\n<p>aws ec2 describe-volumes &#8211;query &#8216;Volumes[*].AvailabilityZone<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14440\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/2.png\" alt=\"2\" width=\"865\" height=\"57\" \/><\/a><\/p>\n<p>Here,<br \/>\naws ec2 describe-volumes : Shows descriptive information for one or more of your Amazon EBS volumes in a particular region<br \/>\n&#8211;query : to filter output<br \/>\n[] : array<br \/>\nAvailabilityZone : key whose value is &#8221; us-east-1b us-east-1b&#8221;<\/p>\n<p>Here * is used to print all values of AvailabilityZone. If we want to print first value of AvailabilityZone or second value of AvailabilityZone than we can replace * by 0 or 1 respectively.<\/p>\n<p>suppose we have to print all the values of Attachments<\/p>\n<p>aws ec2 describe-volumes &#8211;query &#8216;Volumes[*].Attachments&#8217;<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14443\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/3.png\" alt=\"3\" width=\"1017\" height=\"87\" \/><\/a><\/p>\n<p>But, if we only want to print InstanceId<\/p>\n<p>aws ec2 describe-volumes &#8211;query &#8216;Volumes[*].Attachments[*].InstanceId&#8217;<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/4.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14445\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/4.png\" alt=\"4\" width=\"1039\" height=\"76\" \/><\/a><br \/>\nHere this query will print all the value of instansce id from the json file<br \/>\nby volume[*] we enter into the array of volumes<br \/>\nand by appending .attachments[*] to volumes[*] we drill down or enter into the attachment array and by appending .instanceid to .attachments we can get the desire values for the instance id. Again we can replace * in Attachments[*] with 0 or 1 to print first instance id or second instance id respectively.<br \/>\nWe can also get values of multiple parameters, like if we want to print InstanceId and State of the volume<\/p>\n<p>aws ec2 describe-volumes &#8211;query &#8216;Volumes[*].[Attachments[*].[VolumeId,InstanceId,State]]&#8217;<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/5.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14449\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/5.png\" alt=\"5\" width=\"1093\" height=\"72\" \/><\/a><\/p>\n<p>Now if we want to know the availability zone, InstanceId and state of volume<\/p>\n<p>aws ec2 describe-volumes &#8211;query &#8216;Volumes[*].[Attachments[0].VolumeId,AvailabilityZone,Attachments[0].InstanceId,Attachments[0].State]&#8217;<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/6.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-14452\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/06\/6.png\" alt=\"6\" width=\"1294\" height=\"92\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The AWS CLI provides built-in output filtering capabilities with the &#8211;query option. To demonstrate how it works, I&#8217;ll start with JSON output of aws ec2 describe-volumes aws ec2 describe-volumes &#8211;output json Suppose we have to print all availability zones, aws ec2 describe-volumes &#8211;query &#8216;Volumes[*].AvailabilityZone Here, aws ec2 describe-volumes : Shows descriptive information for one or [&hellip;]<\/p>\n","protected":false},"author":124,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[1174],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14402"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/124"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14402"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14402\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}