Scheduling EC2 start and stop using Eventbridge and Lambda (part 2 of 2)
Tags: aws, serverless, lambda, cloud
Published: 2023-10-31 13:33:07

## Introduction
In our previous [blog](https://dev.to/aws-builders/scheduling-ec2-start-and-stop-using-eventbridge-and-lambda-part-1-of-2-30he), 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](https://aws.amazon.com/console/) and navigate to `Amazon EventBridge`

2) From the Amazon Eventbridge _Get started_ options, select `EventBridge Schedule` 

3) Click `Create schedule`

![eventbridge_schedule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6amqtbv5v6tmf3r7gyf6.png)

4) Give the schedule a `name` and `meaningful description`

![event_name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3join4efiy1vi1dxl5yj.png)

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

6) Enter the `Cron` value
![cron_schedule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ehn629md0glsri97rmp.png)

![flex_schedule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbtr0y1ij0tce9q5yp01.png)

> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7r8vmpji234gsum2e3ix.png)

![cron_expression](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sml7vtsvvhp5i6k7em2w.png)
> 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/va4mouml9s3u2td3e8bv.png)


9) From the `Target` options, select `All APIs`

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

![target](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8fatlyguey8m5nw57xw5.png)

11) From the Lambda API list select `Invoke`

![lambda_API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tgxfgonmryxw2xj6vhp.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhp6kubn79mqy7wjxv29.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6bfpy8icjimz4jh1lgx.png)

> 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"
            ]
        }
    ]
}
```

18) Review all the configurations and click `Create Schedule`

![review](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5quvl5yxfvukvm9jdyg6.png)


![create_schedule](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cncwpyqkacusi9mxh80.png)


**Tada! Your Eventbridge scheduler is ready!**

![eventbridge_configured](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0y062m6z7g92lx89jt9x.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omkywfb3k69yxn4qczp1.png)


![start_EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8sico56txudiragisxu5.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5tuwpyawu83cmoje8s2.png)

EC2 instances are in `running` state:

![running_ec2_instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmsmyqkz3trxgq2cgdvt.png)




## 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._