As per AWS “AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume.
With Lambda, you can run code for virtually any type of application or backend service – all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.
As per AWS “Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.”
Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction. It provides you with complete control of your computing resources and lets you run on Amazon’s proven computing environment.
Note: For region, replace “us-west-1” with the AWS Region that your instances are in. For instances, replace the example EC2 instance IDs with the IDs of the specific instances that you want to stop and start.
Under Basic settings, set Timeout to 10 seconds.
Note: Configure the Lambda function settings as needed for your use case. For example, if you want to stop and start multiple instances, you might need a different value for Timeout, as well as Memory.
3. Test your Lambda functions
In the Lambda console, choose Functions.
Select one of the functions that you created.
Choose Actions, and then choose Test.
In the Configure test event dialog, choose Create new test event.
Enter an Event name, and then choose Create.
You don’t need to change the JSON code for the test event—the function doesn’t use it.
Choose Test to execute the function.
4. Create rules that trigger your Lambda functions
Open the CloudWatch console.
In the left navigation pane, under Events, choose Rules.
Choose Create rule.
Under Event Source, choose Schedule.
Do either of the following:
For Fixed rate of, enter an interval of time in minutes, hours, or days.
For Cron expression, enter an expression that tells Lambda when to stop your instances.
Cron expressions are evaluated in UTC. Be sure to adjust the expression for your preferred time zone.
Under Targets, choose Add target.
Choose Lambda function.
For Function, choose the function that stops your EC2 instances.
Choose Configure details.
Under Rule definition, do the following:
For Name, enter a name to identify the rule, such as “StopEC2Instances”.
(Optional) For Description, describe your rule. For example, “Stops EC2 instances every night at 10 PM.”
For State, select the Enabled check box.
Choose Create rule.
Once all these steps are successfully completed, you have your automated lambda function ready that will stop-start your specified instances at your specified time. If facing problems in implementation anywhere in this process, feel free to contact us and we will be more than happy to assist you.
As a Director of Eternal Web Private Ltd an AWS consulting partner company, Nirav is responsible for its operations. AWS, cloud-computing and digital transformation are some of his favorite topics to talk about. His key focus is to help enterprises adopt technology, to solve their business problem with the right cloud solutions.