Create EC2 using Python automation and boto3

Create EC2 using Python automation and boto3

March 25, 2021 / Nirav Shah

How to Create EC2 using Python automation and boto3

If we want to create an EC2 instance with a single click we can go with the python automation.

We already learn why Python is used for the cloud infrastructure automation by referring below link: New Approach for the Automation: Python

What Is Boto3

Let’s get started with knowing something about boto3 and understand it properly. Boto3 is the Amazon Web Services (AWS) SDK for Python. AWS infrastructure provisioning is done with using boto3 script in python. By this Python developers create, configure, and manage AWS services.

Boto3 provides an easy to use, object-oriented API, as well as low-level access to AWS services. Boto3 is built on the Botocore, which is shared by the AWS CLI. Botocore provides the low-level clients, session and credentials and configuration data.

Core Concept of boto3

  • Resource: High-level object to access AWS services
  • Client: Low-level object to access AWS services
  • Meta: Object to enter into client object from the resource object
  • Meta: Object to enter into client object from the resource object
  • Session: It stores the configuration state and you to create AWS service resource object and clients object. (Object to get Connect with particular AWS account or IAM user account)
  • Collections: A tool to iterate and manipulate groups of resources
  • Paginators: Automatic paging of response
  • Waiters: A way to block until a certain state has been reached.

Install AWS CLI and Python Boto3

    
#pip install awscli
#pip install boto3    

Now configure ‘aws configure’ on your CLI, by entering Access key, Security key, Region name and output format- {JSON or Yaml}

You can find Access key and Security key form the My Security Credentials from the Account name.

Now we are going to create AWS infrastructure with Python automation with the help of boto3.

For that, we need to create pratice.py file and write the python automation script.

Pratice.py

When you run this script it will create VPC, InternetGateway Id, RouteTable, Subnets, SecurityGroup, KeyPair and Ec2-Instance automatically.

Run the below script and verify the output.

Python Script – Boto3-Test.py

Refer below the output of the script.

Now Refer AWS cloud console for the infrastructure output of the script.

1. VPC Created

2. RouteTable Created

3. Subnet Created

4. SecurityGroup Created

5. EC2 instance successfully created using the above parameters.

This how you can automate your infrastructure with python and minimize your manual work.

Also Read: Launch an AWS Ec2 instance for free

Talk to AWS Certified Consultant

    Spread Love By Sharing:

    Let’s Talk About Your Needed AWS Infrastructure Management Services

    Have queries about your project idea or concept? Please drop in your project details to discuss with our AWS Global Cloud Infrastructure service specialists and consultants.

    • Swift Hiring and Onboarding
    • Experienced and Trained AWS Team
    • Quality Consulting and Programming
    Let’s Connect and Discuss Your Project