How to create an RDS instance using python Boto3 on AWS

aws python boto3
Reading Time: 4 minutes
Python Boto3 RDS: Postgres, MySQL, Connect, List, Start, Stop, Delete

Hello Readers! In this blog, we will see how to create an RDS MySql Instance using the Boto3 python Library. We will use “create_db_instance” method to create an Instance.

Before we start, I assume that you are familiar with AWS RDS Service. If you are not familiar with it and what to first learn to create an RDS MySql Instance from the AWS Console, search for “How to setup an RDS MySql (Relation Database MySql ) instance on AWS?”. So before starting firstly we will see what is Boto3 and some short introduction about Aws RDS Service ?

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 RDS Service?

  • RDS stands for Relational Database Service
  • It’s a managed DB service for DB use SQL as a query language,
  • It allows you to create databases in the cloud that are managed by AWS
    • Postgres
    • MySQL
    • MariaDB
    • Oracle
    • Microsoft SQL server
    • Aurora (AWS Proprietary database)

Let’s get started!

Pre-requisites:

  • AWS Account
  • Basic understanding of RDS.
  • Basic understanding of Python.
  • Python is available on the system.

 What we will do

  1. Install Dependencies.
  2. Know the required method.
  3. Create an RDS MySql Instance using Python Boto3.

Install Dependencies

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 python3
/usr/bin/python3 --version

or
python3 --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

Know the required method

To create an RDS Instance, we shall use “create_db_instance” method. Following is the syntax of the method with all the parameters which it accepts.

  • DBName: The meaning of this parameter differs according to the database engine you use.
  • DBInstanceIdentifier: This is an important parameter. It is a DB instance identifier. This parameter is stored as a lowercase string.
  • DBInstanceClass: This is an important parameter. It specifies the compute and memory capacity of the DB instance.
  • Engine: The name of the database engine to be used for the instance to be created. This is a mandatory field
  • MasterUsername: The name for the master user. This is the user of the DB in the Instance
  • MasterUserPassword: The password for the master user that we create in the instance.
  • VpcSecurityGroupIds: A list of Amazon EC2 VPC security groups to associate with this DB instance. This Security Group has rules which allow the connection on the specified ports in it.
  • Port: The port number on which the database accepts connections. If you want to allow the connection on this port, you have to specify this port in the Security Group.

Create an RDS MySql Instance using Python Boto3

To create an RDS Instance, create a file “boto.py” and copy-paste the following code in it.

Do not forget to change the values of “aws_access_key_id_value” and “aws_secret_access_key_value” with your own access_key_id and access_key_value respectively. 

If you want to create the instance in a region of your choice, change the value of “region_name” too else keep it unchanged.

Also, make sure to assign the exiting security group id to “VpcSecurityGroupIds”.

vim boto.py
import boto3
conn = boto3.client('rds', aws_access_key_id='ACCESS-KEY-OF-THE-AWS-ACCOUNT',
                     aws_secret_access_key='SECRETE-KEY-OF-THE-AWS-ACCOUNT',
                     region_name='eu-west-3')

response = conn.create_db_instance(
        AllocatedStorage=10,
        DBName="test",
        DBInstanceIdentifier="my-first-rds-instance",
        DBInstanceClass="db.t2.micro",
        Engine="mysql",
        MasterUsername="root",
        MasterUserPassword="pass1234",
        Port=3306,
        VpcSecurityGroupIds=["sg-7fa4d512"],
    )

print (response)

Now, to create an RDS MySQL Instance with the above specific configuration, execute the python script using this command.

python3 boto.py
AWS RDS instance cli

You will see the response on the terminal.

To verify the instance state from the AWS Console, go to an RDS Dashboard.

RDS Dashboard

In the above screenshot, you can see that the RDS MySql Instance using Boto3 Library in Python.You can customize the code and create an instance of your choice.

after creating the RDS MySQL Instance you can also delete this RDS instance, first of all you have to need click on actions bar and choose the delete as you can see below screenshot.

RDS Delete dashboard

Conclusion

We learned to create an RDS MySql Instance in this blog using Boto3 Library in Python. You can customize the code and create an instance of your choice. We also saw how to install the dependencies required to write and execute the Python code.

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.