Stopping AWS EC2 Instances via Python & Boto3 in Cloud9
In this article I will discuss how to terminate multiple EC2 Instances at a time using a Python script with Boto3 SDK in AWS Cloud9 IDE.
What you’ll need?: An AWS account with administrative permissions, An Integrated Development Environment(I’ll use AWS Cloud9), and a GitHub Account. But before we get started…
What is Python? Python just so happens to be one of the most popular programming languages in Information Technology. The human readability makes it one of the easiest languages to learn. It is general purpose so it can be used to design and array of applications, including data science, software and web development, automation, and simple scripting.
What is Boto3? Boto3 is the Python Software Development Kit (SDK) for Amazon Web Services (AWS). It allows you to make Application Programming Interface (API) calls directly to AWS services from the Python scripts.
What is AWS Cloud9? According to Amazon Web Services official documentation “AWS Cloud9 is a cloud-based integrated development environment (IDE) that lets you write, run, and debug your code with just a browser. It includes a code editor, debugger, and terminal. Cloud9 comes prepackaged with essential tools for popular programming languages, including JavaScript, Python, PHP, and more, so you don’t need to install files or configure your development machine to start new projects.”
LET’S GET STARTED:
Step 1: Log into AWS Account and Create a Cloud9 Environment.
- In AWS Console head over Cloud9.
- Name the environment and give a Description (Optional).
- For Environment settings leave all defaults (defaults are within free tier limits.) Click “Create environment” once you’re finished reviewing the summary.
- Open IDE to install packages.
Step 2: Install necessary packages.
pip3 install boto3
pip3 install aws cli
Step 3: Create an IAM Role (with Administrative Permissions) to attach the Cloud9 Instance.
- Select the Cloud9 EC2 Instance.
- Click Actions > Security >Modify IAM role
Step 4: Configure AWS CLI (must have credentials for user with programmatic access)
aws configure (input Access Key, Secret Access Key, and default region.)
Step 5: Clone Git repository. More information on how to clone git repository in my pervious publication (Source Control with Git. | by Sheniell Sinclair | Medium) And configure git credentials.
git clone (https link to GitHub repo)git config --global user.email “email for associated git account”git config --global user.name “username for git account”git config --global credential.helper store
Step 6: Create Python file in Cloud9 IDE in git directory (don’t forget the .py or the file will not compile correctly)
cd (git directory) #changes to git repository directorygit branch temp #creates temporary branchtouch AWS_EC2Stop.py #creates file
Step 7: Create python script to terminate running instances (NOTE: DO NOT STOP CLOUD 9 INSTANCE.)
GitHub Link to Python Script: LevelUpWithPython/AWS_EC2Stop.py at main · ShenSinclair/LevelUpWithPython (github.com)
Step 8: Save file and push Python Script to GitHub (You will need username and personal access token. More on this in previous publication: Phase 4, Source Control with Git. | by Sheniell Sinclair | Medium)
git add . #add file to staginggit status #status of staginggit commit -m "commit message" #commit file in staginggit push #push from local repository to remote repository
FINAL STAGE: Merge temp branch to main in remote GitHub
We’ve successfully stopped the specified EC2 Instances using the Python script we created.
THANKS FOR READING!
Connect with me on Linked In:
www.linkedin.com/in/sheniell-sinclair
Link to GitHub:
ShenSinclair/LevelUpWithPython: Python repo for Level Up In Tech Learning. (github.com)