What does fear breed? Fear breeds anger, intimidation, and confusion. We often fear the things we’re not familiar with and technology or software is no different when you’ve never used it before. That’s the feeling I’ve had since studying Python over the last couple of weeks at Level Up In Tech. Like every horror movie, the protagonist faces down the monster and takes fear head on and that’s what we’re going to do with this project. Follow me as I learn how to use the Python SDK(Software Development Kit) known as Boto3 to develop a script to stop 3 EC2 instances. Let’s work!

Scenario/Objective: Our DevOps engineering team often uses a development lab to test releases of our application. The Managers are complaining about the rising cost of our development lab and need to save money by stopping our 3 ec2 instances after all engineers are clocked out.

Prerequisites/What We’ll Need:

AWS Account

Python Knowledge

IDE(Integrated Development Environment)/AWS Cloud 9

Determination & Perseverance ALWAYS

Step 1:

To start, we want to open up Cloud 9 in our AWS Management Console and that can be done through our search bar or our “recently visited” section of our console. Once there if you haven’t installed boto3 or AWS CLI you’ll need to in order to make sure you have the correct commands for the project. I already installed both prior to this project but you use:

pip install boto3
pip install awscli
Use these two install packages to start off with for the project.

Step 2: We need the correct template and we do that by selecting the “File” option in the upper left-hand corner of Cloud9 and selecting “New From Template” and selecting our template which is “Python File”.

Step 3: Once you’ve selected your template select “Save As” and name your file(I named mine “Week14Project.py) and let’s get started writing the code for our script. To start the script you want to start with “#!/usr/bin/env python3.7” on line 1. Next we enter “import boto3” which imports the boto3 library for our code and that will look like this:

import boto3

While we can create an ec2 instance by going through the normal method of launching it, naming it, giving it an instance type, etc. with the magic of python, we can automate this through our script. Before we run the script we want to make sure we grab our “AMI ID” for our instance.

Our AMI ID is highlighted.

With our AMI ID copied we add it to our script which looks like this:

Our script for launching our 3 instances

When we run our script it will provide us with our 3 unique ec2 instances.

The 3 instances we created

To provide what that would look in AWS like let’s head over to EC2 and identify the instances printed in Cloud9. I was able to identify them by the last two digits.

Step 4: Let’s say we want to stop our instances what would that look like? How we would write the code for it? We would simply write the code as:

ec2.Instance('i-06ee3df3958780598').stop()
ec2.Instance('i-06663687c94affc1a').stop()
ec2.Instance('i-094209236835c2c7b').stop()

When we add our code to stop our instances to the script. Make sure you “disable” or mark out the code used to launch instances otherwise you’ll be launching instances you don’t need for the project.

We can simply mark out the code by using hashtags

Once we run the script we head back over to EC2 to check and make sure that the instances are shutting down and stopping.

Success! We have our stopped instances

Step 5: Let’s dive a little deeper to provide a more unique scenario. Let’s say our instances have tags on them and we want to stop just those tagged instances. We start this by adding a key( I used Dev Team) and a value( ‘dev’) and although it says optional for value make sure you add one anyway. An example of that looks like this:

This can be achieved by selecting the instance and selecting “tag” and “manage tag”.

We would add tags to all 3 instances and then add the necessary code needed to stop our tagged instances. The code for that would look like this:

ec2_filter=[{'Name': 'tag:Dev Team', 'Values': ['dev']}, {'Name': 'instance-state-name', 'Values': ['running']}]

ec2.instances.filter(Filters=ec2_filter).stop()

Before running the code make sure you turn your 3 instances back on

Our running instances

Let’s now run our code(The same rules apply, mark out or disable code not needed for this portion of our project):

We’ve stopped our instances again this time with the selected tags!

Final thoughts: This project definitely challenged me as I got to combine both AWS and python together for it. I attempted to run the stop instance part of my project and ended up creating far more instances than I needed when I just only had to mark out my code in order for it to work. With using python you have to be really thorough with your code and check double-triple check it before running it 😂. I hope you liked my project and found it informative. Feedback is always welcomed.

And REMEMBER…..

Never give into fear.

--

--

Follow my journey from Tech Support to Cloud DevOps Engineer!

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store