Hello Readers! In this blog we will see how we can create EC2 instances using Python3 with Boto3. So, firstly we will see what is AWS Boto3? Basically, AWS Boto3 is a python software development kit for our Amazon Web Services. Here we will see how we can do different operations on AWS services by the help of python scripts and boto3. Versions of Boto include Boto, Boto3, and Botocore. Boto3 is the latest one in these versions.
Let’s get started!
We have some following prerequisites for starting it:
- You must have installed Python3 on your system.
- You must have installed Boto3 on your system. Do prefer this blog for the setup of Boto3: https://blog.knoldus.com/introduction-to-boto3-and-its-installation/
- Install AWS CLI and Configure it. If you want to check if you have configured it or not, you can check it like this.
Create EC2 instance by Boto3:
After following all the above steps successfully, make a folder and create a python file for the scripts. Give the name of your folder and file as per your wish. Now, paste the following python script in your python file you created.
import boto3 ec2 = boto3.resource('ec2') instances = ec2.create_instances( ImageId="ami-0dafa01c8100180f8", MinCount=1, MaxCount=1, InstanceType="t2.micro", KeyName="KeyPair1" )
In the above script we have the following lines:
- Import boto3 : It is used to import boto3.
- ec2 = boto3.resource(‘ec2’) : The resource or service I am using with boto3 is ec2.
- instances = ec2.create_instances : It will create an EC2 instance.
- ImageId : It is an Amazon Machine Image (AMI) id. When we create an EC2 instance the first step is to select AMI id. And it changes according to the region you choose. Here I am using ap-south-1 (Mumbai Region).
So, according to your choice you have to change the ImageId in the code.
- MinCount : This specifies the minimum number of EC2 instances you want to create. According to you, you can change this.
- MaxCount : This specifies the maximum number of EC2 instances you want to create. According to you, you can change this.
- InstanceType: This specifies the type of the EC2 instance you want to create. There are a number of types of instances. You can choose this as per your choice.
Here I am choosing t2.micro. So, you can change this in the script.
- KeyName: This will be the name of the keypair which we will use to access the created instance. So, for this you have to create a keypair and give that keypair name in this script.
If you want to create it using AWS CLI use the following command:
$ aws ec2 create-key-pair --key-name KeyPair1 --query 'KeyMaterial' --output text > KeyPair1.pem
So, that’s all you have to do. Let’s run this script for creating EC2 instances.
Now move to AWS Console and see the result:
You can see here the instance is created and its status is initializing.
Stop, Terminate and start EC2 instance using Boto3:
It is also so simple. You only have to change the InstanceIds of your EC2 instance according to you. So, make the modification in the following script:
import boto3 ec2 = boto3.client('ec2') response = ec2.terminate_instances( InstanceIds=[ 'i-057e9ad0d4b3625e3', ], )
Let’s run the code and see the result:
Move to AWS Console and you can see here the instance is stopping.
If you want to terminate the instance then you have to change ec2.stop_instances to ec2.terminate_instances.
Similarly, If someone wants to start the instance then you have to change ec2.stop_instances to ec2.start_instances in the above python script.
Congrats! 👏 We are successfully done now!
Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies.