Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Configure a internet-connected VPC from scratch
import boto.vpc
import time
REGION_NAME = 'us-west-2'
AMI_ID = 'ami-8e27adbe' # Amazon Linux AMI
conn = boto.vpc.connect_to_region(REGION_NAME)
# Create a VPC
vpc = conn.create_vpc('')
# Configure the VPC to support DNS resolution and hostname assignment
conn.modify_vpc_attribute(, enable_dns_support=True)
conn.modify_vpc_attribute(, enable_dns_hostnames=True)
# Create an Internet Gateway
gateway = conn.create_internet_gateway()
# Attach the Internet Gateway to our VPC
# Create a Route Table
route_table = conn.create_route_table(
# Create a size /16 subnet
subnet = conn.create_subnet(, '')
# Associate Route Table with our subnet
# Create a Route from our Internet Gateway to the internet
route = conn.create_route(, '',
# Create a new VPC security group
sg = conn.create_security_group('pycon_group',
'A group for PyCon',
# Authorize access to port 22 from anywhere
sg.authorize(ip_protocol='tcp', from_port=22, to_port=22, cidr_ip='')
# Run an instance in our new VPC
reservation = conn.run_instances(AMI_ID, key_name='aws_mitch',
instance = reservation.instances[0]
# Now create an Elastic IP address for the instance
eip = conn.allocate_address(domain='vpc')
# And associate the EIP with our instance
# Wait for the instance to be running and have an public DNS name
while instance.state != 'running' or not instance.public_dns_name:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment