CloudBerry Lab Resources
Get started with cloud backup and management solutions

Save up to 50% on Your Non-Production EC2 Instances with Lambda and Resource Tagging

Published: by on Post Type: Categories: AWS

In this article, we will describe the use case for AWS Lambda that allows you to manage your Amazon EC2 instances in a more intelligent way and save some money.

Before we begin

In case you’re using Amazon Web Services to run a development or testing environment in addition to your high-availability production instances, you can benefit from using only what you need and when you need it. Why should you pay for an instance running for 24 hours when you need it for 8 business hours only?

One of the options is to manually monitor your environment and shut down instances that aren't used, but it's better to automate the process. You can use AWS Lambda combined with proper resource tagging to achieve that. Here is how.

Step 1: Tag instances you want to be automatically switched off

With tags, you can categorize your AWS resources in different ways, for example by purpose, by owner, or by the environment. Each tag includes a key and an optional value, defined by a user. In the example below, I will use tags in order to mark instances I want to shutdown using our lambda code.

    1. Choose a region and an EC2 instance from your resources list.
    2. On the details pane below, choose Tags.
    3. Press Add/Edit Tags and specify a tag with the key=shutdown and with the value=true
    4. Perform these steps for all the instances you want to schedule the shutdown.

Step 2: Create a Lambda function

Lambda is an Amazon service that allows running code on its own compute fleet of Amazon EC2 instances across multiple Availability Zones in a region, which provides the high availability, security, performance, and scalability. In other words, we can run our code by demand without having to manage entire server to host it. To create a Lambda function:

  • Go to AWS Lambda, choose to Create a Lambda function, it will show you a set of predefined examples. Skip it and specify a name for your function. Choose Python 2.7 in the Runtime list box.  Note: You can use any other language supported by the service. We are using Python just to demonstrate the concept.
  • Copy and paste the following code to the box underneath:
from __future__ import print_function

import boto3

def lambda_handler(event, context):

    flag = False

    ec2 = boto3.client("ec2", region_name="eu-west-1")

    description = ec2.describe_instances()

    for instances in description["Reservations"]:
        for instance in instances["Instances"]:
            for tag in instance["Tags"]:
                if (tag["Key"] + tag["Value"]) == "shutdowntrue":
                    if instance["State"]["Name"] == "running":
                        print("Stopping " + instance["InstanceId"])
                        ec2 = boto3.resource("ec2", region_name="eu-west-1")
                        instance = ec2.Instance(instance["InstanceId"])
                        instance.stop()
                        flag = True
    if not flag:
        print ("All instances are already stopped")

 

  • Define the permissions for your function by choosing appropriate IAM role in the Role list box. List permissions that are necessary to successfully execute our script are below.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1453628436000",
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeInstances",
                "ec2:StopInstances"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        }
    ]
}
  • You will be asked to choose how much memory should be dedicated for processing the code. Keep 128 MB, as it’s enough.
  • If you have a large number of instances to process it may take more than default 3 seconds. You may need to increase a timeout for a function
  • Press Next, hit Create a function
  • Set up a schedule for your function, go to Event sources, press Add event source, choose CloudWatch Events - Schedule, specify the following statement in the Schedule expression field:
    cron(0 19 * * ? *)

    According to it, the function will be started every day at 19:00 UTC. You can find more examples of cron configuration here:http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/ScheduledEvents.html

Conclusion

Now we have enabled scheduled shutdown of our Amazon EC2 instances. If you want to add an instance, you don’t have to modify a function, just add a shutdown tag to the instance you want to include. Same thing with the exclusion of your instances. If for some reason you don’t want to turn off your instance during the night, just remove a tag.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...