BOTO3 using Python to fetch information of a list of EC2

Viewed 872

I am trying to compile information from a list of EC2s that I have on a .csv using Python + Boto3.

This .csv contains the Private IPs of those instances. The following command returns everything that I need:

aws ec2 describe-network-interfaces --filters Name=addresses.private-ip-address,Values="<PRIVATE IP>" --region <MY REGION>

So I've decided to use Boto3 to do something similar.

But my code isn't returning the information inside the dictionary because I cannot specify the Region inside the code.

The documentation allows me to specify the Availability Zone, but it won't just work.

ec2 = boto3.client('ec2')
describe_network_interfaces = ec2.describe_network_interfaces(
    Filters=[
        {
            'Name': 'addresses.private-ip-address',
            'Values': [
                '<PRIVATE IP>'
            ],
            'Name': 'availability-zone',
            'Values': [
                '<REGION>'
            ]
        }
    ],
    MaxResults=123
)
print(describe_network_interfaces)

☝️ This returns me this

{'NetworkInterfaces': [], 'ResponseMetadata': { <LOTS OF METADATA> }}

I believe it is not working because I can't specify the Region with describe_network_interfaces of Boto3. But I can do it with the AWS CLI command.

Any suggestions?

OBS: popen is not a good idea for this current project.

Thanks in advance.

1 Answers

You can set the region at the client level with something like:

my_region = "us-east-1"
ec2 = boto3.client('ec2', region_name=my_region)

This worked in my environment successfully to get information about systems running in another region.

Related