Automating Amazon EC2 Instances to Shutdown & Power on
Introduction
Amazon EC2 instances are basically the virtual servers running in AWS cloud. It can be the intensive task of manually starting and shutting down every day for these instances. For economic reason, instances must be kept running only during a specified time window.
Basically, what we need is to periodically issue AWS API calls starting and stopping the instance, at scheduled times for this task. Instance to be started and stopped will be identified by its InstanceId in this case.
There are several options to reach this goal.
- AWS Data Pipeline
This approach is complicated and I will not vote for this option. With that being said, I will not discuss anything about this option. - Cron running on EC2 Instance
Maybe simpler than the previous option. But, maintenance, monitoring, and availability are an additional administrative burden. Personally, I will not go with this option. - AWS Lambda schedules events
It is a compute service that lets you run code without provisioning or managing servers. Actually, it is a PAAS (platform as a service) providing support for JavaScript (nodejs), Java and Python. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second. It will only charge when the code is running. It is possible to run configure any amazon instances with AWS Lambda- all with zero administration.
Implementing AWS Lambda Schedules Services
There are mainly three required parts and an optional part.
Required parts:
- IAM policy role
- Lambda functions
- Lambda schedules events
Optional part:
- Logging
STEPS:
Open the AWS Lambda console
Choose Create Function and select “Author from Scratch”.
Provide any suitable name. Example: StartEC2Instances or StopEC2Instances
Runtime: Python 2.7
Expand the Role drop-down menu, and choose to Create a custom role. This opens a new tab or window in your browser.
In the new tab “ IAM Role” drop-down menu, select Create a new IAM Role, and enter a Role Name, such as “lambda_start_stop_ec2."
Choose View Policy Document, Edit, and then choose Ok when prompted to read the documentation. Edit the policy as follows:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "ec2:Start*", "ec2:Stop*" ], "Resource": "*" } ] }
Choose Allow to finish creating the role and return to the AWS Lambda console.
Click on “Create Function”.
Now return to function window.
Its now time to edit the actual code, which will start/stop the Amazon Instances.
If you have multiple functions, click on the function which you created just now.
It will open a new window.
Go to the Function code section.
File --> New File; It will open text editor windows.
For starting your instances, enter the following code into the Function code editor:
import boto3 # Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1' or 'eu-central-1' region = 'XX-XXXXX-X' # Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX'] instances = ['X-XXXXXXXX'] def lambda_handler(event, context): ec2 = boto3.client('ec2', region_name=region) ec2.start_instances(InstanceIds=instances)
File --> Save
Note: include .py as an extension during the file saving.
For stopping the instance:
import boto3 # Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1' or 'eu-central-1' region = 'XX-XXXXX-X' # Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX'] instances = ['X-XXXXXXXX'] def lambda_handler(event, context): ec2 = boto3.client('ec2', region_name=region) ec2.stop_instances(InstanceIds=instances) print stopped your instances: ' + str(instances)
Save the complete configuration as shown below. Example: startec2instances
Please note the region and instances id.
For the region, you can check your EC2 Dashboard URL. For Example https://eu-central-1.console.aws.amazon.com/ec2/v2/home?region=eu-central-1#Home:
The information for instances ID can be found by visiting the ECS Dashboard.
Test your newly created functions
- Open the AWS Lambda console, and choose Functions.
- Select your function, and then choose Test.
- In Event name, enter a name and then choose to Create.
Note: The body of the test event doesn't impact your function because the function does not use it.
If you get any error message, please check it and try to test the function again.
We have now successfully created the function which can start amazon instance. Now, it is time to create a CloudWatch Event that triggers our Lambda function at a specific period of time.
Open the Amazon CloudWatch console.
Choose Events, and then choose Create rule.
Choose Schedule under Event Source.
Enter an interval of time or cron expression that tells Lambda when to stop your instances.
For more information on the correct syntax, see Schedule Expression Syntax for Rules.
Note: Cron expressions are evaluated in UTC. Be sure to adjust the expression for your preferred time zone.
Choose Add target, and then choose Lambda function.
For Function, choose the Lambda function that starts your instances.
Choose Configure details.
Enter the following information in the provided fields:
For Name, enter a meaningful name, such as „Start_ec2_instance_everyday_530GMT"
For Description, add a meaningful description, such as “starts EC2 instances every working day at 530 GMT”
For State, select Enabled.
Choose Create rule.