DEV Community

Cover image for AWS Serverless: How to Stop EC2 using Event Bridge and Lambda
Girish Bhatia
Girish Bhatia

Posted on

AWS Serverless: How to Stop EC2 using Event Bridge and Lambda

"Just like you wouldn’t leave all the lights on while sleeping, why keep your EC2 servers running when they’re not in use?"

In this article, I will demonstrate how to stop EC2 instances using Amazon EventBridge and AWS Lambda on a scheduled basis.

Introduction:

With the cloud, launching EC2 instances to host and run your applications is incredibly easy. You might use these instances for development, testing, production, failover, disaster recovery (DR), proof of concept (PoC), or a variety of other reasons—unless you are fully committed to a serverless architecture! Over time, a company may accumulate hundreds of EC2 instances, and the cost can escalate quickly.

With the convenience of the cloud comes the Shared Responsibility Model, where AWS manages the infrastructure, but customers are responsible for managing resource usage and costs. That's where FinOps (Cloud Financial Management) becomes crucial. One of the best practices is to understand usage and costs for your cloud resources.

In the context of managing numerous EC2 instances, it’s essential to analyze whether all of them need to be running 24/7. Most likely, your analysis will reveal that certain instances are only required during specific periods.

Use Case:

Let’s walk through a real-world use case:

  • Stop EC2 instances with the tag GB_Dev at 6 PM EST every Friday.
  • Start EC2 instances with the tag GB_Dev at 6 AM EST every Monday.

In this article, I'll focus on the stopping scenario, using AWS EventBridge and AWS Lambda with the Python Boto3 library to implement the solution.

In a follow-up article, I'll cover starting EC2 instances on a schedule.

Architecture Overview:

We'll use the following architecture to implement the solution:

  1. EC2 Instance with the tag GB_Dev.
  2. Lambda Function that contains the logic to stop the EC2 instances.
  3. EventBridge Scheduler to trigger the Lambda function at a specific time using a cron job pattern.

Image arch

Here’s how the setup looks:

  • Prerequisites: You must have an EC2 instance tagged with GB_Dev.
  • Lambda Logic: The Lambda function will stop the EC2 instances based on the schedule.
  • EventBridge Schedule: A cron job in EventBridge will schedule the stop action.

Example EC2 with Tag:

Here is an example of what your EC2 instance might look like with the tag GB_Dev:

Image ec2running

Lambda Function Code Snippet:

Image lambda

EventBridge Configuration:

The EventBridge configuration for scheduling the stop action will resemble the diagram below:

Image description

Image description

Outcome:

Once the schedule is triggered, EventBridge will invoke the Lambda function, which will stop the EC2 instances tagged GB_Dev as per our use case.

Image description

Conclusion:

In this article, I’ve shown how to use EventBridge to invoke a Lambda function to stop EC2 instances on a schedule. This method can help save costs by ensuring EC2 instances are not running when they’re not needed.

Stay tuned for my next article, where I’ll demonstrate how to use EventBridge and Lambda to start EC2 instances on a schedule.

Thank you for reading!

Click here to watch the YouTube video for this solution:

https://www.youtube.com/watch?v=NZnMiDF5rDA

𝒢𝒾𝓇𝒾𝓈𝒽 ℬ𝒽𝒶𝓉𝒾𝒶
𝘈𝘞𝘚 𝘊𝘦𝘳𝘵𝘪𝘧𝘪𝘦𝘥 𝘚𝘰𝘭𝘶𝘵𝘪𝘰𝘯 𝘈𝘳𝘤𝘩𝘪𝘵𝘦𝘤𝘵 & 𝘋𝘦𝘷𝘦𝘭𝘰𝘱𝘦𝘳 𝘈𝘴𝘴𝘰𝘤𝘪𝘢𝘵𝘦
𝘊𝘭𝘰𝘶𝘥 𝘛𝘦𝘤𝘩𝘯𝘰𝘭𝘰𝘨𝘺 𝘌𝘯𝘵𝘩𝘶𝘴𝘪𝘢𝘴𝘵

Top comments (0)