Automate Start/Stop AWS EC2 Instance Using Lambda: Step-by-Step Guide

AWS CLOUD

Share Post Now :

HOW TO GET HIGH PAYING JOBS IN AWS CLOUD

Even as a beginner with NO Experience Coding Language

Explore Free course Now

Table of Contents

Loading

In the realm of cloud technology, efficiency is crucial. In this blog, we are going to explore AWS Lambda and EventBridge to automate AWS EC2 instance management. Save costs, streamline operations, and eliminate manual tasks. Discover how automation can transform your cloud workflow effortlessly.

In this blog, we will cover everything step by step about  How to Automate Start/Stop AWS EC2 Instances using Lambda.

Introduction

This blog focuses on using AWS Lambda to automate the starting and stopping of Amazon EC2 instances on a regular schedule. Imagine having a fleet of virtual computers in the cloud, each serving different purposes like testing software or running applications. 

These instances incur costs while running, similar to leaving lights on when not needed. By creating Lambda functions, we can instruct AWS to automatically turn off instances during periods of inactivity, such as nights or weekends, and restart them when needed.

This automation not only streamlines resource management but also helps save costs by optimizing instance usage based on specific time intervals.  The scheduling aspect is handled through Amazon EventBridge, where we set rules to trigger Lambda functions at designated times, ensuring efficient resource utilization and cost-effectiveness within the AWS environment.

Overview of services used

Elastic compute service (EC2):   AWS  EC2 is a cloud service that offers secure and scalable computing capability. It provides scalable instances, Elastic Load Balancing, Auto Scaling, and a variety of storage options including EBS and S3. With strong security features and different pricing models, EC2 works seamlessly with other AWS services to provide flexible and cost-effective cloud computing globally.


Know more on AWS EC2 Here

 Identity and Access Management (IAM): AWS Identity and Access Management (IAM) is a web service that provides secure control over AWS services and resources. It helps you to create and manage AWS users and groups, as well as use permissions to grant and prohibit access to AWS resources.

Know more on IAM Here

AWS Lambda Function: AWS Lambda is a serverless computing solution that executes your code in response to events while automatically maintaining the underlying infrastructure. It scales automatically and you simply pay for the compute time consumed, making it suitable for event-driven applications that don’t require server maintenance.

Know more on Lambda  Here

EventBridge: Amazon EventBridge is a serverless event bus service that connects applications based on data from your own apps, SaaS integrations, and AWS services. It enables you to easily create event-driven architectures by routing events from various sources to targets such as AWS Lambda, SNS, SQS, and other AWS services. EventBridge streamlines event management and delivery allows for event filtering, and assures scalable, reliable, and secure event ingestion and processing.

How to Schedule a Lambda Function using Amazon EventBridge - Knoldus Blogs

Know more on EventBridge Here

Demo:

Automate Start/Stop AWS EC2 Instance Using Lambda: Step-by-Step Guide

We’re going to follow the steps below:

  1. Creating an AWS EC2 Instance
  2. Setting up an AWS IAM Role
  3. Configuring a Lambda Function
  4. Configuring EventBridge
  5. Verifying the Lambda Function & EventBridge Rule.

1. Creating an AWS EC2 Instance

Refer to this blog to create an AWS EC2 Instance

2. Create an AWS IAM Role

We are going to  Create an IAM role Policy.

1. Click on the Policies from the Left Side and click on Create Policy.

create policy

2. Replace the existing Policy with the below policy.

existing Policy

{  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "ec2:Start*",	
        "ec2:Stop*"
      ],
      "Resource": "*"
    }
  ]
}

3. Policy Name as LambdaRole and click on Create policy
LambdaRole and click on Create policy
4. Click  on the Roles

Click  on the Roles

5. Select AWS Service as the trusted entity type

Select AWS Service as the trusted entity type

6. Search for the LambdaRole we had created and click Next

Search for the LambdaRole we had created and click Next

7. Provide the name as AWSlambdaRole and click on Create Role

Provide the name as AWSlambdaRole and click on Create Role

8. AWSLambdaRole is created successfully.

AWSLambdaRole is created successfully.

3. Configure the Lambda Function 

