The serverless computing market is growing fast and expected to reach $36.84 billion by 2028. AWS Lambda is a popular choice for businesses because of its streamlined code maintenance, cost-effectiveness, and seamless scalability. As more companies switch to serverless functions, it is crucial to keep Lambda costs down for long-term success. This exponential growth underscores the importance of cost optimization for sustainable business growth. As businesses increasingly rely on AWS Lambda for streamlined operations, reducing Lambda costs becomes important.[1]
This blog post will cover Lambda pricing, factors influencing Lambda costs, and strategies to reduce Lambda expenses.
Several factors influence AWS Lambda pricing. Function execution time, which is the time taken for functions to execute, impacts costs, calculated in milliseconds. Usage Frequency, measured in requests, determines charges based on how often functions are invoked. Memory Allocation plays a role, with higher memory configurations incurring higher costs. Provisioned Concurrency ensures quick execution but adds an extra cost. Ephemeral Storage usage contributes to overall pricing. HTTP Response Streams and Lambda@Edge usage affect costs due to increased requests and data transfer. Data Transfer to other AWS services and interactions with additional AWS Services may incur extra charges. These factors collectively determine the pricing structure of AWS Lambda.
Refer to AWS Lambda Pricing for detailed Pricing on Lambda.
Scenario: Let say you are running a chat bot like BERT or GPT, on AWS Lambda to improve customer service. The model processes six million requests monthly, with an average duration of 280 ms. It is configured with 4096 MB of memory on an x86 processor and allocated 2048 MB of ephemeral storage
Monthly compute charges:
The monthly compute price is $0.0000166667 per GB-s and the free tier provides 400,000 GB-s.
Total compute (seconds) = 6M * 280ms = 1,680,000 seconds
Total compute (GB-s) = 1,680,000 * 4096 MB/1024 MB = 6,720,000 GB-s
Total compute – AWS Free Tier compute = Monthly billable compute GB- s
6,720,000 GB-s – 400,000 free tier GB-s = 6,320,000 GB-s
Monthly compute charges = 6,320,000 * $0.0000166667 = $105.33
Monthly request charges:
The monthly request price is $0.20 per one million requests and the free tier provides one million requests per month.
Total requests – Free tier requests = monthly billable requests
6 million requests – 1 million free tier requests = 5 million monthly billable requests
Monthly request charges = 5 million * $0.2/million = $1
Monthly ephemeral storage charges:
The monthly ephemeral storage price is $0.0000000309 for every GB-second and Lambda provides 512 MB of storage at no additional cost.
Total compute (seconds) = 6M * 280ms = 1,680,000 seconds
Total billable ephemeral storage = 2048 MB – 512 MB = 1536 MB
Total ephemeral storage (GB-s) = 1,680,000 * 1536 MB/1024 MB = 2,520,000 GB-s
Monthly ephemeral storage charges = 2,520,000 * $0.0000000309 = $0.08
Total monthly charges
Total charges = Compute charges + Request charges + ephemeral charges = $105.33 + $1 + $0.08 = $106.41 per month
Also refer to AWS lambda Pricing for more pricing examples.
Right-sizing Lambda resources involves optimizing the codebase of Lambda functions to improve execution time. This includes identifying and addressing inefficiencies in the code that lead to longer execution times, ultimately enhancing performance and reducing costs. Additionally, aligning memory allocation with the specific workload requirements helps prevent over-provisioning, ensuring efficient resource utilization. Regular monitoring and refinement of the codebase, in response to changing workload demands, are essential practices for maximizing cost efficiency through right-sizing.
To reduce Lambda costs effectively, employ Amazon CodeGuru Profiler, a tool that utilizes machine learning to identify costly lines of code. Enable code profiling in function configurations to gather performance data and analyze it to pinpoint resource-intensive areas and bottlenecks. Optimize these sections using best practices and efficient coding techniques. Validate changes to ensure functionality remains intact. Regularly iterate this process for continuous improvement.
The AWS X-Ray can be used to trace all AWS SDK calls inside Lambda functions. This helps to identify any bottlenecks in the application performance. To optimize Lambda costs using AWS X-Ray, enable tracing for your functions and analyze trace data to identify performance bottlenecks. Optimize code and resources to reduce duration, focusing on minimizing dependencies and package size. Validate changes and iterate regularly for incremental improvements. For Bash functions, ensure efficient code execution and minimize resource usage.
For example, using the AWS CLI:
aws lambda update-function-configuration --function-name my-function --tracing-config Mode=Active
Graviton2 is an AWS designed ARM-based processor for high-performance, cost-effective computing. Graviton2 functions are designed to deliver up to 19% better performance at 20% lower cost than x86. To use AWS Graviton2 for reducing Lambda costs, consider migrating functions to Graviton2-based instances. Graviton2 processors offer better price-performance ratios compared to traditional x86 processors, potentially reducing overall execution costs for Lambda functions.
To assess the impact of Graviton2 on your code, utilize the Lambda Power Tuning tool to compare it against x86. This tool enables you to visualize and compare both results simultaneously on a single chart.
Explore more on choosing Graviton instances for optimizing Lambda costs and performance with these recommended blogs:
These resources will help you make informed decisions when selecting Graviton instances for your Amazon Lambda deployments.
To reduce Lambda costs, utilize Compute Savings Plans. AWS Savings Plans is a flexible pricing model offering lower prices compared to on-demand pricing, in exchange for a specific usage commitment (measured in $/hour) for a one- or three-year period. Lambda usage for duration and provisioned concurrency are charged at a discounted Savings Plans rate of up to 17% for a 1- or 3-year term. Analyze usage patterns, purchase aligned plans, and apply them to functions for discounted rates. Monitor usage and adjust plans to optimize savings over time.
To reduce Lambda costs using event filtering, you can implement mechanisms to filter out unnecessary events before they trigger Lambda functions. This can be achieved by configuring event sources such as Amazon S3 buckets, Amazon SQS queues, or Amazon Kinesis streams to only send relevant events to Lambda. By filtering out irrelevant events, you can minimize the number of function invocations, thus reducing overall Lambda costs.
Example code snippet
Using S3 Event Filtering:
{
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "example-bucket",
"NotificationConfiguration": {
"LambdaConfigurations": [
{
"Event": "s3:ObjectCreated:*",
"Filter": {
"S3Key": {
"Rules": [
{
"Name": "suffix",
"Value": ".log"
}
]
}
},
"Function": {
"Fn::GetAtt": ["LambdaFunction", "Arn"]
}
}
]
}
}
}
Autodesk implemented event filtering in their serverless solution, Tailor, to automate the AWS account creation process efficiently, reducing account-creation time by 99%.-Alan Williams ,Enterprise Architect [6]
To reduce Lambda costs, implement batch processing by aggregating similar tasks, triggering batch jobs efficiently, and optimizing processing logic. Implement robust error handling and monitoring mechanisms to ensure reliable execution while continuously seeking optimization opportunities such as fine-tuning batch sizes and resource allocation.
Example code snippet
Create an SQS Queue with Batch Size:
{
"Type": "AWS::SQS::Queue",
"Properties": {
"QueueName": "batch-processing-queue"
}
}
Lambda Function Configuration:
{
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "BatchProcessingFunction",
"Handler": "index.handler",
"Runtime": "python3.9",
"Role": {
"Fn::GetAtt": ["LambdaExecutionRole", "Arn"]
},
"Environment": {
"Variables": {
"QUEUE_URL": {
"Ref": "BatchProcessingQueue"
}
}
},
"Events": {
"SQSTrigger": {
"Type": "SQS",
"Properties": {
"Queue": {
"Ref": "BatchProcessingQueue"
},
"BatchSize": 10
}
}
}
}
}
By using batch processing, Netflix can handle multiple events together, optimizing processing logic and resource allocation, thus improving efficiency and reducing operational costs. [7]
In conclusion, by implementing cost optimization strategies such as right-sizing resources, leveraging AWS Graviton2 for cost-effective computing, utilizing Compute Savings Plans, and employing event filtering and batch processing techniques, organizations can effectively reduce Lambda expenses. These optimization strategies not only help in minimizing costs but also enhance operational efficiency and scalability. With a proactive approach to cost optimization, businesses can maximize the value of AWS Lambda while ensuring long-term success in the serverless computing landscape.
3. Best practices for working with AWS Lambda functions
4. Architecture and Best Practices - AWS Lambda
5. Cost optimization - AWS Lambda
6. Autodesk Goes Serverless in the AWS Cloud, Reduces Account-Creation Time by 99%
7. Netflix & AWS Lambda Case Study
1. What is AWS Lambda pricing based on?
AWS Lambda pricing is based on the number of requests and the duration it takes for your code to execute. Pricing factors include Number of requests ,Duration (measured in milliseconds),Memory allocation, Provisioned concurrency, Data transfer costs.
2. How does AWS Lambda's free tier work?
AWS Lambda offers a free tier that includes 1 million free requests per month and 400,000 GB-seconds of compute time per month. This is automatically applied to your account.
3. What is the cost of an AWS Lambda function invocation?
The cost of an AWS Lambda function invocation is based on the number of requests and the compute time consumed, billed in milliseconds.
4. How do memory and storage affect AWS Lambda costs?
Memory allocated to your Lambda function and the amount of ephemeral storage used directly impact the cost. More memory and storage lead to higher costs.
5. What is Lambda@Edge pricing?
Lambda@Edge pricing is based on the number of requests and the duration of execution for functions deployed at AWS edge locations. This service is tailored for low-latency web content delivery.
Strategical use of SCPs saves more cloud cost than one can imagine. Astuto does that for you!