AWS, Cloud Computing

6 Mins Read

Automate Scheduled Start and Stop of Amazon EC2 Instances with AWS Lambda

Voiced by Amazon Polly

Overview

The ability to start and stop Amazon EC2 instances at predefined intervals can be useful for optimizing resource utilization and cost control in an AWS environment. AWS Lambda, a serverless computing tool, may do this automation without the server or infrastructure management requirement. This review will walk you through the processes required in configuring a Lambda function to start or stop Amazon EC2 instances on a preset schedule.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

Reasons to schedule Amazon EC2 using AWS Lambda

There are several reasons why you might want to start or stop Amazon EC2 instances at scheduled times using AWS Lambda:

  1. Cost optimization: Running Amazon EC2 instances continuously can be expensive. By scheduling your instances’ start and stop times, you can ensure that you only pay for the compute resources you need.
  2. Automation: Starting and stopping Amazon EC2 instances can be time-consuming and error-prone. By automating this process with Lambda, you can ensure that your instances are always running when needed and that you’re not paying for idle resources.
  3. Resource management: If you have many Amazon EC2 instances, it can be difficult to track which are running and which are not. By scheduling start and stop times, you can ensure that your resources are being used efficiently and that you’re not wasting money on unused instances.
  4. Environment management: If you have different environments (e.g., development, testing, production) running on separate instances, you can use scheduled start and stop times to ensure that each environment is only running during the times when it’s needed. This can help you avoid conflicts and ensure your resources are used efficiently.

AWS Lambda

AWS Lambda function is a serverless compute service that allows you to run code in response to events or on a scheduled basis without having to provision or manage servers. It is a way to create small, self-contained functions triggered by events such as changes to data in an Amazon S3 bucket, a new message in an Amazon SNS topic, or an HTTP request to an API Gateway endpoint.

AWS Lambda functions are typically written in one of the supported programming languages, such as Python, Java, Node.js, C#, or Go, and can be triggered by various events or sources. They can perform various tasks, such as processing data, running ETL (Extract, Transform, Load) jobs, generating reports, or responding to web requests.

Using AWS Lambda functions, developers can create serverless architectures where the code is executed on-demand and billed per invocation, making it highly scalable and cost-effective. AWS Lambda functions can be used to build various applications, including web and mobile backends, data processing pipelines, and chatbots.

Steps to use AWS Lambda to stop and start Amazon EC2 instances at the scheduled time

Step 1: Create an Amazon EC2 Instance:

  • Search Amazon EC2 in the service tab
  • Click on the launch Instance
  • Choose the name of the instance, size of the instance, OS, VPC, Security group, EBS, and other resources as per requirements.

step1

Step 2: Create AWS IAM Policy using the JSON policy editor

step2

step2b

Step 3: Create AWS IAM Role

  • Choose AWS IAM in the service tab
  • Click on Create Role
  • Choose the AWS Service option and AWS Lambda as a use case
  • Attach the policy which you create in Step 2
  • Choose to Create Role

step3

step3b

step3c

step3d

Step 4: Create AWS Lambda functions that stop and start your Amazon EC2 instances

  • Choose to Create function
  • Choose an Author from scratch
  • For the Function name, please enter a name that identifies it as the function used to stop your Amazon EC2 instances.
  • For Runtime, choose Python 3.9
  • Under Permissions, expand Change default execution role
  • Under the Execution role, choose to Use an existing role
  • Under Existing role, choose the IAM role that you created
  • Choose Create function
  • Under Code, Code source, copy and paste the following code into the editor pane.
  • Choose Deploy
  • On the Configuration tab, choose General Configuration, Edit. Set Timeout to 10 seconds, and then choose

Stop the instances:-

Important: For regions and instances, use the values you used for the code to stop your Amazon EC2 instances.

step4

step4b

step4c

step4d

