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,510 points
8,222 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
• 58,020 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
• 69,220 points
13,271 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,820 points
2,868 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
• 58,020 points
5,958 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
• 58,020 points
1,583 views
–1 vote
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
• 58,020 points
5,204 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP