AWS Lambda is a serverless platform that allows users to execute stateless, short-running functions in a variety of languages. One of the main benefits, the fact that these function executions can scale automatically to meet the needs of your application, also means that the pricing can vary based on how much you use it. Especially for teams coming from a more traditional server-based architecture where costs are mostly fixed on a per-month basis, this is a large shift.
In this article, we’ll take a look at some of the specifics around AWS Lambda pricing, examine some ways to save money on your AWS Lambda bill and explore how to utilize the free tier that AWS provides.
Understanding AWS Lambda
AWS Lambda is a serverless compute service provided by Amazon Web Services (AWS). It allows developers to run code without provisioning or managing servers. With AWS Lambda, you can execute code in response to certain events, such as changes to data in an Amazon S3 bucket, updates to a DynamoDB table, or HTTP requests sent through Amazon API Gateway.
In sharp contrast to the traditional always-running server architecture, AWS Lambda functions only run when they’re invoked for as long as they need to in order to complete their designed task. While this allows them to scale more easily, it also introduces the need for additional architecture components to allow functions to communicate with each other or with external resources.
Commonly, this serverless architecture is used for workflows such as parsing logs, generating alternate images sizes for imagery uploaded to an S3 bucket and many other applications where the workflow can be condensed down to a single, event-driven function.
AWS Lambda pricing model
Because AWS Lambda functions only run when they’re invoked, you only pay for the time that your functions are running. This makes the much more cost effective for many workflows, especially ones where your code only needs to run sporadically. To determine how much a specific function invocation costs, AWS measures the duration from when your function is invoked until when it complete, down to the nearest 1ms. It then combines that with the amount of memory your function has to use to decide what the cost will be.
For example, in the US East region, if your Lambda function has 128MB of memory provisioned, each function invocation will cost $0.0000000021 per millisecond that it runs. If instead you have 1024MB of memory available to that same function, it will cost $0.0000000167 per millisecond.
For more complicated use cases, AWS has a Lambda pricing calculator that can accept many more variables and help you figure out a closer pricing estimate for your use case.
Free tier
One of the other main factors that affects Lambda pricing is the free tier that AWS provides.
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.
- AWS Lambda Pricing
This is a very important factor to consider, as it makes many small to mid-size deployments of AWS Lambda functions completely free. This also means that if you can refactor your AWS Lambda deployment to fit within these limits or at least come close, you can greatly reduce your AWS Lambda costs.
How can you optimize your AWS Lambda costs?
If, however, you fall outside the free tier, there are still ways that you can optimize your Lambda costs.
Fine-tune memory settings
Because function cost are primarily associated with how much memory a function uses and how long it runs, refactoring your code to use less memory is one of the most straightforward ways to reduce your Lambda cost. For example, if you read a large file into memory or download a particularly large dataset as part of your function execution, storing that data in a database and querying it as necessary or streaming that data instead of downloading the entire thing can reduce your memory needs.
Even before you look at refactoring your code, you should use the Monitor
tab of the Lambda console to check how much memory your function is actually using. If your function is only using 270MB of memory, for example, and you have 1024MB provisioned, you can adjust your function to only have 512MB provisioned and instantly recoup that savings.
Reducing function execution time
Since execution time is the other metric that AWS uses to determine how much your Lambda functions cost, reducing the time that your functions takes to execute is the other main way to reduce your cost. For example, if your Lambda function relies on an API that takes a long time to return, caching the return value of the API so at least some of your function invocations don’t have to wait that amount of time would be one way to reduce your overall execution time.
Leveraging provisioned concurrency
If your Lambda functions are running frequently, you may be able to take advantage of what AWS calls “provisioned concurrency”. With provisioned concurrency, you can reserve a certain number of instances of your Lambda function, keeping them warm and ready to handle requests instantly. Instead of waiting for the environment to initialize upon each invocation, the provisioned instances remain ready to process requests immediately, significantly reducing response times and improving the overall performance of your serverless applications. According to some estimates, using provisioned concurrency saves 70% of the cost of each millisecond of runtime.
The downside to provisioned concurrency is that there is a fixed cost per month (similar to maintaining your own server) so if you don’t use your Lambda functions enough for the savings to offset this fixed cost, you end up spending more money by using it.
Airplane as an alternative serverless platform
If, however, you’re interested in a serverless platform for running your scripts and functions where you don’t have to spend time relentlessly optimizing costs, check out Airplane.
With Airplane, not only do you get a generous free tier, your pricing scales as you need to onboard more users, meaning that your costs stay predictable. And in addition to a function-as-a-service platform like you get with AWS Lambda, with Airplane you can easily schedule your functions to run, quickly set up permissioning and approval workflows and even build React-based UIs with an included component library.
Sign up for a free account today and get your first function running in minutes.