Make your own VPC with a Single Public Subnet Only with boto

If you create a VPC in the Amazon VPC Management Console,

You can choose from four templates (as of August 2012), but how do you hit them with the API to make your own? It is a story. Isn't it cooler and more reproducible with a single command than making it with a console?

For the time being, let's make the first "VPC with a Single Public Subnet Only". The language is Python, and of course the library is boto. Click here for boto's VPC reference (http://docs.pythonboto.org/en/latest/ref/vpc.html).

import boto.ec2
from boto.vpc import VPCConnection

#Region is ap-northeast-1
ec2region = boto.ec2.get_region("ap-northeast-1")

def launch_vpc():
    conn = VPCConnection(region=ec2region)

    #Create a VPC
    vpc = conn.create_vpc('10.0.0.0/16')

    #Set up internet gateway
    igw = conn.create_internet_gateway()
    conn.attach_internet_gateway(igw.id, vpc.id)

    #Create subnet
    subnet = conn.create_subnet(vpc.id, "10.0.0.0/24")

    #Set up routing
    #See below for filters
    # http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeRouteTables.html
    route_table = conn.get_all_route_tables(filters=(("vpc-id", vpc.id),))[0]
    conn.create_route(route_table.id, "0.0.0.0/0", gateway_id=igw.id)
    print "Created VPC %s" % vpc.id

It looks like this, but strictly speaking, it's a little different from the template.

In the template, the main routing table cannot go out, and a routing table for going out is created separately, but in the above code, an exit is provided for the main routing table. I am. Only one routing table is created. I think this is easier to use if you want all the servers to go out.

The hard part was allocating the routing table assigned to the created VPC. I could do this by setting a filter for get_all_route_tables, but I couldn't get there. There are many types of filters in DescribeRouteTable Reference. When using boto, you should also check the API reference on the AWS side.

conn.get_all_route_tables(filters=(("vpc-id", vpc.id),))[0]

Now you can pull in the routing table where the VPC id matches vpc.id.

By the way, at first I was thinking of writing in Ruby, but the AWS SDK for Ruby does not support VPC. After all, if you use AWS, it's Python!

Recommended Posts

Make your own VPC with a Single Public Subnet Only with boto
Try to make your own AWS-SDK with bash
A memorandum to make WebDAV only with nginx
Make your own module quickly with setuptools (python)
Make your own music player with Bottle0.13 + jPlayer2.5!
Let's make a number guessing game in your own language!
Make a fortune with Python
Make a fire with kdeplot
If you make 1 billion private keys, you can make a public key including your name with high probability.
Make multiple numerical elevation data into a single picture with Python
How to make your own domain site with heroku (free plan)
[Python] Make your own LINE bot
Make your own manual. [Linux] [man]
Let's make a GUI with python.
Make a sound with Jupyter notebook
Solve your own maze with Q-learning
Let's make a breakout with wxPython
Make a recommender system with python
Make a filter with a django template
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Make a model iterator with PySide
Train UGATIT with your own dataset
Make a nice graph with plotly
Solve your own maze with DQN
Let's make an image recognition model with your own data and play!
[Streamlit] I hate JavaScript, so I make a web application only with Python