Automate shutdown of an EC2 with AWS EventBridge & AWS Lambda

Brett Barrientes
6 min readApr 25, 2024

Currently, there is a startup named ‘Cloud City Tech’. Cloud City Tech provided cost-saving solutions to other startups and small businesses. However, the irony was, Cloud City Tech itself had a problem. They were using an Amazon EC2 instance for a non-critical dashboard that was only used during business hours (9 AM — 5 PM). Running this instance 24x7 was racking up unnecessary costs.

One day a cloud engineer at Cloud City Tech had a bright idea. “Why don’t we use AWS EventBridge Scheduler to automate the shutdown of the EC2 instance outside business hours?” he suggested. This hands-on challenge is based on the cloud engineer’s idea to implement this cost-saving solution.

Introduction of EventBridge Sceduler:

AWS EventBridge is a serverless event bus service that allows you to connect applications using data from various sources, including AWS services, integrated software as a service (SaaS) applications, and your custom applications. It is an evolution of Amazon CloudWatch Events.

One of the features of EventBridge is the ability to schedule automated actions based on a cron or rate expression. This feature is commonly referred to as the “EventBridge Scheduler”.

Here’s a breakdown of the EventBridge Scheduler’s capabilities:

  1. Cron Expressions: EventBridge supports cron expressions, which are strings that define a schedule in a Unix-like way. For example, you can set up a cron expression to trigger an event every day at a specific time.

(Okay Linux users, I know you are happy about this because I am)

2. Rate Expressions: If you want to specify a fixed rate, like every 5 minutes or every 2 hours, you can use a rate expression.

3. Targets: When the scheduler triggers an event, it sends that event to the targets you define. These targets can be various AWS services such as AWS Lambda functions, Amazon EC2 instances, Amazon SNS topics, and more.

4. Use Cases: Typical use cases for the EventBridge Scheduler include:

  • Running a Lambda function to clean up old data in a database every night.
  • Starting or stopping EC2 instances at specific times to save costs.
  • Triggering a pipeline in AWS CodePipeline at a specific time every day.

In essence, AWS EventBridge Scheduler allows you to automate actions in a serverless manner based on a predefined schedule. It’s a powerful tool for implementing time-based actions without the need for additional infrastructure or services.

Okay so now that you have a good understanding about AWS EventBridge Scheduler, lets move forward to the objective.

Objective: Automate the shutdown of an EC2 instance every day at 5 PM using AWS EventBridge Scheduler & AWS Lambda.

Steps:

  1. Set Up Your Environment:
  • Ensure you have an AWS account.
  • Log in to the AWS Management Console.
  • Make sure you have an EC2 instance running (you can use a free-tier t2.micro instance for this exercise).

2. Create a Lambda Function:

  • Navigate to the AWS Lambda service in the AWS Management Console.
  • Click ‘Create function’.
  • Name your function, e.g., “StopEC2Instance”.
  • For the runtime, select “Python 3.x”.
  • In the function code, use the following snippet:
import boto3

def lambda_handler(event, context):
# Create an EC2 client
ec2 = boto3.client('ec2')

# Get instance ID from the event argument
instance_id = event['instance_id']

# Stop the EC2 instance
response = ec2.stop_instances(InstanceIds=[instance_id])

return {
'statusCode': 200,
'body': f"Stopped instance: {instance_id}"
}
  • Under ‘Configuration > Permissions’, ensure your Lambda function has a role with permissions to stop EC2 instances (AmazonEC2FullAccess).
  1. Select the Role Name

2. If you do not have the correct permissions, select Add Permissions

3. Add AmazonEC2FullAccess to Permissions.

  • This will allow AWS Lambda to stop the EC2 instance once the action is triggered.
  • Save the function.

3. Navigate to EventBridge:

  • In the AWS Management Console, go to Amazon EventBridge.
  • Choose ‘Create rule’.

4. Configure the Event:

  • Name and Description: Name your rule, e.g., “LambdaShutdownEC2” and provide a brief description.
  • Define Pattern: Choose ‘Schedule’. Use the cron expression for 5 PM:
0 17 ? * MON-FRI *

Choose Targets:

  • Click on ‘Add target’ and select ‘Lambda function’ from the dropdown.
  • For ‘Function’, select the Lambda function you created in step 1.

Scroll down -

Under Payload, we will need to go back to the AWS Lambda function we created, navigate over to the test tab to pick the Key:Value.

Copy from the Event JSON then paste under the Payload.

Now Update the where we will name the Key as the instance_id and value to be the instance id itself from the EC2 instance.

Basically this will allow us to provide the instance_id to our Lambda function.

Now select Next.

Leave everything else as default and select Next under Settings.

7. Create the Schedule:

  • Review all settings and click ‘Create Schedule’.

8. Test Your Setup:

  1. To ensure your setup works, adjust the schedule to a time a few minutes ahead of your current time. Once the EC2 instance stops, revert the schedule back to 5 PM.

With this advanced setup, you now have the power of AWS Lambda at your disposal. You can extend the Lambda function to include more advanced operations, such as checking if the instance is in a certain state before stopping it, or sending notifications when the instance has been stopped.

The possibility is almost endless but try out different situations as you wish.

Conclusion:

The Cloud Engineer’s suggestion was implemented, and Cloud City Tech’s non-critical EC2 instance was automatically shutting down every weekday at 5 PM, leading to significant cost savings. With this automation in place, Cloud City Tech truly lived up to its name.

This offers several benefits:

Cost Optimization: By automating their shutdown when not in use, this can significantly reduce unnecessary expenses. This is particularly advantageous for development and testing environments that may not require 24/7 availability.

Resource Efficiency: Automatically shutting down EC2 instances helps optimize resource utilization within your AWS environment. This ensures computing resources are only active when needed, freeing up capacity for other workloads.

Operational Streamlining: Automating the process with AWS EventBridge and Lambda streamlines operations by executing the shutdown routine reliably based on predefined criteria, such as specific time schedules or usage patterns. Using this process eliminates the manual task and human error of start and stop times of EC2 instances.

Scalability: As your AWS infrastructure grows, managing instances manually becomes increasingly impractical. Leveraging automation tools like EventBridge and Lambda allows you to scale your shutdown processes efficiently without proportional increases in administrative overhead.

Enhanced Security: Automatically shutting down EC2 instances when they are not needed reduces the attack surface and minimizes the window of vulnerability. This proactive approach to security helps mitigate risks associated with unattended or idle instances.

In summary, automating EC2 instance shutdown with AWS EventBridge and Lambda enhances cost efficiency, resource utilization, operational reliability, and security within your AWS environment while providing flexibility and scalability to adapt to changing workload demands.

Hope you enjoyed the challenge and learned something new!

Reminder! Once you finish it, remove all the resources created!

References

--

--

Brett Barrientes

Cloud & DevOps Engineer | AWS | Microsoft Azure | Cloud Automation | Docker | Kubernetes | Ansible | Terraform | Python