10 min read
Do This to Reduce your AWS Serverless Cost
In the modern cloud application world, Serverless is the term that trending and becoming more important to every organization that runs applications in the public cloud.
Serverless enables to build of applications without thinking about servers, server scaling, and server management. Instead, we only need to think about how best to serve our customers, how to write the code that allows us to do so, and How to build the cost-effectiveness of different designs and patterns.
serverless are event-driven cloud-based system that provide scalability, low maintenance cost, fault-tolerance and flexibility for a cloud based application.
image from creately.com
Cost Optimization Strategies
AWS Serverless Well-Architected Framework
While serverless cost is correlated with the level of usage, The architectural decisions and build impact the cost efficiency. The impact of these choices is more significant as your traffic grows, so it’s important to consider the cost-effectiveness of different designs and patterns of serverless application workloads.
The general design principles for the Well-Architected Frameworks are:
Do not guess your capacity needs: To avoid guessing your capacity needs is to use services that automatically scale based on demands. If you make a poor capacity decision, you might end up on expensive idle resources or deal with the performance implications of limited capacity. Instead, use monitoring tools like CloudWatch, Grafana, etc to monitor your resource usage and identify your resource's normal operating condition (Baseline). Later, define your capacity as you need, and scale up and down automatically.
Example: When you choosing DynamoDB service, consider selecting OnDemand DB. It simplifies your billing based on per-request pricing for read and write requests so that you only pay for what you use, making it easy to balance costs and performance
Use staging environment at production scale with decommissioning: In the cloud, you can create a production-scale test environment on demand, complete your testing, and then decommission the resources. Because you only pay for the test environment when it’s running, you can simulate your live environment for a fraction of the cost of testing on premises.
Example: Use IaaC like Terraform, Ansible, Cloudformation,etc to deploy and destroy your testing environment.
Baseline architectures using data: In the cloud, you can collect data on how your architectural choices affect the behavior of your workload. This lets you make fact-based decisions on how to improve your workload.
Review your workloads with tools: AWS provides a service for reviewing your workloads at no charge. AWS Well-Architected Tool , Power Tuning Tool These tools let you make fact-based decisions on how to improve your workload.
Cost Optimization in Function-level
At the function level, the Serverless function's complexity, usage patterns, and technology of the workload may vary. All customers should consider the following advice when deciding how to prioritize cost optimization with tradeoffs associated with the application:
Optimize functions memory allocation: Lambda function is the on-demand serverless application that is billed by requests and GB per second. GB per second is calculated by multiplying duration in seconds by the memory allocated to the function.
Due to the fact that total cost is a combination of memory and duration, choosing more memory can often reduce the duration and lower overall cost.
Consisder using Lambda Power Tunning Tool by Alex Casalboni. This tool run your function against different memory configurations. Then it will analyze all the execution logs and provide suggestions on the best power configuration to minimize cost and/or maximize performance.
Consider sing AWS Gravition2 Processor , an Arm-based architecture designed and built by AWS. Which is 34% better price/performance improvement over x86.
Minimize external calls and Code optimization: In your code, an application may call other internally managed services using Public URLs. Due to that data transfer charges, you may encounter while operating your workload on AWS. Also, the Application may call other external APIs and functions may use application dependencies that may not be suitable for ephemeral environments. Reviewing and replacing can directly reduce your AWS cost.
Data transfer charges must be overlooked while making architectural decisions. Otherwise, potential data transfer charges you may encounter while operating your workload on AWS.
Intergrade AWS X-Ray service with your Lambda function and it generate a detailed service map that shows the latency histograms (Latency is the amount of time between when a request starts and when it completes), services that your front-end service calls to process requests and persist data, memory used by each function , etc. With this insights, you can identify issues and opportunities for optimization.
Log tunning and its retention: AWS Clodwatch charges for the logs ingested by the system (measured in GB), the log storage based on GB, and logs analyzed measured in GBs of data scanned. Therefore, understand your operational needs and manipulate the log level, log output, and log retention period. Avoid unnecessary logging and data retention, while ensuring that you have the minimum levels of logging and data retention necessary for the operation of the workload.
To understand lambda log levels, Use log tuning tool by aws-samples/aws-lambda-extensions . Deploy AWS Lambda extensions with AWS AppConfig. Define different log level & log output to your lambda function. Then decide what level need to be used based on your needs and Avoid unneccessary loggings.
Asynch workflow and Performance optimization techniques in code: Execute independently of the main application flow without blocking the execution of the application while there are running. This workflow can be used to improve the performance and responsiveness of an application by allowing it to perform tasks in the background while continuing to interact with the user or perform other tasks.
Use Task queues or Asynchornous Programing , Task queues means that Tasks are added to a queue and then processed by workers as they become available. Asynchornous Programing means that process can initiate a task and then continue to execute other code while the task is being completed in the background.
Cost Optimization at the Integration Level
SQS, SNS, and Amazon Event Bridge are serverless integration layer that is able to coordinate the execution of functions and tasks to handle the communication and data exchange between different AWS services and components.
Use the right service for your needs: Each type of service has different features and capabilities, and choosing the right one for your specific needs can help reduce costs.
Use Standard Queues or Standard Topics: These options are generally less expensive than their counterparts (FIFO Queues or FIFO Topics).
Use event combining: SQS, SNS is a request-based pricing component, where 64 KB of a payload is billed as one request. For example, a single SQS message with a 256 KB payload is billed as four requests.
Sometime messages are smaller then 64KB. So, combine multiple messages into a single publish action, which can help reduce the total number of publish actions. On the other hand, eliminate unnecessary attributes in the message or compressing the message, you can increase the amount of data that can be stored in a single request.
Filter events: Many events may not have messages to consume by services. For example, an SQS may publish to a Lambda function, which checks the content and discards the message based on some criteria. In such cases, it’s more cost-effective to use the event filtering capabilities in the lambda when you add SQS as triggers.
Before optimization, the SQS sends all messages to a Lambda function. While SQS does not charge for delivery to Lambda functions, you are charged each time the Lambda service is invoked.
Cost Optimization in Storage Level
AWS offers a wide range of data storage and management services that can be used to build and deploy applications in the cloud. This includes Amazon S3, RDS, DynamoDB, Redshift, Elasticache, and DocumentDB. Also, It provides a wide range of tools and services to manage, and scale your data at the storage level. This includes automated backups, monitoring, security, and compliance features, as well as integration with other AWS services such as AWS Lambda, Amazon Kinesis, and Amazon EMR to enable real-time processing and analytics.
Identify the Data Storage Solutions for Your Application: Deciding on storage involves identifying the types of data your organization generates, including their size and frequency of access. This will help you choose the most appropriate storage option.
If you are new to the serverless framework and you want to choose a database for your application. if applications do not require fetching data in real-time and are used in a relatively large scale. Consider choosing AWS RDS DB. Because, AWS RDS is not serverless service so there are monthly charges for the underlying compute resources.
If your use cases are real-time bidding, shopping carts, customer Preferences, content management, Personalization, and mobile applications. Choose DynamoDB instead, it may be more cost-effective.
Read capacity units (RCUs) is part of DynamoDB pricing. Therefore, It’s best a practice to store larger items in S3 for binary items such as images and include a reference token (S3 object identifier) in a table item. It is usually more cost-efficient to use S3 for storage.
Retrieval Demands of Your Application: To optimize costs, it’s important to understand your application’s data access. This involves analyzing how your application stores, retrieves, and processes data, and identifying opportunities without compromising performance or functionality.
AWS offers several storage classes, including Amazon S3 Standard, S3 Standard-Infrequent Access (S3 Standard-IA), S3 One Zone-Infrequent Access (S3 One Zone-IA), S3 Intelligent-Tiering, S3 Glacier, and S3 Glacier Deep Archive. Choose the approoriate storage class that meets your data access and retention requirements while also minimizing costs.
AWS lifecycle policies can help you automate the process of moving objects to lower-cost storage classes as they age, based on rules that you define. This can help optimize costs over time.
S3 inventory reports allow you to identify objects that haven’t been accessed for a specific period.
Compress, Archiving and Delete unnecessary data: By regularly archiving or deleting data that is no longer needed, you can reduce storage costs and improve application performance. However, it’s important to be careful not to delete data that is still needed for compliance or other purposes.
Infrequently accessed data can be moved to lower-cost storage classes such as Amazon S3 Glacier or Amazon S3 Glacier Deep Archive from your application storages
Regularly review your storage usage and delete any unnecessary data. This could include data and logs that’s no longer needed, duplicates, or data that’s been superseded by newer versions.
When using Redshift, consider compressing your data before loading it into the Redshift, you can save money on storage costs. However, it’s important to be careful not to over-compress your data, as this can negatively impact query performance.
Choose the right backup type and Schedule backups strategically: AWS offers different backup types, each with different costs and performance characteristics. Also, By scheduling backups during off-peak hours, you can avoid potential performance impacts and reduce costs.
EBS snapshots are cheaper than Amazon RDS automated backups, but they may take longer to restore. By choosing the right backup type for your workload, you can optimize costs without sacrificing performance or functionality.
Content Delivery and Networking Level
A content Delivery Network is a distributed network of servers span across multiple geographic locations around the world that work together to deliver web static assets to end-users in the fastest and most efficient way possible.
Use regional edge caches: CDNs operate by caching content on their servers and then delivering that content to end-users from the server that is geographically closest to them. This helps to reduce latency and fewer Origin requests. Configuring cache policies have an impact on your overall AWS Data transfer costs.
AWS CloudFront global and regional edge caches. Regional edge caches are more cost-effective for delivering content to end-users located in a specific region. By using regional edge caches, you can reduce the amount of data transferred between regions and thus reduce your data transfer costs.
Optimize the Static Assets Size: Web applications use CDN to serve static assets to end-user. The Assets may not be optimized and can be larger than necessary. Optimize the size of your static assets by resizing or compressing.
You can use tools like Lighthouse to analyze your web apps to find images that can be resized or compressed.
Optimize your Origin Requests by Transforming Content on-the-fly: You can minimize the amount of content that needs to be fetched from the origin server by using Lambda@Edge or CloudFront function. This will potentially reduce data transfer costs.
AWS offers Lambda@Edge and CloudFront Function services that allow you to customize the content delivered through CloudFront. Cloudfront function is cost effective than Lambda@Edge. It provide 200+ Edge Caches, less in execution time (1 millisecond) and memory (2MB). Find my full article about Lambda@Edge & CloudFront Function use-case.
API Gateway Level
API Gateway acts as a front door for backend services and provides a secure & scalable interface for clients to access backend Applications. Amazon API Gateway is a fully managed, highly available & scalable serverless service. It is a great choice for building and managing APIs.
Here are some cost optimization strategies for API Gateway.
Use HTTP APIs: AWS API Gateway provide REST APIs, WebSocket, and HTTP APIs. HTTP APIs are a more cost-effective option than REST APIs as they offer faster performance and lower costs. HTTP APIs have a lower per-request cost than REST APIs and do not charge for data transfer out. The cost is 70% lower than REST APIs.
Use API Gateway VPC Links: Consider using VPC links to privately integrate API Gateway with other AWS services within your VPC. This can reduce data transfer costs and increase security.
Enable Request Validation: This can reduce the number of requests that hit your backend and reduce the data transfer costs.
Monitor and Analyze Your API Gateway Usage: By monitoring and analyzing your API Gateway usage, you can identify areas where you can optimize your API usage and reduce costs. Use CloudWatch metrics and logs to monitor usage and identify potential cost optimization opportunities.
AWS Serverless can provide several benefits, including reduced infrastructure costs, auto-scaling, pay-per-use pricing, and increased development agility without any upfront costs or minimum fees.
But implementing cost optimization strategies is essential for organizations that want to manage their cloud infrastructure costs effectively and achieve better business outcomes as the business grows.
Cost optimization is critical for organizations that can help align their cloud spending with their business goals, reduce waste and inefficiencies, and make informed decisions about cloud resource allocation.