Does boto3 ec2.instances.filter have equivalent to AWS CLI --query?

0 votes

I started by using the AWS CLI to create a bash script to get the data I wanted about my instances. The main line being:

aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" --query Reservations[*].Instances[*].[PublicDnsName,PublicIpAddress,PrivateIpAddress,BlockDeviceMappings[*].DeviceName] --output text

Then I realised I wanted something more complex so switched to python and boto3

ec2 = boto3.resource('ec2')
    instances = ec2.instances.filter(
        Filters = [ {
            'Name' : 'instance-state-name',
            'Values' : [ 'running' ]
        } ]
    )
for i in instances:
    """ The whole data set is here """
    print(i.public_dns_name)
    print(i.public_ip_address)
    """ etc """

I can traverse the response and get the data out but the boto3 version seems quite wasteful as I don't need most of the data that is being returned.

Can I get the boto3 interface to only return the data I am interested in, similar to the way the AWS CLI does using --query? (My assumption here being that the AWS CLI is not just getting the entire response and parsing it).

Sep 18, 2018 in AWS by bug_seeker
• 15,360 points
1,517 views

1 answer to this question.

0 votes
"filter - does filtering at the AWS level and returns the response. query - filters the results returned from AWS based on JMESPath query expressions"

I had also confirmed the same using tcpdump. The same data is returned from the server regardless of the contents of the --query parameter.
answered Sep 18, 2018 by Priyaj
• 56,940 points

Related Questions In AWS

+5 votes
2 answers

AWS EC2 Instances are not getting to internet access

Check your VPC settings, whether you are ...READ MORE

answered Oct 16, 2018 in AWS by Omkar
• 67,660 points
1,372 views
0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29 in AWS by Priyaj
• 56,940 points
390 views
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,560 points
479 views
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket. docs.aws.amazon.com/AmazonS3/latest/user-guide/….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 56,940 points
307 views
0 votes
1 answer

Pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python (boto3)?

Python String and Integer concatenation >>> print("arn:aws:swf:us-east-2:{0}:action/actions/AWS_EC2.InstanceId.Stop/1.0".format(acccnum)) arn:aws:swf:us-east-2:12312312312312:action/actions/AWS_EC2.InstanceId.Stop/1.0 >>> print("arn:aws:swf:us-east-2:" ...READ MORE

answered Oct 5, 2018 in AWS by Priyaj
• 56,940 points
168 views
0 votes
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 56,940 points
683 views
0 votes
1 answer