Deciphering Cost Optimization Secrets for Serverless Success
By KHUSHI CARPENTER, NANDAN GADHETHARIA, SAUMIL SHAH / Jul 18, 2023
In today's technology-driven landscape, optimizing costs while harnessing the power of AWS Lambda has become a critical consideration for tech leaders. In this blog, we will explore the key strategies and techniques that tech leaders can employ to drive cost efficiency and maximize business growth within the AWS Lambda environment. By understanding the tradeoffs involved and emphasizing the broader impact of cost optimization decisions, businesses can position themselves as leaders in their industry, capturing new opportunities and driving innovation.
AWS Lambda, a serverless compute service offered by Amazon Web Services (AWS), provides a scalable and cost-efficient solution for running code without the need for provisioning or managing servers. Lambda operates on the principle of "pay-as-you-go," charging users based on the actual compute time and resources utilized. Functions are triggered in response to events, and AWS takes care of scaling and provisioning resources as needed. However, Lambda functions can quickly add up in price if they are not optimized. In this article, we will explore strategies and best practices to help you optimize your Lambda functions and reduce your AWS costs.
To gain a comprehensive understanding of Lambda's pricing structure, let us delve into its pricing structure.
Lambda Pricing Decoded
Lambda has a pricing structure based on several factors. Here is a breakdown of the Lambda pricing components:
- Invocations: AWS Lambda charges you based on the number of requests made to the functions. Each time the function is triggered, a request is counted. There are different pricing tiers based on the total number of requests per month.
- Duration: The duration is the time it takes for your function to execute, measured in milliseconds. AWS Lambda rounds up the duration to the nearest 100 milliseconds. You are billed for the total duration across all requests for your functions.
- Memory Allocation: You specify the amount of memory allocated to the Lambda function. The pricing is based on the memory allocated, and you are charged per gigabyte-second. This means the total memory allocated multiplied by the function's duration in seconds.
Let's delve into the pricing structure of AWS Lambda and explore an example to understand how the pricing is calculated in greater detail.
Below is the pricing for lambda.
Lambda pricing for invocations
Lambda pricing for allocated memory
Note: Requests are priced per million, with the first million free, while duration is billed per 100 milliseconds.
Assume you have a Lambda function that is triggered by an API Gateway endpoint. The function is allocated 512MB of memory, and it receives 500,000 requests with an average duration of 200 milliseconds per invocation.
Number of requests = 500,000 requests
Price per million requests = $0.20 (for the AWS US East region)
Total requests cost = (Number of requests / 1 million) * Price per million requests = $0.10
Duration per request = 200 ms = 0.2 seconds
Price per 100 ms (based on memory allocation) = $0.000001667 (for 512MB of memory)
Total duration cost = (Duration per request/100 ms) * Price per 100 ms * Number of requests = $0.8335
Next, let's calculate the memory cost. AWS Lambda pricing includes a memory allocation cost per GB-second. This cost is based on the memory allocated to your function and the total execution time.
Memory allocation = 512MB
Execution time (in seconds) = (Duration per request / 1000) * Number of requests = 100 seconds
Memory cost = Memory allocation (in GB) * Execution time (in seconds) * Price per GB-second = $0.0834
Finally, let's calculate the total cost, considering the request cost, duration cost, and memory cost.
Total cost = Total requests cost + Total duration cost + Memory cost
= $0.10 + $0.8335 + $0.0834
Now that we have a solid understanding of Lambda's pricing structure, let us explore multiple ways to optimize Lambda which results in cost savings.
Save Big Bucks With Efficiency
Choose the right runtime:
Optimizing costs involves selecting the runtime that strikes a balance between resource efficiency and function execution speed. Compiled languages, such as Java and Go, are faster than interpreted languages, such as Python and Node.js. However, compiled languages also take longer to initialize. If your function is short and simple, an interpreted language may be a better choice. For example, interpreted languages could be used for scripting, automation, API backend etc.
Reduce the number of invocations:
The number of invocations is the biggest factor in determining your Lambda costs. Most common ways of invocating a Lambda function are:
- API Gateway
- Event Bridge
- Step Functions
- You can use CloudFront caching in front of API Gateway to cache static content.
- Use S3 event notifications only, when necessary, as they have a higher cost per invocation than other triggers.
- For SQS-triggered functions, adjust the Batch Size property to process more data per invocation and reduce the overall number of Lambda invocations.
Request Batching and Function Aggregation:
In scenarios where multiple requests can be processed together, leveraging request batching and function aggregation can be beneficial. By consolidating multiple requests into a single invocation, you reduce the number of function invocations and associated costs. This approach is particularly useful for scenarios involving data processing, where multiple records can be handled in a single function call. Consider a scenario where you have an application that receives user activity logs. Each log entry triggers a Lambda function to process and analyze the data. However, instead of invoking the Lambda function for each individual log entry, you can implement request batching and function aggregation. For example, you can accumulate 100 log entries or wait for a specific time interval, such as every 5 minutes, before triggering the Lambda function.
• Minimize data transfer:
Data transfer can also add up, so it is important to minimize it whenever possible. For instance, you can use a regional endpoint for your Lambda function. Regional endpoints are in the same region as your function, so they do not incur any data transfer charges.
Request Batching and Function Aggregation:
The size of your Lambda function package can have a significant impact on function performance and cost. A larger package size increases the cold start time and increases the amount of memory required. Several approaches to reduce Package Size:
- Minimize package size by removing any unnecessary libraries or dependencies.
- Use AWS Lambda Layers to reuse common code across Lambda functions.
In addition to these tips, you can also use Provisioned Concurrency Lambda compute costs and save money.
- Provisioned Concurrency allows you to pre-allocate a certain number of concurrent executions for your function. This can help to reduce latency and improve performance. For example, if you know that your function is going to be invoked 100 times per second, you can pre-allocate 100 concurrent executions. This will ensure that your function is always available to handle requests, even if there is a sudden spike in traffic.
When considering cost savings, it is crucial to evaluate the per millisecond execution time cost. With provisioned concurrency, this cost is approximately 70% cheaper compared to on-demand pricing. To break even against on-demand pricing, the provisioned concurrency must be utilized for a significant portion of the time, ideally around 60%.
Illustrating Cost Savings:
Lambda On-Demand Pricing
Lambda Pricing for Provisioned Concurrency
Note: It is crucial to bear in mind that this cost-saving technique is most suitable for high throughput functions. If a function has low or unpredictable invocation rates, it may lead to wastage of provisioned concurrency resources, ultimately increasing costs. Moreover, managing provisioned concurrency introduces operational overheads that should be factored into the decision-making process.
Therefore, it is advised to employ provisioned concurrency primarily to eliminate cold starts and enhance performance. Utilizing it as a cost-saving mechanism necessitates a deep understanding of function throughput patterns and accurate utilization analysis. Only when confident in meeting the required utilization levels should organizations leverage provisioned concurrency to achieve substantial cost savings.
Conclusion: Cost optimization techniques in AWS Lambda play a vital role in achieving efficient resource utilization and minimizing expenses. By carefully selecting runtimes, optimizing invocations, Batching and Function Aggregation, minimizing data transfers, and optimizing lambda package size, businesses can strike the right balance between cost efficiency and performance. However, it is crucial to consider the tradeoffs and challenges involved, and always prioritize the impact on business operations. By implementing these techniques, organizations can unlock the full potential of AWS Lambda while maximizing their return on investment.