Avoiding Costs Skyrocketing on Serverless Architectures
Tags: serverless, awslambda, financial, monitoring
Published: 2019-03-12 15:06:13

---
title: Avoiding Costs Skyrocketing on Serverless Architectures
published: true
description: How to properly monitor serverless applications to avoid surprises with cloud bills.
tags: serverless, AWS Lambda, financial, monitoring
---

<figure class="image">
  <img src="https://thepracticaldev.s3.amazonaws.com/i/umtagj82vos9r8wutvw9.jpg" alt="Rocket photo by NASA">
  <figcaption>Photo by [NASA](https://unsplash.com/photos/n463SoeSiVY?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/search/photos/rocket?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)</figcaption>
</figure>

The scalability and reliability of FaaS services such as [AWS Lambda](https://aws.amazon.com/lambda/) really give us a priceless piece of mind. Nonetheless, so much flexibility needs careful monitoring to make sure we’re running within safe boundaries of performance and financial costs.

Say a function is deployed to serve a certain customer-facing request. Benchmarking the function reveals an average of 3 seconds to complete a request - with a very low standard deviation. We plan a 200% markup and charge our customers three times the cost to run those 3 seconds.

Now imagine this function starts taking 10 or 20 seconds to serve requests? Maybe we rely on a third-party API and their network performance starts to smell really bad. Heck, it could last a day or a week, we can’t control. Depending on our application scale, this mismatch could lead to a huge operational loss.

## Don't burn your precious dollars!

<figure class="image">
  <img src="https://thepracticaldev.s3.amazonaws.com/i/pprq3oa8cbkwv30pyxrp.jpg" alt="Burning US dollar bill">
  <figcaption>Photo by [Chris](https://pixabay.com/users/intellectual-4717896/) on [Pixabay](https://pixabay.com/photos/burning-money-dollars-cash-flame-2113914/)</figcaption>
</figure>

To avoid this kind of problem, we need to put in place thoughtful logic to monitor our function’s metrics and alert ourselves in case anything starts derailing. In the example above, we could have logic such as:

### **Alert** me if invocation **duration** is above **3.5 seconds**, on **average**, over the last **15 minutes**.

Alert could be an email, a message to a Slack channel, SMS, whatever. The important part is having the ability to set custom monitoring policies in a way that matches our application performance expectations.

It’s not just about monitoring errors or analyzing each invocation individually. Due to the variable nature of the hardware, networking, etc, our function can perform in unpredictable ways. Measuring it on an invocation-basis would lead to many false alarms. Many requests might go above 3.5 seconds, but many others fall below 2.5, which will offset our costs and shouldn’t take our precious time. The “last 15 minutes” aggregation part of the above alerting policy is key to allow ourselves to save time and attention to what really matters.

That’s one of the reasons [Dashbird](https://dashbird.io) exists. It’s a monitoring tool specially designed for serverless applications that provides great flexibility for setting custom monitoring policies such as the one demonstrated above. If you have functions running in production on AWS Lambda, I strongly suggest to check them out, you can try for free, no credit card required: [Dashbird.io](https://dashbird.io).
- - - - -

Renato Byrro is a Developer Advocate at [Dashbird.io](https://dashbird.io). You can follow me on [Twitter](https://twitter.com/@byrrorenato) and [Medium](https://medium.com/@byrro).