2

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.

whoisraibolt
  • 614
  • 1
  • 9
  • 32
Exiett
  • 21
  • 4
  • Does this answer your question? [boto3 client NoRegionError: You must specify a region error only sometimes](https://stackoverflow.com/questions/40377662/boto3-client-noregionerror-you-must-specify-a-region-error-only-sometimes) – Anon Coward Mar 12 '21 at 20:08
  • You can specify region when you create the client. region_name='us-west-2' – balderman Mar 12 '21 at 20:09

1 Answers1

2

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.

Coin Graham
  • 255
  • 7