We are going to create a Lambda Function in this section

1. Search for Lambda and Click on Create a function.

Search for Lambda and Click on Create a function.

2. Select the Author from scratch Name as LambdaAutomation and Select the Runtime as Python 3.9.

Select the Author from scratch Name as LambdaAutomation

4.   select the Use an existing role choose the role we created and then click on Create Function.

Create Function

Here, we have successfully created the Lambda Function.

successfully created the Lambda Function.

Under Code, replace the existing code with the below Python code and click on deploy to save it

import boto3

region = 'us-west-1'

instances = ['i-12345cb6de4f78g9h']

ec2 = boto3.client('ec2', region_name=region)




def lambda_handler(event, context):

ec2.stop_instances(InstanceIds=instances)

print('stopped your instances: ' + str(instances))

 we have successfully deployed the Python code in the Lambda Function.

 4. Configure the EventBridge

We are going to create an EventBridge and add a trigger

1. Navigate to  Lambda Console and click on Add Trigger.

Add Trigger.

2. Select the EventBridge (CloudWatch Events)

EventBridge (CloudWatch Events)

3. Select Create a new rule and Select the Rule type as Schedule expression.

Select Create a new rule and Select the Rule type as Schedule expression.

4. Now in the New Google Tab, search for the UTC Time Right Now.

UTC Time Right Now.

5. we will write the Expression as cron(47 6 ? * * *) for  the instance to be stopped after 10 minutes

cron(47 6 ? * * *) for  the instance to be stopped after 10 minutes

6. We have successfully added the Trigger.

successfully added the Trigger.

5. Verify the Lambda Function & EventBridge Rule

We are going to verify the instance state in this section

1. Now it’s 6:47 UTC, refresh the EC2 Console and you can our EC2 Instance has Stopped Automatically.

EC2 Instance has Stopped Automatically

Thus we have successfully automated the process of Stopping the AWS EC2 Instance at the Specific Time

Conclusion

We covered the essential steps in building a comprehensive AWS workflow: creating an AWS EC2 instance for foundational infrastructure, configuring IAM roles to manage access securely, setting up Lambda functions for event-driven processing, and integrating EventBridge for seamless event routing. This setup ensures a robust and scalable architecture for AWS applications.

Frequently Asked Questions

What permissions are required for the Lambda function?

The Lambda function needs appropriate permissions to interact with EC2 instances. This typically involves attaching a policy to the Lambda execution role that allows actions like ec2:StartInstances and ec2:StopInstances'.

Can I use tags to manage which EC2 instances are affected?

Yes, you can modify your Lambda function to start and stop instances based on tags. For example, you can use the AWS SDK to filter instances that have a specific tag like Environment: Dev and apply start or stop actions only to them.

How can I handle errors in my Lambda function?

Error handling is crucial to ensure the reliability of your automation. Consider these strategies: Try-Except Blocks: Use try-except blocks in your Lambda function to catch and handle errors during execution. Dead Letter Queues (DLQ): Configure a DLQ to capture and analyze failed events or notifications. Alerts and Notifications: Set up CloudWatch Alarms or use SNS to send notifications if your Lambda function fails.

What alternatives are there to Lambda for automating EC2?

While AWS Lambda is a popular choice for automation, other AWS services and third-party tools can also be used, such as: AWS Systems Manager: For more complex scenarios involving patching, data collection, and resource configuration. Third-Party Automation Tools: Tools like Terraform or Ansible can also automate the provisioning and management of AWS resources, including EC2 instances.

Related References

Next Task For You

Attend our FREE CLASS to explore AWS Cloud discover in-demand job opportunities, and gain insights into essential Hands-On labs and projects.

Register now for our FREE Class on Amazon Web Services: Build In-Demand Skills and Secure High-Paying Jobs by clicking the image below.

AWS Job Oriented Free Class

 

Picture of mike

mike

I started my IT career in 2000 as an Oracle DBA/Apps DBA. The first few years were tough (<$100/month), with very little growth. In 2004, I moved to the UK. After working really hard, I landed a job that paid me £2700 per month. In February 2005, I saw a job that was £450 per day, which was nearly 4 times of my then salary.