Auto Shutdown and Start Amazon EC2 Instance: A Guide

In our quest for efficient resource management and cost-saving, automating the start and stop times of Amazon EC2 instances can be an effective solution. This guide will walk you through the process of setting up automatic shutdown and start for your Amazon EC2 instances.

Auto Shutdown and Start Amazon EC2 Instance: A Guide

In our quest for efficient resource management and cost-saving, automating the start and stop times of Amazon EC2 instances can be an effective solution. This guide will walk you through the process of setting up automatic shutdown and start for your Amazon EC2 instances.

What is Amazon EC2?

Amazon Elastic Compute Cloud (EC2) is a part of Amazon’s cloud-computing platform, Amazon Web Services (AWS). EC2 provides scalable computing capacity in the AWS cloud, eliminating the need to invest in hardware upfront, so you can develop and deploy applications faster.

Why Automate EC2 Instance Start and Stop?

Automating the start and stop times of your EC2 instances can significantly help you cut down on unnecessary costs. EC2 instances can run up high costs if they are left running when not in use, especially for instances used for development, testing, or staging, which are not typically used outside of business hours. By ensuring that these instances are shut down during non-working hours, you can save up to 70% of your EC2 costs.

How to Automate EC2 Instance Start and Stop?

Now, let’s move to the main part of our post: how to automate the start and stop for your Amazon EC2 instances. This process is made possible by using AWS Lambda and CloudWatch. AWS Lambda lets you run your code without provisioning or managing servers, and AWS CloudWatch allows you to collect and track metrics, collect and monitor log files, set alarms, and automatically react to changes in your AWS resources.

Step 1: Creating IAM Role

Before we start, you need to create an IAM role that AWS Lambda can assume to carry out the stop and start actions on your EC2 instances. This role should have the necessary permissions to access AWS Lambda, CloudWatch, and EC2.

1. Navigate to the IAM console in AWS.
2. Click on Roles > Create role.
3. Choose Lambda as the service and click on Next: Permissions.
4. Attach the following policies: AWSLambdaExecute, AmazonEC2FullAccess, and CloudWatchFullAccess.
5. Review and create your role.

Step 2: Creating the Lambda Function

Next, we’ll create our Lambda function to stop and start the EC2 instances.

1. Navigate to the Lambda console in AWS.
2. Click on Create function.
3. Choose 'Author from scratch', give your function a name, and choose Python 3.8 as the runtime.
4. Under permissions, select the IAM role that you created in Step 1.
5. Click on Create function.

Then, insert this Python script in the Function code section:

import boto3
region = 'us-west-1'
instances = ['i-*********']
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.start_instances(InstanceIds=instances)

Replace the region and instances values with your own AWS region and the EC2 instance IDs you want to start or stop.

Step 3: Creating CloudWatch Rules

Now, we’ll create two CloudWatch rules to trigger the Lambda function at specific times.

1. Navigate to the CloudWatch console in AWS.
2. Click on Rules > Create rule.
3. Under Event Source, select Schedule.
4. Set your desired cron expression for when you want to start your EC2 instances.
5. Under Targets, select the Lambda function you created in Step 2.
6. Click on Configure details, give your rule a name, and click on Create rule.

Repeat the same process for when you want to stop your instances, but use a different cron expression.

Conclusion

Automating the start and stop times of your Amazon EC2 instances is a simple yet effective way to manage resources and reduce operational costs. With just a few steps, you can set up your EC2 instances to only be up and running when you need them to be. Remember, automation is key to efficiency in the cloud. Happy cost-saving!


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.