Stop and start EC2 instances at regular intervals using AWS Lambda

 

You can use a CloudWatch Event to trigger a Lambda function to start and stop your EC2 instances at scheduled intervals.

For that , you will create Lambda functions to start and stop EC2 instances and then create CloudWatch Events that trigger your instances to start in the morning and stop at night.

  • Open the AWS Lambda console and select the Create a Lambda function.

When prompted to select a blueprint choose Blank Function

  • Choose Configure triggers if it is not already selected, and then choose Next. You will configure a Lambda trigger later

  • Enter the following information to configure your Lambda function:
    • Name : enter meaningful name
    • Description : enter description regarding lambda function
    • Run time : select python 2.7

  • To stoop your instances, enter the following sample code in lambda function code and choose next

import boto3

# Enter the region your instances are in, 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)

  • Expand the Role drop-down menu and choose Create a custom role. This should open a new tab or window in your browser.

  • Choose View Policy Document, Edit, and then edit the policy as follows and choose Allow

{
“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 Next to review your function configuration, and then choose Create function.
  • Repeat steps 1-4 and 9 to create another function that will start your instances again,Use the following code:

import boto3

# Enter the region your instances are in, 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)

  • To test your newly created functions:
    • From the Lambda console, choose Functions, select your function, and then choose Test
    • Your function doesn’t use the test event, so from the Input test event editor just choose Save and test.
  • Create a CloudWatch event that will trigger your Lambda function
  • Open the cloudWatch console.
  • Choose Events, and then choose Create rule.
  • Select Schedule under Event Selector
  • Enter an interval of time or cron expression that will tell Lambda when to start your instances

  • Choose Add target.Under Targets, choose Lambda function
  • For Function, choose the Lambda function that starts your instances and Choose Configure details.

  • Enter the information in configure rule details and select create rule

  • To stop your instances in the night, repeat these steps using your preferred time.
  • To restart your instances in the morning, repeat these steps using your preferred time.

https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/ #link to follow

I hope you like this article!!!!!

Thank you..
Aarti Parikh
AWS-team,
Solution Architect,
Electromech Corporation.

 

One thought on “Stop and start EC2 instances at regular intervals using AWS Lambda

Leave a Reply

Your email address will not be published. Required fields are marked *

*