Step 5: Repeat all the steps in step 4 to Create AWS Lambda Function to start your Amazon EC2 Instance

  • Under Code, Code source, copy and paste the following code into the editor pane

Start the instances:-

Important: For regions and instances, use the values you used for the code to stop your Amazon EC2 instances.

Step 6: Test Your Lambda Function

  • Open the AWS Lambda Console, and then choose Functions.
  • Choose one of the functions that you created
  • Choose the Code In the Code source section, choose Test
  • In the Configure test event dialog box, choose to Create a new test event
  • Enter an Event name. Then, choose Create
  • Choose Test to run the function
  • Repeat steps 1-7 for the other function that you created.

step6

Step 7: Check the status of your Amazon EC2 instances

  • Open the Amazon CloudTrail Console
  • In the navigation pane, choose Event History
  • Choose the Lookup attributes dropdown list, and then choose an Event name
  • In the search bar, enter StopInstances to review the results
  • In the search bar, enter StartInstances to review the results

step7

step7b

Step 8: Create Amazon EventBridge rules to run AWS Lambda functions

  • Open the Amazon EventBridge Console
  • Select Create rule
  • Enter a Name for your rule
  • For Rule type, choose Schedule, and then choose to Continue in Amazon EventBridge Scheduler
  • For Schedule pattern, choose Recurring schedule
  • When Schedule type is Cron-based schedule, for the Cron expression, enter an expression that tells Lambda when to stop your instance, and Cron expressions are evaluated in UTC
  • In Select targets, choose AWS Lambda function from the Target
  • For Function, choose the function that stops your Amazon EC2 instances
  • Repeat steps 1-8 to create a rule to start your Amazon EC2 instances

step8

step8b

step8c

step8d

step8e

step8f

Conclusion

AWS Lambda to start and stop Amazon EC2 instances can provide many benefits, including cost savings, automation of routine tasks, and improved security. However, it’s important to configure AWS IAM roles and policies to ensure that only authorized users can access and perform actions on Amazon EC2 instances. There are also some limitations to using AWS Lambda, such as the maximum execution time of 15 minutes per function and the potential for cold start times. Using Lambda to manage Amazon EC2 instances can be useful for optimizing and automating your AWS environment.

For more information, refer: Stop and start EC2 instances at set intervals using Lambda | AWS re:Post (repost.aws)

Making IT Networks Enterprise-ready – Cloud Management Services

  • Accelerated cloud migration
  • End-to-end view of the cloud environment
Get Started

About CloudThat

CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding AWS Lambda, Amazon EC2, I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.

FAQs

1. How can Lambda be used to start and stop Amazon EC2 instances?

ANS: – AWS Lambda can start and stop Amazon EC2 instances by creating functions that use the AWS SDK to interact with Amazon EC2. The function can be triggered by an Amazon CloudWatch event, such as a schedule, to start or stop Amazon EC2 instances based on a specified time.

2. How can I ensure that only authorized users can start and stop Amazon EC2 instances using AWS Lambda?

ANS: – You can ensure that only authorized users can start and stop Amazon EC2 instances using AWS Lambda by configuring AWS Identity and Access Management (IAM) roles and policies. AWS IAM allows you to define granular permissions for different users and resources, ensuring that only authorized users can perform certain actions.

3. Are there any limitations to using AWS Lambda to start and stop Amazon EC2 instances?

ANS: – There are some limitations to using AWS Lambda to start and stop Amazon EC2 instances, such as the maximum execution time of 15 minutes per function and the inability to access resources outside the AWS ecosystem. Additionally, AWS Lambda functions may have cold start times, which can cause delays in starting or stopping instances.

WRITTEN BY Ayush Agarwal

Ayush Agarwal works as a Research Associate at CloudThat. He has excellent analytical thinking and carries an optimistic approach toward his life. He is having sound Knowledge of AWS Cloud Services, Infra setup, Security, WAR, and Migration. He is always keen to learn and adopt new technologies.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!