AWS lambda function and CloudWatch to schedule for start and stop of the server-EC2 instance-Part 2
Tags: aws, cloudnative, community, communitybuilder
Published: 2023-01-05 18:17:53

In the first part of the article, we manually launched an instance using the lambda function without going to the instance console. In this part, we will stop the EC2 instance and automatically stop and start the EC2 instance by setting a scheduler using AWS CloudWatch.

- We have one function that created previously to start EC2 instance (StartEC2Instance), and now to create another lambda function to stop the EC2 instance, click on **create function.**


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

**1.** Choose **Author from scratch.**
**2.** For **Function name**: enter a name that identifies it as the function used to stop your EC2 instances.
**3.** For **Runtime**: here choose Python 3.9.


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

**4.** Under Permissions: expand Change default execution role.
**5.** Under Execution role: choose Use an existing role, the choose the IAM role that you created.
**6.** Then click on **Create function**.


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

- Our lambda function for stop instance is Successfully created.

And again, from AWS documentation of stop and start Amazon EC2 instances at regular intervals using Lambda:

https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambdaeventbridge/


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

Under **Code**, **Code source**, copy and paste the code into the editor pane in the code editor **( lambda_function)**. This code stop the EC2 instances that you identify.


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


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

To ensure that the code is executed successfully:
We go to the instance and do a refresh, and notice that the instance is stopped.

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

- In CloudWatch, we notice that the log for the start and stop instance are recorded in **log groups**.

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



**CloudWatch to Scheduling start and stop the instance:**

We can automate the operation of start and stop instances by scheduling the time of start and stop the instance by the AWS CloudWatch and the events of the AWS lambda function, the CloudWatch rule can trigger multiple kinds of things based on certain rules or conditions and what we are going to use the CloudWatch rules for is to set up a cron at which a specific time will trigger a specific lambda function.

**The steps of Scheduling:**
1. Click on the **Rules** in the left navigation pane.
**2.** click on **Create rule**.

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

**3.** Use the **schedule** option
**4.** Choose Cron expression .
**5.** Enter the time for start or (stope the server-instance).
**6.** Choose the target to invoke an event (here Lambda function 
        is our target).
**7.** Choose the lambda function.
**8.** Click on **Configure details**.


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

In the next step, to configure the rule details:
**9.** Give a name to the rule.
**10.** Give a description to the rule (optional).
**11.** Click on **Create rule**.


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

The rule for starting instance is successfully created.

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

And the instance will run at the time we specified.

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


like the same steps, we can make a schedule to stop instances by choosing the stop instance event of the lambda function.

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

The rule for stopping instance is successfully created.

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

And the instance will stop at the time we specified.

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