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.
AWS EC2
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.
Create a custom AWS Identity and Access Management (IAM) policy and execution role for your Lambda function.
Create Lambda functions that stop and start your EC2 instances.
Test your Lambda functions
Create CloudWatch Events rules that trigger your function on a schedule. For example, you could create a rule to stop your EC2 instances at night, and another to start them again in the morning.
Pre-Requirements
Get the IDs of the EC2 instances that you want to stop and start, and then follow these instructions.
Steps To Start And Stop AWS EC2 Instance Using Lambda
1. Create a custom AWS Identity and Access Management (IAM) policy and execution role for your Lambda function.
Create an IAM policy using the JSON policy editor. Paste this JSON policy document into the policy editor:
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.
Step 8
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.
Step 9
Choose Save.
3. Test your Lambda functions
Step 1
In the Lambda console, choose Functions.
Step 2
Select one of the functions that you created.
Step 3
Choose Actions, and then choose Test.
Step 4
In the Configure test event dialog, choose Create new test event.
Step 5
Enter an Event name, and then choose Create.
Note
You don’t need to change the JSON code for the test event—the function doesn’t use it.
Step 6
Choose Test to execute the function.
4. Create rules that trigger your Lambda functions
Step 1
Open the CloudWatch console.
Step 2
In the left navigation pane, under Events, choose Rules.
Step 3
Choose Create rule.
Step 4
Under Event Source, choose Schedule.
Step 5
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.
Note
Cron expressions are evaluated in UTC. Be sure to adjust the expression for your preferred time zone.
Step 6
Under Targets, choose Add target.
Step 7
Choose Lambda function.
Step 8
For Function, choose the function that stops your EC2 instances.
Step 9
Choose Configure details.
Step 10
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.
Step 11
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.
Let Us Talk About Your AWS Development Requirements
Have queries about your AWS project ideas and concepts? Please drop in your project details to discuss with our AWS experts, professionals and consultants.