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
- Microsoft SQL server
- Aurora (AWS Proprietary database)
Let’s get started!
- AWS Account
- Basic understanding of RDS.
- Basic understanding of Python.
- Python is available on the system.
What we will do
- Install Dependencies.
- Know the required method.
- Create an RDS MySql Instance 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 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.
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
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”.
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.
You will see the response on the terminal.
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.
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.