Introduction
Hello, in this project, I will be going over how I created a Python Script that can be used to stop running instances with a specific tag.
Scenario: A DevOps engineering team uses a development lab (environment) to test releases of an application. The managers have noticed and complained about the rising cost of the development lab and the need to cut down on costs by stopping the EC2 instances when not in use.
In order to achieve this, a Python script needs to be created that can be run to stop the necessary instances. These instances have a tag with a Key of Environment and a Value of Dev. (See the picture below of the instance with the appropriate tag)
Pre-Requisites
Please note that I created and ran this Python Script in AWS Cloud9 which is a cloud-based integrated development environment (IDE) that one can use to write, run, and debug your code from a browser.
Cloud9 is set up with an IAM role which grants it the necessary permissions and allows the Python script to perform actions on services/resources in your AWS account.
Solution
The Python script I created is shown below. It can also be found on my Github repository here
In order to create this script, I broke the problem down into 3 steps:
- Find out how many instances were running and had the needed tag
- Store the Instance IDs of these instances
- Stop the instances using the Instance IDs
The script was developed by doing the following:
import boto3
: This imports the boto3 library. boto3 is the AWS SDK for Python, making integrating Python applications, libraries, and scripts with AWS services easy.ec2 = boto3.client(‘ec2’)
: This creates an EC2 client object using the boto3 library. It allows you to perform various operations with instances and any other EC2 services.dev_tag = {“Key”: “Environment”, “Value”: “Dev”}
: This defines a dictionary with the desired tag needed to filter instances.instances_running = []
: This creates an empty list called ‘instances_running’. It will be used later on to store the Instance IDs of the instances that need to be stopped.
NOTE: To complete this project and get the necessary commands, I used the EC2 boto3 documentation which can be found here.
response = ec2.describe_instances(Filters=[ {‘Name’: ‘instance-state-name’, ‘Values’: [‘running’]}])
: This command is used to describe instances. A filter is added to make sure that only the “running” instances are saved in the “response” variable. The response variable is a dictionary that nests lists and dictionaries which need to be parsed through to get specific information. The for loops seen in the code below were used to do that.
In order to check if the instance has values that match the value of the variable, dev_tag, the following commands were run below. When a match was found, the Instance ID was added to the empty list created earlier using the .append() method.
for reservation in response["Reservations"]:
for instance in reservation["Instances"]:
# Check if the 'Tags' key exists in the instance metadata
if "Tags" in instance.keys():
# Check if the desired 'Environment' tag is present
if dev_tag in instance['Tags']:
# Append the instance ID to the list
instances_running.append(instance["InstanceId"])
- The last part of the project is to stop the instances using values stored in the list, instances_running.
In case, there are no running instances, an if function was used to check if the list, instances_running is empty. If it is, a prompt stating “No running instances to be stopped is shown”.
if instances_running == []:
print('No running instances to be stopped')
else:
# Stop the running instances using their IDs
ec2.stop_instances(InstanceIds=instances_running)
print("All running instances stopped")
If instances_running is not empty, the instances in the list were stopped using ec2.stop_instances().
Since this script listed above only works for the instances that have a tag with the values stated in the dev_tag variable, it won’t shut down any other instances.
I have included another script below that will shut down ALL running instances should you need it. The script can also be found on my Github Repository here.
NOTE: There is a change to the code to remove the EC2 instance that AWS Cloud9 is running on. This prevents your Cloud9 environment from stopping when you run this script.
Thank you for making it this far. Happy learning!