Automate AWS EC2’s Start and Stop using Lambda and CloudWatch - CNDRO.LLC
1865
post-template-default,single,single-post,postid-1865,single-format-standard,wp-custom-logo,theme-bridge,bridge-core-2.9.4,woocommerce-no-js,tribe-no-js,ehf-template-bridge,ehf-stylesheet-bridge-child,qode-page-transition-enabled,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,columns-4,qode-child-theme-ver-1.0.0,qode-theme-ver-27.8,qode-theme-bridge,qode_header_in_grid,wpb-js-composer js-comp-ver-6.7.0,vc_responsive,elementor-default,elementor-kit-2634

Automate AWS EC2’s Start and Stop using Lambda and CloudWatch

This post is a quick guide on how to automatically start and stop EC2 instances on Amazon Web Services (AWS). Amazon Elastic Compute Cloud (EC2) instances are like virtual servers on AWS. AWS is a cloud computing platform and EC2s help business subscribers run application programs on AWS.

Let’s say, for example, a particular EC2 instance has been placed in your care by your organization. It needs to be turned on by midnight and turned off by 8 pm in the evening, which is quite absurd. You can choose to wake up every midnight to turn it on and cut your dinner date short to turn it off. You can also choose to dedicate 30 minutes only this one time to automate the entire process.

In this post, we will quickly walk through how to automatically turn on and turn off these EC2s using Lambda and CloudWatch. All you need is an AWS Console root user or an IAM user with Lambda, CloudWatch and EC2 access. Oh!… and of course, 30 minutes of your time.

STAGE 1: CREATE THE LAMBDA FUNCTIONS TO AUTOMATICALLY START/STOP EC2 INSTANCES

Login to AWS Console: This is, of course, the first thing to do.

Click the ‘Services’ drop-down menu and select Lambda under the Compute serviceChoose the ‘Author from scratch’ option and a Basic Information form pops up with three fields: Function, Runtime, and Permissions.

Put in your function name; “Start EC2” for example. Input your preferred programming language in the Runtime field. Python 2.7 was used in this case. In the Permissions field, you can choose to create a new role with basic Lambda permissions or use an existing role, if you already have one. To learn how to create a new role, check out our article on How to create an EC2 role.

Hit that orange Create Function button to display a page where you can input your code.

To create a Lambda function that automatically turns on an EC2 instance. Edit the code in the snippet below with the region and instance ID of the EC2 you are trying to automatically start.

import boto3

region = 'insert ec2 region'

instances = ['insert ec2 instance ID']

ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):

    ec2.start_instances(InstanceIds=instances)

    print('started your instances: ' + str(instances))

To create a Lambda function that automatically turns off an EC2 instance. The code is similar to the one in the snippet above, ‘start’ is just replaced with ‘stop’:

import boto3

region = 'insert ec2 region'

instances = ['insert ec2 instance ID']

ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):

    ec2.stop_instances(InstanceIds=instances)

    print('stopped your instances: ' + str(instances))

Our lambdas are ready, up next is CloudWatch.

STAGE 2: CREATE CLOUDWATCH RULES TO TRIGGER THE LAMBDA FUNCTIONS

Back to AWS Console.  Click the ‘Services’ drop-down menu and select CloudWatch under the Management and Governance service. In the CloudWatch dashboard, select Rules

Hit the Create Rule button, select the ‘Schedule’ option and input the date and time you want your EC2 to start in the Cron expression field.

In this study, the EC2 was set to start at 1:05 am every Monday to Thursday with a Cron expression of :

05 01 ? * MON-THUR*

If you want to start your EC2 at 8 pm every working day of the week, for example, your Cron expression is going to be:

00 20 ? * MON-FRI* 

In the Targets section, select the Lambda function created earlier to turn on an EC2 instance. Hit the Configure details button, type in your preferred rule name and Create.

The same process is undergone to create a rule that will trigger the stop Lambda function. The only thing that changes is the Lambda function you are targeting.

Follow these steps properly to automatically turns on and off EC2 instances with Lambda and CloudWatch on Amazon Web Services (AWS).

No Comments

Post A Comment