DEV Community

Cover image for 6 AWS Lambda Cost Optimization Strategies that Work
Taavi Rehemägi for Dashbird

Posted on

6 AWS Lambda Cost Optimization Strategies that Work

In 2021 it's common practice for businesses to use a pay-as-you-go/use pricing model. It's no different with Amazon. It's also the primary reason why this article is such an important read for all those looking to reduce their AWS Lambda costs

In this article, we will go over six actionable strategies to optimize the cost relating to our AWS Lambda usage.

What is AWS Lambda Cost Optimization?

One of the main reasons for choosing to move into the cloud is the ability to reduce costs. It's essential to optimize how much you spend, so you only pay for what you need and only when you need it. Optimizing costs will help your organization get the most out of your investment, helping meet demand and capacity while using the most economically useful options that AWS has to offer.

Cost optimization allows you to decide how much, when, and in which cases you'll pay for the service provided to you. AWS will allow you to easily pick the right size for your service and leverage memory size based on how much you need.

For a better overview, you can use tools like Dashbird AWS Lambda function cost tracking tool that will allow you to monitor the cost of your Lambdas in real-time with detailed insights. That way, you'll always be on top of how much you're spending on this service.

1. Caching Lambda Responses

For some Lambda functions, like the ones called from an API Gateway event, AppSync, or Lambda@Edge functions called from CloudFront, you can cache responses. A function that isn't called doesn't cost you any money, so make sure you don't hammer your Lambda function if its responses don't change that often.

Lambda@Edge functions are costlier than regular Lambda functions, but if you only call them once every few minutes and deliver the cached response to thousands of users per second, you can significantly reduce your bill.

One benefit of caching is cheaper responses because you don't pay for Lambda. Another benefit is that you'll see faster responses because you remove the extra way the request would have to travel if Lambda was involved. 

In the business sense, this could also uplift revenue by providing a better experience for the end-user. Lambda cost optimization means you not only reduce costs, but implementations could end up driving more revenue.

To enable caching for API Gateway deployed with AWS SAM, you can use the MethodSettings attribute.

Alt Text

You can use the * wildcard in HttpMethod and ResourcePath to configure caching for multiple routes in your API.

2. Minimize Lambda Usage

Don't use Lambda functions for simple transforms. If you're building an API with AppSync or API Gateway this is often the case. You implemented authentication Cognito and custom authorizers in your API Gateway and now just want to push data directly to upstream services like DynamoDB or SQS.

API Gateway supports the Velocity Templating Language, a simple programming language that can transform the JSON objects of API Gateway requests. They can't do everything, but they neither have cold starts nor incur extra costs as Lambda functions do.

Keep in mind that optimizing with VTL is not too straightforward, but it can be worth your time if you have frequently called endpoints that don't require the full power of Lambda. Richard Boyd, a developer relations engineer at AWS, wrote a bit on that topic

3. Building Small Lambda Functions

The goal of all your AWS Lambda function is t*o be small and purpose-built*. If a Lambda function only does one thing, you only have to optimize this specific use case. When you end up with multiple use-cases for one function, you can end up making compromises to satisfy all of them.

Function size is also part of the costs. The Lambda runtime has to get your function's code from S3 or a Docker image registry on every cold start. Downloading one gigabyte takes Lambda much more time than downloading one megabyte. This is the waiting time you pay for

Since 2021 Lambda functions are billed in one millisecond increments, you can now save money for every millisecond your function runs faster. So getting your Lambda code down to the absolute basics can increase your savings for functions that run very often.

4. Never Call Lambda from Lambda

Again, this is most important for synchronous Lambda calls, which happen with API Gateway.

If you call a Lambda function directly from within a Lambda function, you pay for both of them. The first one will wait for the second one to finish, and you'll be paying for the waiting time

If you need to call multiple Lambda functions, try to finish the synchronous API Gateway Lambda function early, and start the process of running the other functions with some extra service.

AWS has multiple queuing services to offer. SQS, SNS, Kinesis, and Step Functions are a few of them. When the heavy task is done, you can notify the clients with WebSockets or email.

5. Choosing the Ideal Memory Configuration

After you made sure you only call functions when needed and kept them all small and purpose-built, you're enabled to control your AWS cost by following a few straightforward steps. 

First off, we'll mention choosing the right size, which means that with AWS, you can set the memory, and in turn CPU, of your AWS Lambda functions to meet precisely the necessary capacities that you need.

There's no need to over-provision or make compromises. Adapt your services to address the actual business needs at any given time, without any penalties or hidden fees whatsoever. AWS allows you to choose between services that meet your criteria, and while your demands change, it is quite easy to switch to the service option which will cover your new requirements. AWS also allows you to run multiple service options simultaneously, helping you reduce costs while maintaining optimal performance at all times.

Another way would be utilizing Step-Functions to find the optimal memory capacity for your functions. Here's an open-source module built by Sr. Tech. Evangelist Alex Casalboni of AWS.

6. Cost Reduction with Observability

With a proper serverless observability system in place, your company will for sure minimize the risks that inherently come with serverless architectures. You will also have a way to manage the budget predictably, in a way that complies with policies that require commitments on a long-term basis.

Dashbird single function view

Dashbird single function view

This would include monitoring, tracking, analyzing, and alerting your service usage. With a trusted advisor, you can provision your resources by keeping up with the best possible practices to improve system performance and reliability.

It will also increase security and give you opportunities to save some money. CloudWatch is an option that (in case you decide to turn off non-production functions) will allow you to match increases or reductions in demand. It will collect and track the metrics, monitor log files, and automatically respond to any changes made within your AWS resources.

But it doesn't give you full insight into your system or instant alerts when things break. For that, you need a tool that will make sure to alert you instantly when your system is misbehaving. Dashbird is such a tool that gives you insights into your Lambda functions, all in one place.

More Opportunities

lambda cost view

There are other ways to reduce cost and optimize it for your own needs. Dashbird's Cost Explorer can help you with analyzing your usage and cost. It is a magnificent tool that allows you to use a set of default reports to identify cost drivers and usage trends. Dashbird's own cost tracking system, which you can see on an account-wide scale or per-function basis, also gives you a real-time presentation of how much your services are costing you.

There are various choices to make and strategies to reduce costs and optimize them for your own needs. The essential thing you need to realize is discovering which of the given approaches best suits your personal needs. After finding out what you need, it'll be much easier to choose a more personalized way to reduce costs. 

Further reading:

AWS Lambda metrics that you should be monitoring

4 tips for tuning AWS Lambda for production

Quick ways to cut cost on your AWS Lambda

Top comments (0)