Setting Up Schedules with AWS EventBridge Scheduler
Serverless scheduler for creating and managing schedules
What is Amazon EventBridge Scheduler
Amazon EventBridge Scheduler is a serverless scheduler that allows you to create, run, and manage tasks from one central, managed service. Highly scalable, EventBridge Scheduler allows you to schedule millions of tasks that can invoke more than 270 AWS services and over 6,000 API operations. Without the need to provision and manage infrastructure, or integrate with multiple services, EventBridge Scheduler provides you with the ability to deliver schedules at scale and reduce maintenance costs.
Working of Amazon EventBridge Scheduler
In our case, EventBridge Scheduler Target is invokation of Lambda.
Types of schedule
You can define a one-time or recurrent schedule in the Amazon EventBridge Scheduler
You can create your schedule. A one-time schedule invokes its target only once at the date, time, and time zone that you provide. A recurring schedule invokes its target based on the rate expression, or cron expression that you provide when you configure the schedule.
One-time schedule
Recurring schedule
Setup
In this demo, we will set up how to automatically start and stop (Amazon EC2) instances using AWS Eventbridge Rule with AWS Lambda (Target of Scheduler) to reduce Amazon EC2 usage.
Steps
Create the Lamba Function to stop and start instances
Code the Python
Test the code
Update the IAM Permission of AWS Lambda
Test the code.
Create a Schedule
Repeat the process 3 to 7 to create another Lambda to start
Create a Lambda function
Setting up Lambda for stoping instance
we first need to create the Lambda function because we will invoke a Lambda function from AWS Eventbridge Schedular as Target,
so we first need to create a Lambda function to stop the EC2 instance
Create a new function at console.aws.amazon.com/lambda/
Paste the following code, make sure to change the region and instance IDs
import boto3
region = 'rgion-name'
instances = ['instances-id', 'instances-id']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))
Test the Lambda Function by pressing the Test button, It will give an Error because the lambda Function needs EC2 IAM Role Permission to make a change in EC2 instances
Update the IAM Permission of AWS Lambda
Editing role of AWS Lambda default role to give Access to EC2, clicking on Role link will redirect you to this role's permissions
on the permission tab click on add permission and attach ---> inline policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
}
]
}
Now we have given the AWS Lambda function to stop and start EC2 instances by updating the role stopinstances-role-q3tfohe6
attached to the Lambda function,
Test the Lambda
We have the correct permission for Lambda, Let test this
Create a schedule
We have configured the Lambda for stopping instances, now create a schedule to do it automatically whenever you want, whether one-time or Recurring base
open scheduler console console.aws.amazon.com/scheduler/home
Create a Schedule for running it on Schedule on Recurring base a 19 on Monday to Friday
Paste the following cronjob 0 19 ? mon-fri
, and press next
Enable Schedule and set NONE to action after completion also turn off retry policy
Note: when setting EventBridge Target to AWS Lambda you are also give it access to invoke Lambda
Verifying your Eventbridge Scheduler
There must be a better way to investigate the result of Eventbridge Schedular, but in our case, we are verifying it through AWS cloud watch logs
AWS Cloudwatch ---> Logs Groups --> Select the logs of that time
Repeat the Process for starting,
Repeat the Process by changing Lambda Function to startec2instances
and create another schedule cron expression according to your desire time.
import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))
Additional Concept
Time window allows you to start a schedule within a window of time. This means that the scheduled tasks are dispersed across the time window to reduce the impact of multiple requests on downstream services.
The maximum retention time of the event is the maximum time to keep an unprocessed event in the scheduler. If the target is not responding during this time, the event is dropped or sent to a DLQ.
Retries with exponential backoff help to retry a failed task with delayed attempts. This improves the success of the task when the target is available.
Additional cronjobs Example
The following examples show cron expressions that you can enter into the console for your build schedule. UTC time is specified using a 24-hour clock.
Run daily at 10:00 AM (UTC) 0 10 * * ? *
Run daily at 12:15 PM (UTC) 15 12 * * ? *
Run daily at midnight (UTC) 0 0 * * ? *
Run at 10:00 AM (UTC) every weekday morning 0 10 ? * 2-6 *
Run at 6 PM (UTC) every weekday evening 0 18 ? * mon-fri *
Run at 8:00 AM (UTC) on the first day of every month 0 8 1 * ? *
Run on the second Tuesday of every month at 10:30 PM (UTC) 30 22 ? * tue#2 *
Additional Resource
Read more about How does EventBridge Scheduler work?, When to use EventBridge Scheduler?
https://aws.amazon.com/blogs/compute/introducing-amazon-eventbridge-scheduler/