How to Stop and Start Amazon Ec2 Instances at Regular Interval of Time
Administration is all about deploying stable, scalable and secure environments. Cost optimization is the also plays a very important role into this. Administrator should be smart enough to manage all these things in cost-effective manner. In this tutorial we are trying to demonstarte a smart way of deploying amazon ec2 instances, in which we can Stop and Start Amazon Ec2 Instances at Regular Interval of Time and use them when they are needed.
Create AWS Identity and Access Management Policies and Roles
To create a new IAM policy and role
Open the IAM console at https://console.aws.amazon.com/iam/.
- In the navigation bar, choose Policies.
- Choose Create policy.
- On the JSON tab, paste the following code into the editor:
- Choose Review policy.
{ "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": "*" } ] }
- On the Review policy screen, complete the following sections:– For Name, type a name for the policy.– For Description, type a brief description of the policy.
- Choose Create policy.
- In the navigation bar, choose Roles.
- Choose Create role.
- Under Select type of trusted entity, choose Lambda, and then choose Next: Permissions.
- On the Attach permissions policies screen, check the box next to the name of the policy you just created, and then choose Next: Review.
Create an AWS Lambda Function
- Open the AWS Lambda console, and choose Create function
- Choose Author from scratch.
- For Name, type a name for the function.
- From the Runtime drop-down menu, choose Python2.7.
- For Role, select Choose an existing role.
- For Existing role, choose the IAM role you created above with name “ec2-start-stop“.
- Choose Create function.
To stop your instances, enter the following 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' 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)
- In Basic settings, enter 10 seconds for the function Timeout.
- Choose Save.
Repeat these steps to create another function that starts your instances again by using the following:
import boto3 # Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g.; 'us-east-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) print 'started your instances: ' + str(instances)
NOTE: You can use the previously created role.
Test your functions
- Open the AWS Lambda console, and then choose Functions.
- Choose your function, and then choose Test.
- In Event name, type a name, and then choose Create.
- Choose Test to execute the function.
- Choose Test. When the function finishes running, expand the Details section. If the Lambda function was configured properly, you will receive one of the following messages: —
null
: Indicates that the function ran without errors.
Create a CloudWatch Event that triggers your Lambda function at night
- 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.
- Choose Add target.
- Select Lambda function.
- For Function, choose the Lambda function that stops your instances.
- Choose Configure details.
- Use the following information in the provided fields:
–For Name, type a meaningful name, such as “StopEC2Instances.”
–For Description, add a meaningful description, such as “stops EC2 instances every day at night.”
–For State, choose Enabled. - Choose Create rule.
This brings an end to this tutorial. We hope this tutorial has added some values to you knoledgebase. Do share and come back to https://linuxtogether.org for more future updates.
Thanks!!!
Recent Comments