DEV Community

Tom Watt
Tom Watt

Posted on

AWS - Save Costs with Lambda

When I first started at my current role, AWS was completely new to me. And I mean everything. I'd worked previously dealing with physical servers and now I was just dealing with the CLI and Web Console to interact and manage servers.

One thing that caught me by chance was when I saw the billing every month and wondered why were we paying for development instances, databases, etc. when no one was there to work on them?

Whilst learning more as time went by, I found inspiration and use from previous colleagues works to deal with these unused resources. And that's when I found use and power of AWS Lambda.

I won't go in-depth but keeping things simple, using Crons from CloudWatch Events to trigger Lambdas that shutdown or terminate instances saves money.

Why is that? Because in most cases, unless you use Lambda a lot, it's free under the AWS Free Tier and such simple tasks such as deleting EC2 instances run take less than 1 second. Finally, it is automated.

Here's an example of what is needed to filter and terminate EC2 instances with specific tags:

import boto3

EC2_CLIENT = boto3.client('ec2')
EC2_RESOURCE = boto3.resource('ec2')

PROJECTS = ['super', 'special', 'ultra',]
ENV = ['dev']

def get_ec2_instances():
    ec2_instances = EC2_RESOURCE.instances.filter(
        Filters=[
            {
                'Name': 'tag:project',
                'Values': PROJECTS
            },
            {
                'Name': 'tag:env',
                'Values': ENV
            },
        ]
    )

    return [instance.id for instance in ec2_instances]

def delete_ec2_instances(instance_ids):
    if instance_ids:
        EC2_CLIENT.terminate_instances(InstanceIds=instance_ids)

def delete_instances(event=None, conext=None):
    delete_ec2_instances(get_ec2_instances())
Enter fullscreen mode Exit fullscreen mode

You can easily use environment variables, add logging and error handling if needed but sometimes keeping it simple is the best.

And if you need to change when instances get terminated, just update your CloudWatch Event.

Top comments (2)

Collapse
 
adir1661 profile image
adir abargil

man you are awesome!

Collapse
 
tomowatt profile image
Tom Watt

You are too kind, I hoped this helped!