Easy setup for EC2 stop jobs with Amazon EventBridge Scheduler
Tags: aws, eventbridge, ec2
Published: 2022-11-14 15:20:06


A great update **Amazon EventBridge Scheduler** announced!

https://aws.amazon.com/jp/blogs/compute/introducing-amazon-eventbridge-scheduler/

## What is Amazon EventBridge Scheduler?

EventBridge Scheduler is a more powerful scheduling feature than the previous EventBridge rules.EventBridge Scheduler has the following characteristics.

* You can create up to 1 million schedules per account
* Support 1,000s of TPS
* Support many AWS Service APIs(Over 270 services and over 6,000 API Actions)
* You can create one-time schedules

Supporting many APIs means we can create many automated jobs without Lambda, code, etc, just with EventBridge Scheduler. 

## EC2 stop jobs with Amazon EventBridge Scheduler

[Instance Scheduler](https://aws.amazon.com/jp/solutions/implementations/instance-scheduler-on-aws/) was commonly used for stopping EC2 instances.It consists of Lambda and EventBridge rules.

Now it can be created only by EventBridge Scheduler. So let's create it.

A running instance was prepared in advance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gd5snwoagwl1twzbf7ie.png)


### 1. Create an IAM Role

We need to create an IAM Role for EventBridge Scheduler. In this case, Scheduler only stop EC2 instances, so we grant `ec2:StopInstances` action. Create the following IAM Role.

* IAM Role Name: `SchedulerEC2Stop`
* Trusted Policy:

```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "scheduler.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
```
* IAM Policy:
```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ec2Stop",
            "Effect": "Allow",
            "Action": "ec2:StopInstances",
            "Resource": "*"
        }
    ]
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d54ho0tiwxpabpyn1ks5.png)

### 2. Create a Schedule

Moving to EventBridge Schedules page and create schedule. (Not a rules page.)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gfojn6fsbve6af2vafr.png)

Set a schedule name and configure the schedule.If you want to stop instances daily 21:00, set as follows.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b95bjz134p0qch0u5z68.png)

You can optionally set Timezone, Start date, End date.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh4a2yw2i2jmkz5lu0ng.png)

Select Amazon EC2 and select `stopInstances` action.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejnlzz3jw6couacl3wmx.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxaqbwk5pqm7il29mckb.png)


You only need to enter your instance ID. Multiple IDs can also be specified.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tesn79ik68zkimyhf2kq.png)

In the Settings phase, only an IAM role need to be set.　In this case, other settings are OK with default values.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdjhi6mvfxo5eaike6pi.png)

Review settings and create schedule.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvvomtppmt18zeb9u49y.png)

### 3. Check EC2 instance is stopped.

Now it's ready. When the scheduled time comes, you can confirm that EC2 instance is stopped.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bv0rs08eoxk81x3z7eq.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkqvrkc8lhe9noh5ssd5.png)

## conclusion

We could set up EC2 stopping job easily.　This feature can be applied to implement operational jobs such as ECS RunTask　and changing alarm configuration of CloudWatch without programming code.

If you want to know more detail of EventBridge Scheduler, Check the [AWS Documents](https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html) and [AWS Blog post](https://aws.amazon.com/jp/blogs/compute/introducing-amazon-eventbridge-scheduler/).