DEV Community

Tanushree Aggarwal for AWS Community Builders

Posted on

Scheduling EC2 start and stop using Eventbridge and Lambda (part 2 of 2)

Introduction

In our previous blog, we discussed the use case and the architecture. The major steps have already been implemented, and today we will simply be tying everything together by configuring the Eventbridge schedule, that will execute the Lambda functions we created earlier.

Creating an Eventbridge Schedule

1) Login to the AWS Management Console and navigate to Amazon EventBridge

2) From the Amazon Eventbridge Get started options, select EventBridge Schedule

3) Click Create schedule

eventbridge_schedule

4) Give the schedule a name and meaningful description

event_name

5) Select Recurring schedule and Cron-based schedule for the Schedule Type

6) Enter the Cron value
cron_schedule

flex_schedule

CRONs are commonly used in Unix based systems to schedule certain jobs/scripts. A CRONjob comprises of 6 space separated fields, which define the execution time of the script.

understand_cron

cron_expression

The CRON Expression 00 17 ? * 2-6 * indicates that it will execute 0th minute of 17th hour (5PM), 2-6 day of the week, i.e. Mon-Fri.
An asterisk (*) is used as a wildcard to represent any possible value for that field
A question mark (?) indicates no specific value.

7) Select the desired Time zone

8) Select the optional values as per your use case. Click Next
We are leaving these as default.

optional_values

9) From the Target options, select All APIs

10) From the All AWS Services dropdown select Lambda
Click Next

target

11) From the Lambda API list select Invoke

lambda_API

12) For the Invoke settings, select the Lambda function Stop_EC2_instance we created in the previous blog.

13) Leave Configure version/aliases as the default values.

14) Since our Lambda function does not require any parameters/arguments, we can leave the Payload as blank.

Click Next

15) At the next screen, make sure that Enable schedule button is toggled to enabled. This button gives us the option to not enable the schedule right away. One use case can be to trigger this Eventbridge from another service based on defined conditions.

16) We have the option to execute a Delete action after the Eventbridge execution. This is outside the scope of this demo, so can leave it as blank, or select None

schedule

17) Scroll all the way to the bottom, to reach the Permissions settings.
We need to allow our Eventbridge scheduler access to the Lambda function, lets create an IAM role for this.
Select Create a new role for this schedule. A IAM role name will be assigned automatically with the relevant permissions.

Click Next

select IAM

Alternately, we can create the IAM Policy and Role from the IAM dashboard. The steps will be similar to those performed while creating the IAM Policy and Role for the Lambda.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "lambda:InvokeFunction"
            ],
            "Resource": [
                "arn:aws:lambda:eu-central-1:<AWS_Account_ID>:function:Stop_EC2_instances:*",
                "arn:aws:lambda:eu-central-1:<AWS_Account_ID>:function:Stop_EC2_instances"
            ]
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

18) Review all the configurations and click Create Schedule

review

create_schedule

Tada! Your Eventbridge scheduler is ready!

eventbridge_configured

Create Eventbridge schedule for the starting the EC2 instances

Repeat the above steps to create another Eventbridge scheduler to Start the EC2 instances, configuring the Start_EC2_instances Lambda function.

The CRON entry for 08AM every weekday will be as follows:

start_cron

start_EC2

Verification

We are all set! Our final step is to verify if our Eventbridge schedulers triggered as per schedule and if they were actually able to stop/start our EC2 instances!

Cloudwatch metrics indicating that our Start_EC2_instances Lambda function was invoked at 08:00AM.
lambda_invocation

EC2 instances are in running state:

running_ec2_instance

Conclusion

In this two-part blog we covered a range of AWS services. We understood how different services can be used in conjunction with each other to create unique set of actions.
I hope this blog has ignited some level of curiosity in your mind, for AWS and cloud architecture in general!

If you read till the end, Thank you!

Watch out this space for more such articles. Do like and comment if you found this useful! Your feedback is welcome in the comment section.

Top comments (0)