Stop and Start instances using the AWS Instance Scheduler - STOPINATOR
Tags: webdev, devops, automation, aws
Published: 2022-02-11 11:54:04

## DAY 6 - STOPINATOR- Start and Stop EC2 at predefined times 🔴🟢

☁️[100 days of Cloud- **Day Six**](https://github.com/anuvindhs/100daysofcloud)
 ✅[Follow Me on Twitter](https://twitter.com/anuvindhs)


![Cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf1v28lcll27cqph44tv.gif)

[Tweet This Blog](https://link.anuvindh.com/twitter/DAY-6-ec2-stopinatortweet.html)  - [Read on GitHub](https://github.com/anuvindhs/100daysofcloud) - [Read On iCTPro.co.nz](https://ictpro.co.nz)

-----
## Stopping and starting  EC2 instances automatically
Using Instance scheduler you can start and stop instance automatically using Cloud formation

### Prerequisites
- Install [Scheduler CLI](https://s3.amazonaws.com/solutions-reference/aws-instance-scheduler/latest/scheduler-cli.zip)
-  Python 2 installed on instance

#### Installing Scheduler CLI

```bash
curl "https://s3.amazonaws.com/solutions-reference/aws-instance-scheduler/latest/scheduler-cli.zip" -o "scheduler-cli.zip"
``` 

Unzip
```bash
unzip scheduler-cli.zip
```
Install
```bash
sudo python setup.py install
```
Check 
```bash
scheduler-cli --version
```
### Creating AWS CloudFormation Stack
[Launch Solution](https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/new?templateURL=https:%2F%2Fs3.amazonaws.com%2Fsolutions-reference%2Faws-instance-scheduler%2Flatest%2Faws-instance-scheduler.template) , or download [template](https://s3.amazonaws.com/solutions-reference/aws-instance-scheduler/latest/aws-instance-scheduler.template)

| Comments  | ScreenShots  |
| ------------ | ------------ |
| Click on to Launch Solution  | ![Stack launch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l59cv556gd3cwjcg0eab.png)   |
|Enter Stack Name|![Image StackName](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m525sajallefx26p3leb.png)|
|Choose your frequency (in Mins)|![Image frequency](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f5g771s7rsifgdoqgsz.png)|
|Enable CloudWatch Logs|![Image Cloudwatchlogs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsl1c5ub4cv0ov6suhj8.png)|
|Start & Stop tag|![Image startandstop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1pms330860r3uv8vivj.png)|
|keep everything default and acknowledge then click create stack |![Image Stack creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lfrou8hf5wxv0l057vq.png)|

### Creating Start and Stop Period

Go to instance and paste this script

```bash
scheduler-cli create-period --stack Ec2instanceScheduler --region ap-southeast-2 --name mon-fri-9-5 --begintime 9:00 --endtime 16:59 --weekdays mon-fri
```
Here i am setting up to start time  9 am and stop time 4.59 Pm  from Monday to Friday.

Now go to AWS Console and open the [DynamoDB](https://console.aws.amazon.com/dynamodb/).

Click on to **Tables** and choose the **config table** .
Then Choose the **Items** , and check for **QuickPeriod** appears under Name.

### Creating Schedule for the period
Each schedule must contain at least one schedule period, it defines the time instance should run.

SSH into ec2
```bash
scheduler-cli create-schedule --stack Ec2Stopinator --name QuickSchedule --region ap-southeast-2 --periods QuickPeriod --timezone UTC+1300

```
goto  the [DynamoDB](https://console.aws.amazon.com/dynamodb/).

Click on to **Tables** and choose the **config table** .
Then Choose the **Items** , and check for **QuickSchedule** appears under Name.


### Tagging the Schedule

Now go to ec2 and select **Manage tags**
Key = **Schedule**
Value = **QuickSchedule**


🎉 **Congratulations** 🎉 you have successfully configured **STOPINATOR**









{% user anuvindhs %}