Skip to content

Instantly share code, notes, and snippets.

@HelioCampos
Forked from avoidik/README.md
Created September 16, 2019 10:41
Show Gist options
  • Save HelioCampos/0092f031bfa02c84068bef714bdf20d7 to your computer and use it in GitHub Desktop.
Save HelioCampos/0092f031bfa02c84068bef714bdf20d7 to your computer and use it in GitHub Desktop.
AWS query examples

Top 10 Examples of AWS CLI Query

List Volumes showing attachment using Dictionary Notation

$ aws ec2 describe-volumes \
  --query 'Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,AZ:AvailabilityZone,Size:Size}'
[
    {
        "InstanceId": "i-a071c394",
        "AZ": "us-west-2a",
        "ID": "vol-e11a5288",
        "Size": 30
    },
    {
        "InstanceId": "i-4b41a37c",
        "AZ": "us-west-2a",
        "ID": "vol-2e410a47",
        "Size": 8
    }
]

List volumes using List Notation

$ aws ec2 describe-volumes \
    --query 'Volumes[*].[VolumeId, Attachments[0].InstanceId, AvailabilityZone, Size]'
[
    [
        "vol-e11a5288",
        "i-a071c394",
        "us-west-2a",
        30
    ],
    [
        "vol-2e410a47",
        "i-4b41a37c",
        "us-west-2a",
        8
    ]
]

List running workspaces

$ aws workspaces describe-workspaces \
  --query "Workspaces[?WorkspaceProperties.RunningMode==`ALWAYS_ON`][UserName,State]"

[
    [
        "Alice",
        "AVAILABLE"
    ],
    [
        "Ben",
        "AVAILABLE"
    ]
] 

List all Route 53 record names and their type for a zone

$ aws route53 list-resource-record-sets \
	--hosted-zone-id HOSTED_ZONE_ID \
	--output text \
--query "ResourceRecordSets[].[join(': ',[Name,Type])]"

Filter Instances based on Name tag. [0] will flatten the list — good for output as text

$ aws ec2 describe-instances \
  --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`].Value[] | [0]]' \
--output text

List first 5 images owned by AMAZON

$ aws ec2 describe-images \
  --owner amazon --query 'Images[0:5].[ImageId,Name]' \
--output text

List Images starts with test in the image Name

$ aws ec2 describe-images --owner amazon \
  --query 'Images[?starts_with(Name, `test`) == `true`]|[0:5].[ImageId,Name]' \
--output text

List all CloudWatch log groups with event expiry

$ aws logs describe-log-groups \
	--output text \
--query "logGroups[].[join(': ',[logGroupName,to_string(retentionInDays || 'Never Expire')])]"

List EC2 marketplace AMI ID’s for a given product ID

$ aws ec2 describe-images \
	--filters "Name=name,Values=*-PRODUCT_ID-*" \
	--output text \
--query "reverse(sort_by(Images,&CreationDate))[].[join(':',[ImageId,CreationDate,Description])]"

List of tomcat7 Elastic Beanstalk Images

$ aws ec2 describe-images --owner amazon \
  --query 'Images[?Name!=`null`] \
  |[?starts_with(Name, `aws-elasticbeanstalk`) == `true`] \
|[?contains(Name, `tomcat7java6-pv`) == `true`].[CreationDate,ImageId,Name]' 

Reference

https://medium.com/@ripon.banik/top-10-examples-of-aws-cli-query-e717524dc346
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment