How to Auto Create, Terminate, Stop and Start EC2 instance using Python3 with Boto3

Sabina Akter
5 min readDec 10, 2022

--

Hello Everyone, Welcome to my MediumPage!! In this article, I will go over how to Create, Terminate, Stop and Start EC2 instance using Python3 with Boto3. And also how to automate the process.

Boto3 is the name of the Python SDK for AWS. It allows you to directly create, update, and delete AWS resources from your Python scripts.

Let’s get started!

Prerequisites:

  • I used VSCode, but you can use any text editor you prefer
  • AWS Management Console, you should have an aws workspace account
  • python3 installed in your local machine
  • BOTO3 installed:
pip3 install boto3
I already installed, boto3, so it shows requirement satisfied

Cool!! Now Boto3 has been successfully installed 🆒.

  • Install AWS CLI and Configure it. If you want to check if you have configured it or not, you can check it like this.

Step-1 | Let’s create our script that will autogenerate our AWS instances:

I have created a folder name, Python and created a file name app.py using cli:

cd Desktop/ # navigate to the desktop

mkdir python # a folder will create

touch app.py # a python file will create

Now put the below scripts inside app.py

EC2 instance create

In the above script we have the following lines:

  • Import boto3 : It imports boto3.
  • ec2 = boto3.resource(‘ec2’) : The resource or service, here we are using boto3 for 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 us-east-1 (N. Virginia Region).
getting AMI id from EC2 instance
  • MinCount : This specifies the minimum number of EC2 instances you want to create. You can also change it if you want.
  • MaxCount : This specifies the maximum number of EC2 instances you want to create. You can also change it if you want.
  • InstanceType: This specifies the type of the EC2 instance you want to create. Here I choose t2 micro as its free on aws free tier 😃

Now the scripts in app.py file will create EC2 instances in your aws. Once you log in aws console and look at the EC2 dashboard, you will see 3 EC2 instances are created. We can also verify this by switching over to the AWS Management Console:

3 EC2 has been created in aws

Step-2 | Let’s create a script to automate the termination/Stop/Start our instances:

1. Let’s see, how to terminate EC2 instance:

  • First uncomment lines from 7 to 27 (see the attached screenshot)
  • Then look at the script for terminate instance ( see the attached screenshot for the scripts):

ec2.instances.filter(InstanceIds=[“i-0b10a6d1fd76e3d2c”, “i-0df5b736a602f3298”]).terminate()

print(“Instances terminate successfully”)

Terminate 2 EC2 instances

Now, run the scripts using cli :

python3 app.py

Go to the aws management console and look at the EC2’s, you will see 2 instances terminated. That’s how we can automate the termination process of unused EC2 to minimize aws EC2 instance running cost.

2 EC2 terminated and 1 instance running

2. Now, we will see how to stop running EC2 instance:

  • uncomment lines from 7 to 31 (see the attached screenshot for the scripts)
  • Then look at the script for terminate instance:

ec2.instances.filter(InstanceIds=[“i-01a7d9857d956f926”]).stop()

print(“Instances stopped successfully”)

Stopped EC2 instance

Run the scripts using cli :

python3 app.py

Go to the aws management console and look at the EC2’s, you will see the only one running instance stopped. That’s how we can automatically stop the EC2 instance as we don’t want to run it but can use later, to minimize aws EC2 instance running cost.

EC2 instance stopping

3. Now, we will see how to start stopped EC2 instance:

  • uncomment lines from 7 to 35 see the scripts
  • Then look at the script for terminate instance (see the attached screenshot for the scripts):
Started EC2 instance from stop status

ec2.instances.filter(InstanceIds=[“i-01a7d9857d956f926”]).stop()

print(“Instances started successfully”)

Run the scripts using cli :

python3 app.py

Go to the aws management console and look at the EC2’s, you will see the only one stopped instance started pending first and then started running.

EC2 instance pending status
EC2 instance started running

Congratulations!! You have successfully Create, Terminate, Stop and Start EC2 instance using Python3 with Boto3. 🆒

Error while working on this project:

  • When I run aws configure on the VSCode cli, it had my other aws credentials by default
  • Solution: I have deleted that credentials as I don’t use that account anymore and created a new aws access key, uninstall aws toolkit and reinstall aws toolkit but I was not able to connect the aws. Finally after doing little research, I had to reload the VSCode windows to fix the issue.

Thank you everyone 😃, for reading my blog!!

Stay Tuned for my next project!!

👏👏👏👏 Happy Learning!!

If you think, this article is helpful 😄, please give me a clap 👏👏 to inspire me.

Please connect me on linkedin 👎

https://www.linkedin.com/in/aktersabina122/

Please follow me on Medium and Twitter 👎:

https://aktersabina122.medium.com/

https://twitter.com/aktersabina122

References:

https://blog.knoldus.com/how-to-create-ec2-instance-using-python3-with-boto3/

https://realpython.com/python-boto3-aws-s3/

https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/setup-credentials.html

--

--

Sabina Akter
Sabina Akter

Written by Sabina Akter

Infrastructure Engineer/Cloud Engineer at Fox

No responses yet