Automating EC2 Instance Startup with Lambda Functions
Introduction
In today’s cloud computing landscape, managing resources efficiently is crucial for optimizing costs and ensuring smooth operations. AWS provides various services to automate and streamline tasks, and in this blog post, we’ll explore how to automate the management of EC2 instances using AWS Lambda, Simple Notification Service (SNS), and Slack.
Why Automate EC2 Instance Management?
EC2 instances are fundamental building blocks of cloud infrastructure, but manually managing them can be time-consuming and error-prone. Automating tasks such as starting and stopping instances based on specific time can help reduce costs in Poc environments and help in reducing excessive costs caused by letting the ec2 instances run in the background while no one is using them.
Solution Overview:
We’ll use AWS Lambda, a serverless computing service, to run our code in response to events triggered by Amazon EventBridge scheduler schedules. EventBridge will serve as the event bus, allowing us to define rules that trigger Lambda functions based on events such as EC2 instance state changes. The Lambda functions will perform actions such as starting or stopping instances on the respective time set, sending notifications via SNS, and alerting stakeholders through Slack.
Implementation:
- Setting Up EventBridge Schedules: We’ll configure EventBridge schedules using Cron expressions to execute specific Lambda functions at predetermined times. For instance, we’ll schedule the Lambda function to start instances every weekday at 9:00 AM and stop instances at 5:00 PM, ensuring optimal resource utilization during business hours.
- Creating Lambda Functions: Lambda functions will be created to start and stop EC2 instances based on the specified events. These functions will use the Boto3 library to interact with the AWS SDK programmatically.
- Configuring SNS Topics for SMS Notifications: SNS topics will be established to send SMS notifications whenever instances are started or stopped. These topics will function as dedicated communication channels for promptly informing stakeholders via text messages about any alterations in the EC2 environment.
- Integrating with Slack: Slack webhook URLs will be used to send messages to designated channels whenever instances are started or stopped. This integration will facilitate real-time communication and collaboration among team members.
Code Walkthrough: We’ll provide detailed code snippets for implementing Lambda functions to start and stop EC2 instances.
IAM POLICY
The IAM policy grants permissions necessary for the Lambda functions to perform required actions on AWS resources.
Iam policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "<your_sns_topic_arn>"
},
{
"Effect": "Allow",
"Action": [
"ec2:DescribeInstances",
"ec2:StopInstances",
"ec2:StartInstances"
],
"Resource": "*"
}
]
}
Lambda Function Code:
Below is the link to the Python code for Lambda functions to start and stop EC2 instances.
GitHub Code
Sending notifications to Slack channels.
- Create a Channel on Slack for the Slack-configured app to send messages.
- Go to https://api.slack.com/ and log in to your workspace. Click on “Your Apps” and then “Create New App.

- Choose “From Scratch” and enter a name for your app. Select your workspace.


- Once the app is created, go to “Incoming Webhooks” and enable it.


- Scroll down to “Webhooks to Workspace” and click “Add New Webhook to Workspace.”

- Select the channel where you want to send notifications and click “Allow.”

- Copy the newly generated webhook URL and add it to the lambda function code.

Additionally, we’ll demonstrate how to configure EventBridge rules to trigger Lambda functions based on EC2 instance state changes.
EventBridge Scheduler Cron job for AutoStart Lambda Function.
0 9 ? * 2-6 *
This Cron Job schedules a job to run at 9:00 AM every Monday to Friday, regardless of the month or year.
Minute : (0-59)
Hour : (0-23)
Day of the month : (1-31)/ ‘?’ This symbol is used as a wildcard character in the “day of the month” field. It indicates that the cron job should run regardless of the day of the month.
Month : (1-12)
Day of the week: (0-6, where 0 represents Sunday)
Year: The asterisk (*
) means the job will run every year.
EventBridge Scheduler Cron job for AutoStop Lambda Function.
0 17 ? * 2-6 *
This Cron Job schedules a job to run at 5:00 PM every Monday to Friday, regardless of the month or year.
SMS (Sent to your Registered phone number – SNS)

Conclusion: Automating EC2 instance management with AWS Lambda, EventBridge, SNS, and Slack streamlines operations, improves resource utilization, and enhances collaboration among team members. By leveraging event-driven architecture and serverless computing, organizations can achieve greater efficiency, agility, and scalability in their cloud environments.
Stay tuned for more insights into cloud automation and best practices for managing AWS resources effectively!
Post Comment