How To Create Aws VPC Using Boto3 Python

aws python boto3
Reading Time: 3 minutes

Hello Readers! In this blog we will see How To Create Aws VPC Using Boto3 Python. So before starting firstly we will see what is Boto3 and some short introduction about Aws VPC ?

What is Boto3 ?

  • Boto3 is the name of python sdk for AWS or you can say it is a module, library or API to work with AWS Services using python Scripts.
  • Using Boto3 we can create, delete and update AWS Services.
  • Boto3 can be executed from local server or using AWS lambda Service.
  • If we have to work with AWS Services using python we have to install Boto3.

What is Aws VPC ?

  • Amazon VPC is a Virtual Network that closely resembles a traditional networking that you operate in your own data center , with the benefits of using the scalable Infrastructure of AWS.
  • It is logically Isolated from other Virtual network in the AWS Cloud.

Prerequisites:

  • Create IAM User
  • Install AWS CLI and configure
  • python3
  • boto3

 What we will do

  1. Install Boto3
  2. Know the required method
  3. Create an VPC using Python Boto3

Python comes by default in Ubuntu 18.04 Server, so you do not need to install it.

To check the Python version on your system, use the following command.

which python
/usr/bin/python --version

or
python --version

If you do not have python than you can execute the following command to first update the local repo.

sudo apt update

 To install pip use by this command.

sudo apt install python-pip

To check the version of Pip installed, you can execute the following command.

pip --version

Install boto3:

Install Boto3, by using this command:

pip3 install boto3
To check if the Boto3 is installed and to check its version, execute the following command.
pip show boto3
boto3 pip cli

Let’s get started !

Know the required methods

To create an VPC and its dependent components we will use this methods.

  1. create_vpc
  2. create_route_table
  3. create_internet_gateway
  4. create_subnet
  5. associate_with_subnet

Now, let’s see the methods with their list of acceptable parameters for creating an vpc.

Syntax of  create_vpc method

response = client.create_vpc(
    CidrBlock='string',
    AmazonProvidedIpv6CidrBlock=True|False,
    Ipv6Pool='string',
    Ipv6CidrBlock='string',
    DryRun=True|False,
    InstanceTenancy='default'|'dedicated'|'host',
    Ipv6CidrBlockNetworkBorderGroup='string'
)
  • CidrBlock: The IPv4 network range for the VPC, in CIDR notation. This is an important field and an VPC can’t we create without this.

Syntax of create_route_table method

route_table = ec2.create_route_table(
    DryRun=True|False,
    VpcId='string'
)
  • VpcId: This ID of an VPC. This is also an important field and use to create a Route Table in the Specified VPC.

Syntax of create_internet_gateway method

response = client.create_internet_gateway(
    DryRun=True|False
)

Every VPC has a single Internet Gateway. This will be attache with the VPC

Syntax of create_subnet method

subnet = ec2.create_subnet(
    AvailabilityZone='string',
    AvailabilityZoneId='string',
    CidrBlock='string',
    Ipv6CidrBlock='string',
    OutpostArn='string',
    VpcId='string',
    DryRun=True|False
)

basically this is use to subdivide the VPC.

Syntax of associate_with_subnet method

route_table_association = route_table.associate_with_subnet(
    DryRun=True|False,
    SubnetId='string',
    GatewayId='string'
)
  • SubnetId: The ID of the subnet.

Create a VPC using Python Boto3

Step 1: I will create an VPC, create “boto.py” name of it and also you can give other name of your files.

firstly you have need to change the value of “aws_access_key_id_value” and “aws_secret_access_key_value” with your own access_key_id and access_key_value respectively. Also change the value of “region_name”. 

If you want you can change the values of CidrBlock for “create_vpc” and “create_subnet” or you can keep as is and you can use an editor according your choice.

vim boto.py
import boto3

ec2 = boto3.resource('ec2', aws_access_key_id='ACCESS-KEY-OF-THE-AWS-ACCOUNT',
                     aws_secret_access_key='SECRETE-KEY-OF-THE-AWS-ACCOUNT',
                     region_name='AWS-Region')

vpc = ec2.create_vpc(CidrBlock='192.168.0.0/16')
# Assign a name to the VPC
vpc.create_tags(Tags=[{"Key": "Name", "Value": "my_vpc"}])
vpc.wait_until_available()
print(vpc.id)

# Create and Attach the Internet Gateway
ig = ec2.create_internet_gateway()
vpc.attach_internet_gateway(InternetGatewayId=ig.id)
print(ig.id)

# Create a route table and a public route to Internet Gateway
route_table = vpc.create_route_table()
route = route_table.create_route(
    DestinationCidrBlock='0.0.0.0/0',
    GatewayId=ig.id
)
print(route_table.id)

# Create a Subnet
subnet = ec2.create_subnet(CidrBlock='192.168.1.0/24', VpcId=vpc.id)
print(subnet.id)

# associate the route table with the subnet
route_table.associate_with_subnet(SubnetId=subnet.id)

Now, to create a VPC with the above specific configuration, execute the python script using this command.

python3 boto.py
boto3 cli

You can verify if the VPC has been created from the AWS Console.

aws vpc dashboard

In the above screenshot, you can see that the VPC with the specific CIDR and Name has been created. You can check for components like Subnet, Internet Gateway, Route Table.

Conclusion

In this blog we have seen How To Create Aws VPC Using Boto3 Python. Thank you for sticking to the end. Therefore If you like this blog, please do show your appreciation by giving thumbs ups and share this blog.

HAPPY LEARNING!

knoldus

Written by 

I am an enthusiastic , hard-working and determine girl with strong attention to detail and eager to learn about new technologies.

1 thought on “How To Create Aws VPC Using Boto3 Python5 min read

Comments are closed.