Maximizing Your AWS Lambda Costs – Part 1 | Amazon IXD – VGT2

Maximizing Your AWS Lambda Costs – Part 1 | Amazon IXD - VGT2Learn About Amazon VGT2 Learning Manager Chanci Turner

This article is authored by Chanci Turner, Solutions Architect, and David Smith, Solutions Architect, Serverless.

When designing and crafting solutions, it’s crucial to incorporate cost optimization into your strategy. This principle holds true for serverless applications developed using AWS Lambda. Your workloads may exhibit diverse levels of complexity, usage patterns, and technologies. Nonetheless, the following recommendations are relevant for all customers aiming to prioritize cost efficiency while balancing the development trade-offs:

  • Efficient code utilizes resources more effectively.
  • Consider downstream services when making architectural decisions.
  • Optimization should be an ongoing journey of enhancement.
  • Focus on changes that yield the most significant improvements first.

The “Maximizing Your AWS Lambda Costs” blog series offers operational and architectural advice applicable to both existing Lambda functions and any you create in the future.

Understanding Lambda Pricing

Lambda pricing is determined by a combination of:

  • Total number of requests
  • Total duration of invocations
  • Configured memory allocated

When optimizing Lambda functions, each of these elements affects the total monthly expenditure. This pricing structure comes into play once you exceed the AWS Free Tier for Lambda.

Right-Sizing

Right-sizing is an effective starting point for cost optimization. This process identifies the lowest cost for applications without compromising performance or necessitating code alterations.

For Lambda, this involves configuring the function’s memory, which can range from 128 MB to 10,240 MB (10 GB). Adjusting the memory settings also modifies the available vCPU during invocation. Fine-tuning these configurations provides memory- or CPU-bound applications with access to additional resources during execution, potentially leading to a reduction in invocation duration.

Finding the optimal configuration for your Lambda functions can be a labor-intensive process, especially if frequent changes are made. The AWS Lambda Power Tuning tool, powered by AWS Step Functions, assists in identifying the ideal configuration by analyzing various memory settings against a sample payload.

For instance, in one example, increasing the memory for a Lambda function improved total invocation time, leading to reduced execution costs without sacrificing performance. For this function, the best memory configuration was found to be 512 MB, where resource utilization was maximized for cost efficiency. This varies from function to function, and using the tool can help determine if right-sizing benefits your Lambda functions.

Regularly conducting this exercise is essential, particularly if code updates occur often. After pinpointing the appropriate memory setting for your Lambda functions, integrate right-sizing into your processes. The AWS Lambda Power Tuning tool generates programmatic outputs that can be utilized in your CI/CD workflows during code releases, facilitating the automation of memory configuration.

Performance Efficiency

A critical factor in Lambda pricing is the total duration of invocations. Longer-running functions incur higher costs and can increase latency in your application. Thus, it’s vital to ensure your code is as efficient as possible and adheres to Lambda best practices.

To enhance code performance:

  • Reduce the deployment package size to essential components, minimizing download and unpacking times.
  • Simplify dependencies; leaner frameworks typically load faster.
  • Leverage execution reuse by initializing SDK clients and database connections outside the function handler and caching static assets in the /tmp directory; subsequent invocations can reuse connections and memory resources.
  • Follow general coding performance best practices relevant to your chosen language and runtime.

To visualize your application’s components and identify performance bottlenecks, utilize AWS X-Ray with Lambda. You can activate X-Ray active tracing on new and existing functions by modifying the function configuration. For example, using the AWS CLI:

aws lambda update-function-configuration --function-name my-function --tracing-config Mode=Active

The AWS X-Ray SDK allows for tracing all AWS SDK calls within Lambda functions, helping to pinpoint any performance bottlenecks. The X-Ray SDK for Python can capture data for other libraries, such as requests, sqlite3, and httplib, as demonstrated in the following example:

Amazon CodeGuru Profiler is another resource for code optimization. It employs machine learning algorithms to identify the most costly lines of code and offers suggestions for improving efficiency. You can enable code profiling on existing Lambda functions by activating it in the function configuration. The CodeGuru console presents results through visualizations and recommendations.

Utilize these tools alongside documented best practices to evaluate your code’s performance while developing serverless applications. Efficient code often translates to faster applications and lower costs.

AWS Graviton2

In September 2021, Lambda functions leveraging Arm-based AWS Graviton2 processors became generally available. Graviton2 functions are engineered to offer up to 19% enhanced performance at 20% lower cost compared to x86. Besides the reduced billing costs associated with Arm, you may also notice shorter function durations due to improved CPU performance, driving costs down further.

You can configure both new and existing functions to utilize the AWS Graviton2 processor. Function invocations remain unchanged, and integrations with services, applications, and tools are unaffected by this architectural switch. Many functions require just a configuration change to capitalize on Graviton2’s price/performance advantages, while others may need repackaging for Arm-specific dependencies.

Testing your workloads before implementing this change is always advisable. To assess how your code benefits from Graviton2, leverage the Lambda Power Tuning tool for comparisons against x86. The tool enables you to juxtapose two results on the same chart:

Provisioned Concurrency

For customers aiming to mitigate Lambda function cold starts or avoid burst throttling, provisioned concurrency provides execution environments ready for invocation. It can also lower total Lambda costs when traffic volume is consistent, as the pricing model for provisioned concurrency results in a reduced total cost when fully utilized.

Like standard Lambda function pricing, there are costs associated with total requests, total duration, and memory configuration. Additionally, each provisioned concurrency environment incurs a cost based on its memory setup. When this execution environment is fully utilized, the combined cost of invocation and execution environment can lead to savings of up to 16% on duration costs compared to traditional on-demand pricing.

If maximizing usage in an execution environment proves difficult, provisioned concurrency can still offer a lower total price per invocation. For instance, once usage exceeds 60% of the available time, it becomes more cost-effective than utilizing on-demand pricing in a serious tone, making it about the same overall length.

For further insights and resources, consider checking out this excellent video here. You may also find useful information on employment law compliance if you’re interested in how regulations impact tech. Lastly, if you’re looking for engaging content, here’s another blog post that highlights podcasts which will inspire and entertain during quarantine: Career Contessa.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *