Python's Boto3 Library can be used to automate infrastructure provisioning in AWS.
Using Boto3 infrastructure, automation can be performed efficiently saving developers time and required effort.
Python is a beginner-friendly, high-level programming language which is easily readable. It was created in the 1980s by Guido van Rossum and was coined Python because he liked the humour of the Monty Python comedy group.
It's one of the most popular languages in the world and is widely used for creating websites, games, etc. Due to its easy-to-learn nature, it has achieved widespread adoption. Python can also do things like math, creating pictures, decoding pictures, scraping data, etc., and can also be used in training AI models.
About Boto3
The Boto library is the AWS Software Development Kit (SDK) for Python that allows us to create, configure, and manage AWS services using APIs. The SDK provides API and low-level access to AWS.
Boto3 relies on the botocore library to handle low-level operations that are required to send secure API requests to AWS. The received XML responses are then transformed to Dictionary Type. Using Boto3, we can write Python scripts to automate various tasks with AWS, including:
Provisioning and managing Amazon Elastic Compute Cloud (EC2) instances
Storing and retrieving data from Amazon Simple Storage Service (S3)
Setting up and managing Amazon Virtual Private Clouds (VPCs)
Sending and receiving emails using Amazon Simple Email Service (SES) and much more
Overall, using Boto3 and Python to automate AWS infrastructure can save us time and effort and help in managing resources more efficiently.
Step 2: Click on New Application, fill in the details and click Create.
✍️
A bot token will be provided in the dashboard. Copy and save the provided bot token somewhere safe as it will be used for logging into the bot.
⚠️
If the token gets into the hands of Malicious Hackers, it can be used to impersonate your bot and steal sensitive info.
Step 3: Once created, go to the Bot tab and click Add Bot.
Step 4: Go to the OAuth2 tab, click on the drop-down and select URL Generator. We'll now pick the permissions for our bot.
Step 5: After you've picked the permissions, scroll down, and look for the auto-generated URL. This link contains parameters that will authorize the bot to access a server of our choice.
✍️
You should have Server Management Permissions to add the bot to our channel.
Now that our bot is created, let's work on the code for the bot itself!
Code the Bot
Setup a Virtual Environment
Step 1: Create a Virtual Environment
python3 -m venv venv
Step 2: Activate Virtual Environment
source venv/bin/activate
Install the Required Packages
Step 1: Create a file called requirements.txt. In this file, we will add the required packages.
Step 2: Run the following command to install the packages.
pip install -r requirements.txt
Setup .env
Create an .env file and paste the recently generated token into the file as presented below.
Let's create a bot that can create and delete S3 resources (bucket & objects).
Create a file called bot.py and paste the following code into it.
This code contains all the required functions for performing bucket creation, bucket deletion and object deletion operations.
ℹ️
If you need to delete a bucket, you must delete all the objects (data) inside the bucket as well. This is required, so we don't accidentally delete a bucket with data.
Run the App
Now, run the application using the command below:
python3 bot/test_bot.py
Communicate with the Bot
Now that our bot is running let's provide some commands to the discord bot.
For Creating Bucket
For Deleting Bucket
✍️
These commands are to be provided to the discord bot we created via Direct Message or by mentioning it in the channel we authorized it on.
Conclusion
In this article, we learned to code a python application which controls AWS infrastructure according to the user's requirements. We learned to use libraries like Boto3, Logging, Discord, etc., to create our application.
In the next article, we will learn to implement AWS Lambda Functions and API Gateway in our Python Application.
Thanks for reading! Please drop any queries you have below, I'll periodically keep updating this article to maintain it's authenticity.