Automating EC2 Instance Management to Reduce AWS Costs
In a typical AWS environment, running EC2 instances continuously can lead to high costs, especially if those instances are not utilized during certain periods, such as overnight or during non-business hours. To address this, I developed a solution that automatically starts and stops EC2 instances based on a specific schedule. This approach ensures that resources are only active when needed, thereby reducing unnecessary AWS billing costs.
In this blog post, I'll walk you through a step-by-step process to automate the start and stop actions of an EC2 instance using AWS Lambda. We'll also set up an Amazon SNS (Simple Notification Service) to send email notifications whenever these actions occur.
Step 1: Launching an EC2 Instance
Go to the EC2 Dashboard in the AWS Management Console.
Click on Launch Instance and follow the steps to select an Amazon Machine Image (AMI), choose an instance type, configure instance details, and launch it.
Make sure to note the instance ID as it will be crucial in the following steps.
Step 2: Creating an IAM Policy for EC2 Management
Navigate to the IAM Dashboard in the AWS Management Console.
Under Policies, click Create Policy.
Use the JSON editor to define a policy that grants permissions to start and stop the EC2 instances:
Step 3: Creating an IAM Role for Lambda
Next, we need to create an IAM role that AWS Lambda can assume to execute our EC2 management tasks:
In the IAM dashboard, go to Roles and click on Create Role.
Choose Lambda as the trusted entity type, as this role will be used by the Lambda function.
Attach our created policy ( StartandStopEc2Policy )to the role.
Name the role, for example, StartandStopEc2Role.
Step 4: Writing the Lambda Function
Go to the Lambda Dashboard and click Create Function.
Choose Author from Scratch, and give your function a name like StartandStopInstances.
Select the Python runtime and use the role you created (StartandStopEc2Role).
Replace the default code with the following script:
In the above code replaces instances with your instancesIds, <your-account-id> and <your-topic-name> in TopicArn Variable with the actual values corresponding to your environment.
Step 5: Automating Execution with CloudWatch Events
In the CloudWatch Dashboard, go to Rules under the Events section.
Click on Create Rule and select Event Source as Schedule.
Define the cron expression or rate (e.g., every day at 7 AM) to trigger the function.
Set the target to the Lambda function you created.
Define the action as JSON input to specify whether to start or stop the instance.
Ex:- { "action": "start" }.
Create another rule to stop the instance at a different time with "action": "stop".
Now in our lambda function configuration in trigger section. We can see that there is two trigger points available. Our lambda function will execute based on the trigger points.
Step 6: Setting Up SNS for Notifications
In the SNS Dashboard, click on Create Topic.
Choose a name and create the topic.
Click on the newly created topic and add a subscription.
Choose Email as the protocol, and enter your email address.
Check your email for a confirmation link from AWS, and confirm the subscription.
Now our lambda function will look like:
Make sure that your Lambda function publishes messages to the SNS topic whenever it performs an action.
You will get a notification to your subscribed email whenever the lambda function executes.
Conclusion:
By leveraging AWS Lambda, CloudWatch Events, and SNS, I was able to automate the start and stop operations for an EC2 instance, significantly reducing unnecessary costs. This setup not only saves you time but also optimizes resource usage, ensuring that your instances are only running when needed. Implementing such a solution is a great way to improve cost efficiency in AWS environments, particularly for businesses with predictable workloads that do not require 24/7 operation .
Subscribe to my newsletter
Read articles from Prem Sai Noolu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by