Cost Reduction in AWS Serverless: Quick Wins for API Gateway & Lambda

Serverless computing has gained immense popularity in recent years, thanks to its scalability, reduced operational overhead, and pay-as-you-go pricing model. Serverless is a development model that allows developers to build and run applications without the need to manage servers. Serverless enables developers to focus on the application logic rather than the server infrastructure. AWS offers a range of serverless services, such as AWS Lambda, Amazon API Gateway, Amazon DynamoDB, and more. These services enable you to focus on your business logic and pay only for the resources you consume.

However, serverless does not mean costless. You still need to monitor and optimize your serverless costs, especially as your application scales and grows. In this blog post, we will share some tips and best practices on how to control your serverless costs on AWS. We’ll delve into strategies to control and optimize AWS Serverless costs, focusing specifically on API Gateway and Lambda services.

Cost Management and Optimizing Strategy

AWS Lambda: Lambda pricing is directly determined by these factors: memory usage, function duration (function execution time, measures in milliseconds), number of invocations (the number of times your function runs) and data transfer. When you optimize Lambda functions, each of these factors impacts the total monthly cost.

Here are some strategies to help you manage and optimize the costs of your AWS Lambda.

AWS Lambda Power Tuning Tool
AWS Lambda Power Tuning utilizes AWS Step Functions to operate as a state machine, enabling the optimization of your Lambda functions based on cost and/or performance through a data-driven approach.

We can provide a Lambda function ARN as input and the state machine will invoke that function with multiple power configurations (from 128MB to 3GB). It will then analyze all execution logs and provide suggestions for the best power configuration to minimize cost and/or maximize performance.

Here is an example:
Deploy the Lambda Power Tuning Tool hosted in the Serverless Application Repository (SAR)

Next, let’s check the best memory size for the function.

Next, we can provide a Lambda function ARN as input to the state machine.

At strategy parameter: it can be “cost” or “speed” or “balanced”.
Note: (the default value is “cost”); if you use “cost” the state machine will suggest the cheapest option (disregarding its performance), while if you use “speed” the state machine will suggest the fastest option (disregarding its cost). When using “balanced”, the state machine chooses a compromise between “cost” and “speed” according to the parameter “balancedWeight”.

The result of the execution with strategy cost shows, the optimal memory/RAM should be 512. You can also see the visualization of this.

Here you can see the execution time execution cost for each MB passed. You can see there the lessor memory allocated will take higher response time, and if you allocate more memory the response time is very less but if you see the cost, it’s high.
If we change the strategy parameter to speed, the execution result shows that the optimal memory/RAM should be 1024.

If we change the strategy parameter to balanced, then the execution result shows that the optimal memory/RAM should be 512.

Alternatively, you can consider the intersection point the optimal memory for selection, which in this case should be 512.

Graviton2
Leveraging an Arm-based processor architecture, Graviton2 functions are engineered to provide a 19% boost in performance while offering a 20% reduction in costs across diverse serverless workloads, including web and mobile backends, data processing, and media tasks.

Next let’s check the performance of both x86_64 and arm64 architecture. We have created 2 Lambda with all configuration and code same, only the architecture is different.

Using AWS Lambda Power Tuning tool, we compare their performance and cost.

You can now visually see both the invocation time and cost comparisons between the x86 and arm64 architecture functions in the above image. Observe that the arm64 function performs better in both cost and invocation time in this case.

Event Filtering
Employing event filtering allows you to manage the records sent from a stream or queue to your Lambda function. For example, event filtering helps reduce requests made to your Lambda functions, may simplify code, and can reduce overall cost (Lambda will automatically discard non-matching events).

For Lambda Event Filtering it supports SQS, DynamoDB and Kinesis event sources, MQ and MSK.

Imagine a logistics company with a fleet of vehicles in the field. Each vehicle is equipped with sensors and 4G/5G connectivity to transmit telemetry data to Kinesis Data Streams. The following shows an example record received.

{
"time": "2021-11-09 13:32:04",
"fleet_id": "fleet-452",
"vehicle_id": "a42bb15c-43eb-11ec-81d3-0242ac130003",
"lat": 47.616226213162406,
"lon": -122.33989110734133,
"speed": 43,
"odometer": 43519,
"tire_pressure": [41, 40, 31, 41],
"weather_temp": 76,
"weather_pressure": 1013,
"weather_humidity": 66,
"weather_wind_speed": 8,
"weather_wind_dir": "ne"
}

To process all fleet messages, configure the appropriate filter. The Lambda service applies the filter pattern to the entire payload it receives. Use the filter criteria below to filter out messages where tyre_pressure is 32 or higher.

