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).