Skip to content

Instantly share code, notes, and snippets.

@avoidik
Created October 29, 2018 07:37
Show Gist options
  • Save avoidik/de015c0841aabec5e2d6c9fd6092d206 to your computer and use it in GitHub Desktop.
Save avoidik/de015c0841aabec5e2d6c9fd6092d206 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
@Selvaslm4
Copy link

List the all the Instances With all the Instances Details along with attached Volume Details it must include Volume Name, Volume ID and Size.

Using this command I can be able to list the volume details with instance id and volume size.

aws ec2 describe-volumes –-query "Volumes[*].[Attachments[0].VolumeId,AvailabilityZone,Attachments[0].InstanceId,Attachments[0].State,Size]" --output text > test.txt

But my query I have to consolidate the whole instance list its includes instance id, instance state, region, platform, key pair name along with attached volumes name, volume id, volume size and mount path name like /dev/sda1,

Please help me with this.

Using this command can be able to get the instance details but I need to single query to get all the details.

aws ec2 describe-instances --filters "Name=instance-state-name,Values=*" --query "Reservations[].Instances[].[Tags[?Key==Name]| [0].Value,InstanceId,State.Name,InstanceType,Placement.AvailabilityZone,PrivateIpAddress,VolumeInfo:BlockDeviceMappings" --output text > instances.txt

@avoidik
Copy link
Author

avoidik commented Aug 15, 2019

@Selvaslm4 you cannot do it in one query, what I may suggest is to use for-loops in bash, something like

for i in $(aws ec2 describe-instances --query ... --output text) ; do
  for v in $(aws ec2 describe-volumes --query ... --output text) ; do
    echo "${i} ${v}"
  done
done

@KarnGusain
Copy link

@Selvaslm4 .. you can do like.

$ aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[*].{"Instance Name":Tags[?Key==Name]|[0].Value, ImageId: ImageId, InstanceType: InstanceType, InstanceId: InstanceId, State: State.Name}' --profile dev --output table

OR

Below you can get the volume information along with instance info if that's the purpose.

$ aws ec2 describe-volumes --query 'Volumes[*].{AttachTime: Attachments[0].AttachTime, Device: Attachments[0].Device, InstanceId: Attachments[0].InstanceId, VolumeId: Attachments[0].VolumeId, DeleteOnTermination: Attachments[0].DeleteOnTermination, "Volume State": Attachments[0].State, SnapshotId: SnapshotId, Iops: Iops, Size: Size, Encrypted: Encrypted, "Volume Status": State }' --profile dev --output table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment