Case study

CategoryArticles

AWS Lambda Pricing: Cost Optimization Approaches Guide

In this article, you will learn:

There are plenty of benefits to going serverless, from low latency to scalability to simple deployment. With AWS Lambda, you only pay for the time your code is running. This model forever changes the relationship between application code and infrastructure costs. The hardware is automatically provisioned with every aspect when needed and billed accordingly. There is no need to manage and sometimes overprovision servers to cope with the load.

In this article, we’ll take a look at the AWS Lambda pricing model, some things you need to keep in mind when using it and how to optimize the cost of Lambda functions.

How does AWS Lambda pricing work?

There are multiple AWS Lambda pricing options:

  • Free-tier: The AWS Lambda free tier includes one million free requests per month and 400,000 GB-seconds of compute time per month, usable for functions powered by both x86 and Graviton2 processors, in aggregate.
  • Lambda on-demand: Your costs are determined by pay-as-you-go pricing and the actual usage of AWS Lambda.
  • AWS Lambda saving plans: These plans are a flexible pricing model that offers low prices on AWS Lambda usage in exchange for a commitment to a consistent amount of usage for a one- or three-year term. With these plans, you can save up to 17 percent.

In general, Lambda functions run only when triggered and the calculation of the cost of running your Lambda function is based on:

  • A number of executions/invocations.
  • Duration of the execution in milliseconds.
  • Memory size (Between 128 MB and 10 240 MB).
  • Usage of AWS Lambda provisioned concurrency

AWS uses GB-seconds as the main unit, so you will need to use this formula:

38_lambda-pricing-7.png

For each Lambda function, you can set the maximum memory size and maximum function execution time. The more memory you provision, the more CPU your function gets.

In general, you will need to add costs for DTO (data transfer out) and other services like Amazon API Gateway, Amazon S3, Amazon CloudFront, etc. But this blog post is focused on AWS Lambda.

AWS Lambda request charges in Europe and US regions:

Lambda GB second charges in Europe and US regions:

How to calculate AWS Lambda costs

It’s best to go through a simple example when you need to understand how to do this.

Here’s an example of AWS Lambda costs calculation.

We assume that this example organization is in Europe (Ireland), but this example will work for every US and European region because the pricing is the same for them:

  • You allocate 512 MB of memory to your functions.
  • Execute it 5 million times in a month.
  • Run approximately for 200 milliseconds each time.

Do not forget that AWS offers every account holder one million free requests and 400,000 GB-seconds of compute time monthly.

Thus, you calculate an AWS Lambda bill like this:

38_lambda-pricing-4.png

This example takes into account only AWS Lambda pricing. You may pay more depending on the other AWS services you use with Lambda, such as data transfer charges.

Having problems with your AWS Lambda costs estimation? Contact us and our team of AWS certified specialists will help you with the calculations.

Contact us

AWS Lambda vs Amazon EC2 cost comparison

Amazon EC2 can provide the stability that developers can take advantage of to build robust web applications, or it can provide on-demand usage by Auto-Scaling, which is primarily billed at the time the instances are running.

When comparing AWS Lambda pricing, it's important to understand the characteristics of your application. While Lambda is billed based on the GB-seconds your function uses during execution, with EC2 you need to think more about machine design as the cost increases with the size of the machine you choose, and you also need to add the cost of high availability, safety, and the compliance of these machines.

With EC2 instances, you must fully develop/manage the contents of the machine, from choosing a web server to launching application instances through middleware to respond to user requests. You also typically need to run EC2 instances across multiple AZs using an Elastic Load Balancer, which can incur significant costs.

Compare this to AWS Lambda, where you only need to write your application's functions at the most granular level. The developer's time investment can be higher, as can the costs involved. But you will most likely pay less for running, managing, and also for the high availability and security of these functions.

AWS Lambda cost advantages and disadvantages

Lambda cost advantages

Comparing a traditional resource (EC2 instance) to a serverless resource like AWS Lambda will always be use-case dependent, but there are a few cost advantages that can lend weight to choosing Function-as-a-Service (FAAS) architecture over a traditional approach:

38_lambda-pricing-8.png

1. Low usage or light workloads = Low cost

For light workloads, AWS Lambda is cheap, especially if your usage falls under the fairly generous free usage tier. Depending on your specific use case, a simplified Lambda function infrastructure can significantly outperform equivalent EC2 instances, which require additional development and configuration.

2. Control and testing capabilities

If your Lambda charges are higher than you expected, you can quickly take control and try to change your code, change the memory size or limit the execution time.

3. Granularity

AWS Lambda gives you a more granular cost measurement than EC2, which is billed by time rather than resource usage. You can get a per-execution view of the resources used by your Lambda function, and you can use this data to more accurately predict the cost of future runs.

4. Cost-based pricing of your applications

With each run, you can clearly see what expenses are incurred by your organization. When this data is fed into a pricing model, it can be used to reduce the cost per request.

Combining this with a dynamic pricing, you can change how much you charge users based on their actual resource usage. This cost-based pricing allows you to ensure you're being fairly compensated for the effort you put into your app, rather than relying on a flat monthly fee that's profitable for only a portion of your user base.

Lambda cost disadvantages/challenges

While the flexibility of Lambda over the traditional approach gives you a lot of options in optimizing your application’s resource expenditures, the serverless approach introduces some unique challenges. These challenges can add unpredictability to your application’s running costs if not carefully monitored and managed.

38_lambda-pricing-5.png

1. Forecasting Costs

One of the hardest parts of optimizing an application is finding out exactly how many resources your application consumes. This is doubly challenging in a serverless environment, as each application path can include an indeterminate number of Lambda invocations. This complicates cost forecasting, adding confusion to infrastructure budgeting.

2. Cost explosions

Lambda can be very susceptible to cost increases due to heightened usage. While ideally this heightened usage results from an increasing user base, lending some predictability to the growth, there are several scenarios where you can end up with a large bill with very little warning. DDoS attacks, for example, can very quickly drive your Lambda bills through the roof.

Simple development bugs can result in greatly inflated costs if your developers, for example, add an infinite loop or fail to make use of caching.

3. Complexity and usage of other services

Building a robust serverless application doesn't stop at the invocation of your Lambda functions. You need services to store user data (Amazon S3), additional development and maintenance load for the disparate functions of your application, and so on.

Each of these elements has the potential to increase your costs in a way that wouldn’t necessarily be present in a more traditional EC2-based application.

Optimizing AWS Lambda Costs

With the flexibility of serverless applications, you gain the advantage over traditional applications when it comes to optimizing execution costs. The granularity of AWS Lambda billing and how often you receive related information gives you a lot of flexibility in optimizing your application's resource usage.

38_lambda-pricing-6.png

1. AWS regions and data transfer

One of the ignored costs in AWS Lambda development is the cost of data transfer between AWS regions. If your AWS Lambda function uses AWS resources spread across multiple regions, you’re paying for the data transfer between those services. Moving your Lambda functions to the same region as the resources they depend upon can reduce your data transfer costs.

You’ll also be charged for the data transfer between the internet and your functions at standard EC2 data transfer rates (0.09$ per GB). At StormIT, we have helped many customers to optimize their overall spending on data transfer (up to 60%) by using Amazon CloudFront CDN.

StormIT offers custom Amazon CloudFront pay-as-you-go pricing and you pay only for what you use.

Estimate savings

2. Optimize memory allocation

Choosing the right size of Lambda memory means that with AWS, you can set the memory, and in turn CPU, of your AWS Lambda functions. There’s no need to over-provision. It is quite easy to switch to the memory option which will cover your new requirements.

AWS doesn’t allow you to customize the CPU for Lambda functions, but the more memory you allocate, the more computing power you get. There are a few caveats to this strategy, though. For example: over 2 GB of RAM, Lambda will allocate a second vCPU to the function. In this case, single-threaded programs won’t see any speed gains from increasing memory.

3. Writing efficient code

Quite simply, a Lambda function that runs in half the time is a function that costs you half the money. The execution time is proportional to the amount you charge. If the execution takes a long time, it is wise to change your function and iterate.

You can leverage the duration metric inside the CloudWatch or you can try using AWS X-Ray to analyze and debug your applications.

4. Caching

If your code is not running, you will not be charged. Proper Lambda caching methods can also have a significant impact on your AWS Lambda running costs. By adding caching in your serverless functions or client-side caching in your front-end code, you can reduce costs by doing less work.

Conclusion

There are other strategies to reduce cost and optimize it for your own needs and there are various choices to make to reduce costs and optimize them for your own needs.

The essential thing you need is to discover which of the given approaches best suits your personal needs. After finding out what you need, it will be easier to choose a more personalized way to reduce costs.

The StormIT team has the AWS Service Delivery Program designation for AWS Lambda and helps organizations to redesign their legacy applications or release new ones by leveraging the AWS Lambda and other AWS serverless services. Got a question? Please get in touch.

Similar blog posts

See all posts
CategoryCase Studies

Windy - The Extraordinary Tool for Weather Forecast Visualization

StormIT helps Windy optimize their Amazon CloudFront CDN costs to accommodate for the rapid growth.

Find out more
CategoryCase Studies

AWS Well-Architected Review Series: Healthcare Industry Client

Transforming healthcare AWS operations with StormIT using our expertise and the AWS Well-Architected Framework. Learn more.

Find out more
CategoryCase Studies

Srovnejto.cz - Breaking the Legacy Monolith into Serverless Microservices in AWS Cloud

The StormIT team helps Srovnejto.cz with the creation of the AWS Cloud infrastructure with serverless services.

Find out more
CategoryCase Studies

AWS Well-Architected Review Series: Renewable Energy Industry Client

See how StormIT optimized a renewable energy client's AWS infrastructure through the Well-Architected Framework. Explore now...

Find out more
CategoryCase Studies

Microsoft Windows in AWS - Enhancing Kemper Technology Client Solutions with StormIT

StormIT helped Kemper Technology Consulting enhance its technical capabilities in AWS.

Find out more
CategoryNews

Introducing FlashEdge: CDN from StormIT

Let’s look into some features of this new CDN created and recently launched by the StormIT team.

Find out more