DEV Community

Cover image for AWS EC2 - How To Automate Stop/Start By Id, Instant Type Or Platform
Harinder Seera 🇭🇲 for AWS Community Builders

Posted on • Edited on

AWS EC2 - How To Automate Stop/Start By Id, Instant Type Or Platform

Amazon EC2 (Elastic Compute Cloud) is a cloud service that lets you create virtual machines (instances) and run them online. You can do so by using the AWS Dashboard or by using a programming language supported by AWS. For example, Python.

This article is about the Python programming language and its SDK, Boto3. The SDK allows you to programmatically interact with AWS services. Which is great, but every now and then you want the flexibility to do things your own way, which can create some difficulties.

Boto3, for example, provides functions for dynamically starting and stopping an EC2 instance. However, you must provide the EC2 instance id. That is your only choice. There is no ability to start or stop an EC2 instance based on the instance type, platform, or existence of a specific EC2 tag.

Assume you have many EC2 instances running in your test/production environment, each with its own instance type and platform, such as t2.micro, t4g.2xlarge, c4.2xlarge, and r5.4xlarge. You merely want to stop all c4.2xlarge instances for the time being, or you only want to stop instances running Windows. Or maybe instances running Windows and the t4g.2xlarge instance type.

How can you complete this task with only an instance id? I wrote a Python script to help me with it. The current version of the script allows you to stop and/or start EC2 instances based on the following conditions:

  • All EC2 instances (default behavior)
  • By Instance Type
  • By Platform Type
  • By Platform & Instance Type

All EC2 Instances

To stop or start all EC2 instances, the following are the commands.

./stop_start_ec2.py stop id
./stop_start_ec2.py start id
Enter fullscreen mode Exit fullscreen mode

By Instance Type

To stop or start all EC2 instances based on a specific instance type, the following are the commands.

./stop_start_ec2.py stop type {instanceType}
./stop_start_ec2.py start type {instanceType}
Enter fullscreen mode Exit fullscreen mode

Example

./stop_start_ec2.py stop type t2.micro
./stop_start_ec2.py start type m5.4xlarge
Enter fullscreen mode Exit fullscreen mode

By Platform Type

To stop or start all EC2 instances based on platform type (windows or other), you pass in the following arguments to the script.

For windows

./stop_start_ec2.py stop platform windows
./stop_start_ec2.py start platform windows
Enter fullscreen mode Exit fullscreen mode

For Linux/Unix

./stop_start_ec2.py stop platform other
./stop_start_ec2.py start platform other
Enter fullscreen mode Exit fullscreen mode

By Platform & Instance Type

To stop or start all EC2 instances based on platform & instance type, you pass in the following arguments to the script.

For windows

./stop_start_ec2.py stop windows {InstanceType}
./stop_start_ec2.py start windows {InstanceType}
Enter fullscreen mode Exit fullscreen mode

For Linux/Unix

./stop_start_ec2.py stop other {InstanceType}
./stop_start_ec2.py start other {InstanceType}
Enter fullscreen mode Exit fullscreen mode

Example

./stop_start_ec2.py stop windows t2.micro
./stop_start_ec2.py start other t2.micro
./stop_start_ec2.py start windows t2.small
./stop_start_ec2.py stop other c4.2xlarge
Enter fullscreen mode Exit fullscreen mode

Hopefully, this script will come in handy to you in the aforementioned case. Keep an eye out for the script updates as I add new features. Contact me if you would like to contribute to the code. Also, leave a comment or a star on the git repo if you find the script useful.

Note: Before using the script in production, try it in your development/test environment first.


Thanks for reading!

If you enjoyed this article feel free to share it on social media 🙂

Say Hello on: Linkedin | Twitter | Polywork

Blogging: Dev | Hashnode

Github: hseera

Top comments (1)

Collapse
 
atsag profile image
Andreas

Thank you for your article! It would be nice though to explain that id is the instance type (not instance id) and also to add details on the preliminary configuration which is necessary (access key/secret key, possibly iam role etc.)