DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Optimize Cost of AWS Lambda

How to Optimize Cost of AWS Lambda

AWS Lambda is an excellent cloud compute tool, but every second and request adds up.

Rohit Akiwatkar user avatar by
Rohit Akiwatkar
·
Jan. 04, 19 · Analysis
Like (2)
Save
Tweet
Share
10.36K Views

Join the DZone community and get the full member experience.

Join For Free

In the monolithic architecture, analyzing and forecasting the expenses of running an application is pretty straightforward. However, after moving to microservices and serverless architecture, the cost complexity has increased. 

With the increase in cost complexity, various factors need to be considered. Even though AWS Lambda pricing is pretty straightforward, the cost of running a serverless application is complex and involves many challenges. In this article, we'll explore those challenges along with how you can optimize bill of running a serverless application. 

Before you jump into the cost optimization strategies, here are the three questions that you need to analyze: 

#1. What is the contribution of AWS Lambda pricing to your total bill? Serverless applications consist of databases, storage, network costs, APIs, and data processing systems to name a few. Considering the percentage AWS Lambda cost to the overall application cost, you will have to see whether it is worth running cost optimization cycles or not.

#2. What is your app’s requirement in terms of the performance? Before iterating functions memory allocation for the sake of lower cost, consider this: memory allocation has a significant impact on the cold start and runtime, both of which are directly proportional to the cost. Hence, if your app requires low latency performance, then opting for degraded performance at lower cost won’t be a recommended solution.

#3. Which and how many of the functions of your app are frequently used? Not every one of your functions will be invoked at the same frequency. Hence, focusing on cost optimization for functions with high frequency is recommended, for example, functions having hundreds of thousands or millions number of invocations.

Strategies for AWS Lambda Cost Optimization

There are multiple combinations of usage and configuration that affects the overall pricing of Lambda functions. This can be problematic sometimes!

For example, if you’re working for a small startup, you definitely don’t want to spend $500 over a single Lambda function. Let alone having hundreds of them. Here are few strategies to help you keep your Lambda cost in control:

1. Optimizing Function Frequency

There are various factors that affect the invocation frequency of Lambda function. This is based on the triggers. Closely monitor your triggers and see if you can do to reduce the number of invocations over the time.

For example, suppose your Lambda function is being triggered by the Kinesis Stream. Since the batch size is quite small, the invocation frequency is pretty high. In such cases, what you can do is opt for higher batch size so that your Lambda function is invoked less frequently.

2. Writing Efficient Code

The function that executes in half the time is a function that will cost you half the money. One must note that the execution duration is directly proportional to the amount of money you’ll be charged. It is critical to keep an eye on the Duration metric inside CloudWatch. And it’d be common sense to modify and iterate your function if it is taking a suspiciously long time to execute.

For this purpose, AWS X-Ray can help you monitor function from end to end.

3. Monitoring Data Transfer

While talking about Lambda charges, it often gets out of our sight that you’ll also be charged for the data transfer at standard EC2 data transfer rates. Hence, it is the default to keep an eye on the amount of data you’re transferring out to the internet and other regions of AWS.

While talking about internal data transfer, there isn’t much you can do about it as you can’t control the amount of data your Lambda will transfer. Likewise, there is no metric inside CloudWatch which can help you in monitoring the data transfer rate.

In such cases, here’s what you can do:

  • Keenly monitor your AWS Cost & Usage Report. Filter it by Resource (your Lambda function) and find different values in the Transfer Type column. Further, get the usage amount. This might come off as slightly time-consuming.
  • Log the size of data transfer operations in your Lambda code and then configure a CloudWatch Metric Filter as a reference that becomes a CloudWatch Metric.

4. Developing In-House Tools

If all of the above options aren’t suitable for you and you need a concrete solution that actually works, you can opt for developing in-house tools. Here are some of the examples:

  • Real-time Lambda Price Calculator: This is a quick and automated way to monitor estimated monthly cost in real-time of AWS Lambda based on your current usage. For more information, you can look here.
  • Memory Metric Filter: This is a simple CloudWatch Logs Metric Filter that can help you in monitoring by extracting memory size and allocated memory from the execution records. Launch Stack here and get started.

Takeaway!

That being said, no one size fits all! However, for some organizations, serverless is helping them in cutting down the cost with a huge margin while others are probably looking for more comprehensive options considering the hidden costs of serverless. To avoid surprises, it is better to keep the eye on the bill and optimize on the go. If you have incorporated a strategy not mentioned here, I'd love to hear from you. Hit me up on Twitter @RohitAkiwatkar.

AWS AWS Lambda

Published at DZone with permission of Rohit Akiwatkar. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Insight Into Developing Quarkus-Based Microservices
  • Essential Mobile App Security Tips for Coders in 2023: Make Your App Unhackable
  • Required Knowledge To Pass AWS Certified Data Analytics Specialty Exam
  • 5 Tips for Optimizing Your React App’s Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: