DEV Community

Cover image for Easy setup for EC2 stop jobs with Amazon EventBridge Scheduler
Fumiaki Ueno for AWS Community Builders

Posted on

Easy setup for EC2 stop jobs with Amazon EventBridge Scheduler

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

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:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "scheduler.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode
  • IAM Policy:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ec2Stop",
            "Effect": "Allow",
            "Action": "ec2:StopInstances",
            "Resource": "*"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Image description

2. Create a Schedule

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

Image description

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

Image description

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

Image description

Select Amazon EC2 and select stopInstances action.

Image description

Image description

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

Image description

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

Image description

Review settings and create schedule.

Image description

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

Image description

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 and AWS Blog post.

Oldest comments (0)