How Do You Get Passed “ImportError: No module named ‘boto'”?

Problem scenario
You want to retrieve VPC peering connection info and other VPC info via Boto3. With Python 3 you tried to run this Python 2 (and Boto 2.x) program:

import boto.vpc
c = boto.vpc.connect_to_region('us-east-1')
vpcs = c.get_all_vpcs()
vpc_peering_connection = c.create_vpc_peering_connection(vpcs[0].id, vpcs[1].id)

(It was written by the person who developed Boto 2.x here.)

You have a variety or problems with the syntax not working. Here are some example error messages:

ImportError: No module named 'boto'

NameError: name 'boto' is not defined

NameError: name 'vpc' is not defined

What do you do to adapt "import boto.vpc" and using Boto3 to interact with VPC components (with Python 3) or otherwise get the above program to work?

Solution
Try this program (but replace "pcx-abcd1234" with a VpcPeeringConnection ID):

import boto3
ec2_contint = boto3.client('ec2')
pcs = ec2_contint.describe_vpc_peering_connections(VpcPeeringConnectionsIds=['pcx-abcd1234'])
#to list all peering connections uncomment out this line:
#pcs = ec2_contint.describe_vpc_peering_connections()
print(pcs)

Leave a comment

Your email address will not be published. Required fields are marked *