AWS EC2 - How To Automate Stop/Start By Id, Instant Type Or Platform
Tags: aws, devops, cloud
Published: 2021-08-12 22:29:17

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](https://github.com/hseera/aws-python-utilities/blob/main/stop_start_ec2.py)_** 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
```

## 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}
```

Example
```
./stop_start_ec2.py stop type t2.micro
./stop_start_ec2.py start type m5.4xlarge
```


## 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
```

For Linux/Unix
```
./stop_start_ec2.py stop platform other
./stop_start_ec2.py start platform other
```


## 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}
```

For Linux/Unix
```
./stop_start_ec2.py stop other {InstanceType}
./stop_start_ec2.py start other {InstanceType}
```

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
```

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](https://www.linkedin.com/in/harinderseera/) | [Twitter](https://twitter.com/harinderseera) | [Polywork](https://www.polywork.com/harinderseera) 

Blogging: [Dev](https://dev.to/harinderseera) | [Hashnode](https://harinderseera.hashnode.dev/)

Github: [hseera](https://github.com/hseera)