Introduction:
EC2 Instances serve as the fundamental building blocks of your cloud setup—crucial for your virtual environment. But here's the challenge: keeping costs under control can be tricky, especially when you want to make sure you're not overspending.
If you need an environment for ad-hoc work, you know they don’t need to run constantly. Why pay for server uptime while you’re off the clock? You might want to turn these instances on and off according to your needs.
So, what are your choices?
You want to avoid labor-intensive and inefficient approaches. And you obviously cannot keep your EC2 instances running 24/7 unless you're using a free-tier instance.
Automate the Process – Here’s the exciting part! You can automate the start and stop times of your EC2 Instances using a few AWS services.
By the end of this article, you'll have a streamlined, cost-efficient way to manage your EC2 Instances effortlessly. Let’s dive in!
Purpose:
It is easy to spin up a new EC2 instance, and along with the convenience comes the price. On-demand user pricing is charged per hour, which can be hard on you if you are not careful with start/stop.
Below are 2 scenarios discussed from a user’s perspective and their solution.
Scenario 1
A user logout time is unknown, while the EC2 instances are left running unintentionally.
Solution:
The CloudWatch alarm monitors metrics and sends notifications to an SNS topic, triggering a Lambda function to stop the instance. This setup helps automate resource management based on specified conditions.
This solution is less discussed as it involves identifying appropriate CPU utilization percentages that can be regarded as idle/inactive EC2 instances.
Steps:
1)Create an IAM policy and IAM role for your Lambda function
a)Create IAM policy
i)Set permissions for EC2.
ii)Select Write Access level: StartInstances, StopInstances.
iii)Specify resource ARNs: Add ARNs and mention resource region, resource instance (instance Id and resource instance (instance ID).
b)Create IAM role
i)Choose the above policy while creating the role.
2)Create lambda functions to stop EC2
a)Create function: Author from scratch.
b)Choose Python 3.9 for runtime.
c)Choose an existing role and choose the IAM role created above.
d)Under the create function on the code tab, use the code below and update the region and instance IDs.
import boto3 region = '<us-west-1>' instances = ['<i-12345cb6de4f78g9h>', '<i-08ce9b2d7eccf6d26>'] ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context): ec2.stop_instances(InstanceIds=instances) print('stopped your instances: ' + str(instances)) |
e)Deploy code and test the function.
3)Create an SNS topic
a)Configure the topic and choose the Standard option.
b)Create a subscription and select the lambda created as the endpoint.
4)Create a Cloudwatch alarm
a)Choose the appropriate metric that you want to monitor. Such as CPU utilization for the instance < ~10% for 1 hour (appropriate % is not confirmed).
b)Select the instance metric on which you want to base the alarm and define the threshold conditions.
c)Configure alarm to send a notification to the SNS topic created earlier.
d)Review, provide a name for the alarm, and create.
Scenario 2
When the EC2 instances are left running, the user logout time is known.
Solution:
The below setup allows you to automate the stopping of instances based on schedules or specific events, reducing manual intervention and optimizing resource management.
This solution is quite common and is discussed in a few blog posts.
Steps:
1)Create an IAM policy and IAM role for your Lambda function
a)Create IAM policy
i)Set permissions for EC2.
ii)Select Write Access level: StartInstances, StopInstances.
iii)Specify resource ARNs: Add ARNs and mention resource region and resource instance (instance ID).
b)Create IAM role
i)Choose the above policy while creating the role.
2)Create lambda functions to stop EC2
a)Create function: Author from scratch.
b)Choose Python 3.9 for runtime.
c)Choose an existing role and choose the IAM role created above.
d)Under the create function on the code tab, use the code below and update the region and instance IDs.
import boto3 region = '<us-west-1>' instances = ['<i-12345cb6de4f78g9h>', '<i-08ce9b2d7eccf6d26>'] ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context): ec2.stop_instances(InstanceIds=instances) print('stopped your instances: ' + str(instances)) |
e)Deploy code and test the function.
3) Create EventBridge rules that run your Lambda functions
a)Create rule on the console.
b)Choose Schedule on rule type.
c)Under the schedule pattern, choose the recurring schedule and the Cron-based schedule.
d)Select Minute, Hours, Day of month, Month, Day of the weekthe month, Month, Day of the week, and Year.
e)Select targets, choose Lambda function from the Target dropdown list, and finally create.
Conclusion:
By leveraging these techniques, you can shift your focus back to what truly matters—your core work—while leaving the manual management of EC2 instances behind. This streamlined setup boosts both efficiency and cost-effectiveness, automating your instance operations effortlessly. Dive into the perks of automated management and enjoy the significant cost savings it brings to your AWS environment.
Comments