{
"body": {
"tire_pressure": [{"numeric": ["<", 32]}] } }

Depending upon which events are invoking Lambda functions, there are various controls you can use to reduce the total number of invocations. For example, Lambda functions triggered by:

Invocation Frequency
Depending upon which events are invoking Lambda functions, there are various controls you can use to reduce the total number of invocations. For example, Lambda functions triggered by:

API Gateway:
Scenario: For read-heavy API requests where data doesn't change frequently.
Control: Implement CloudFront caching in front of API Gateway.
Benefits: CloudFront caches responses, reducing API Gateway and Lambda invocations for repeated requests, optimizing costs.
Cons: Increased CloudFront costs due to data transfer and cache operations. Over-caching might lead to serving stale data if not managed effectively.

Rule Pattern Matching:
Scenario: Triggering Lambda based on specific event patterns such as specific service names and error statuses.
Control: Use EventBridge rules to filter events based on patterns.
Benefits: Lambda only executes events matching defined patterns, minimizing unnecessary invocations.

API Gateway Cost optimization

  • Choosing the right type of API Gateway: One of the best ways to optimize API gateway costs is to choose the most cost-effective type of gateway.
  • API Gateway integration feature: AWS API Gateway provides integration capabilities with other AWS services via service proxies, allowing direct integration without passing data through Lambda functions. This reduces the associated Lambda invocation costs and streamlines data flow between services like Kinesis, SQS, SNS, and DynamoDB.
  • Consideration of ALB (Application Load Balancer): Scaling applications might necessitate replacing API Gateway with an ALB. If your use case doesn’t heavily rely on API Gateway-specific features like caching, authentication, or transformations, ALB can be a cost-effective alternative for triggering Lambda functions.
  • Managing Data Transfer Costs: API Gateway incurs data transfer costs when transferring data to the public internet. Utilizing CloudFront, a CDN service, helps reduce data delivery costs by caching frequently accessed content closer to end users, minimizing data transfer from AWS resources like S3, EC2, and API Gateway to CloudFront (which is free).
  • Authentication with Cognito: If your front-end is the only application using the backend APIs, you can use Cognito to get your users' IAM credentials. After that, they can securely use the AWS JavaScript SDK and the temporary credentials to call your Lambda services. With this method, you can accomplish authorization and authentication using IAM without the requirement for an API Gateway, albeit at the expense of fewer features (you will still need to manage failures and responses in your Lambda code in addition to preparing your request on the frontend).

By considering these strategies and selecting the most appropriate API Gateway type, leveraging integration features, optimizing data transfer, and utilizing CDN services like CloudFront, you can effectively reduce API Gateway costs while ensuring efficient data transfer and delivery.

Cost Monitoring and Budgeting

  • AWS Budgets: An AWS cost management tool that helps customers define and track budgets for AWS costs and forecast up to three months ahead. Utilizing AWS Budgets does not incur any additional charges. You only pay for configured actions that exceed the free tier offer of 62 days of budget with actions enabled.
  • AWS Trusted Advisor: AWS Trusted Advisor is a service that helps you optimize your AWS infrastructure by providing recommendations across four key areas: cost optimization, performance, security, and fault tolerance.
  • Amazon CloudWatch: Amazon CloudWatch is a service that helps you monitor and optimize the performance and cost of your AWS resources, including serverless applications.
  • AWS Cost Anomaly detection: Cost Anomaly Detection is an AWS cost management service that uses advanced machine learning to detect anomalous spend and provide contextualized alert notifications through email and Amazon SNS. Every anomaly comes with a root cause analysis and direct links for further investigation in Cost Explorer, aiding in comprehending the unexpected usage and its underlying factors.

Summary

The blog post dives into cost control strategies within AWS serverless setups, focusing notably on API Gateway and Lambda services. It outlines approaches to optimize Lambda functions using tools like the Power Tuning Tool and explores the cost-effectiveness of Graviton2 architecture. The post also delves into event filtering and invocation frequency management for cost reduction. It offers insights into API Gateway optimization, suggests leveraging Amazon Cognito for front-end authentication without API Gateway, and introduces AWS tools like AWS Budgets, Trusted Advisor, CloudWatch, and Cost Anomaly Detection for effective cost monitoring and budgeting, emphasizing the importance of cost-efficient operations in serverless architectures.

Deepa Chand

Cloud and DevOps software engineer, enthusiastic about technology, currently immersed in AWS Cloud, automation, and HIPAA-compliant network solutions.

Leave a comment

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

Share Post