Serverless Functions have three important properties, each of them having a significant impact on the way we deal with the cost of an application:
- Low management overhead The cloud provider manages the service. The total cost of ownership is minimal: developers create code to solve business problems—the rest is taken care of.
- Pay-per-use Functions are charged per actual executions. Nothing is reserved in advance, so the cost of running a Function App grows linearly with the application demand.
- Elastically scalable When a Function is idle, Azure scales the infrastructure down to zero with no associated cost. Whenever the workload grows, Azure brings enough capacity to serve all the demand.
Why does this matter?
In the past, the cost of infrastructure running an application and the value that the application provides were separated. A company would run multiple applications, each having numerous components and services, on a shared infrastructure of dedicated hardware, a pool of VMs, or IaaS services in the cloud. It’s quite complicated to tear the expenses apart and determine the exact cost of each application, let alone a particular component. Moreover, the investments are planned and executed in advance, so the infrastructure can’t follow the elasticity of workload and ends up overprovisioned and underutilized.
Today, the application portfolio can run on serverless functions, each component becoming a separate Function App. In turn, each App may contain multiple functions. Since the company pays per actual use, they can understand and manage the exact cost of each component.
This ability enables the business to:
- See which features are most profitable, and which ones are too expensive
- Optimize the Functions that have a high-impact on the invoice and ignore the rest
- Make informed decisions of whether to spend engineering time on such optimizations or create new business value while paying a premium to the cloud provider
Note that the cost information is retrospective: the actual numbers come after the fact of spending. The lack of budgeting makes decision-makers nervous: they’re used to planning the cost of infrastructure long in advance. Therefore, our goal is to understand the cost structure and be able to predict the changes in invoices as applications and business evolve.
Billing model of the consumption plan
Let’s dissect the structure of the Consumption plan. There are two core components of the cost of serverless Functions in Azure: Execution Count and Execution Time.
Execution Count is straightforward. Each Function defines a trigger—an event which causes the code to execute. It can be an incoming HTTP request or a message in a given queue. Every call counts: you get charged $0.20 per million executions. This component of the cost can be substantially reduced if you batch events: process several events in a single execution.
The second cost component is called Execution Time on the pricing page, which isn’t exactly correct: it depends on both completion time and memory consumption and is metered in GB-seconds. You pay $16 per million GB-seconds. That is, if a Function runs 1 million times, it always consumes 1 GB of memory and completes in 1 second, you pay $16.
The memory consumption is always rounded up to the next 128 MB, and the minimum time charge is 100 milliseconds. Therefore, the minimal time charge is again $0.20 per million executions.
Holistically, there are other cost components of running serverless applications not tied directly to the Azure Functions service: I’ll briefly touch on those at the end of the article.
Azure bill and cost analysis
The prominent place to see the cost of operating Azure Functions is the monthly bill. Open the Azure portal and navigate to your subscription’s page, choose Invoices and then a period to look at. Here’s a sample report from a subscription:
Azure Monitor metrics
Azure Monitor is a service for collecting, analyzing, and acting on telemetry from applications running in the Azure cloud. While mostly focusing on performance, it also collects some useful data related to service consumption.
Azure Functions issue two cost-related metrics into Azure Monitor: Function Execution Count and Function Execution Units. Each metric emits a value once every minute.
To see these metrics in the Azure portal, navigate to the Monitor service and select the Metrics item on the left. Click Select a Resource button and find the Function App that you want to investigate. Note that the Resource Type should be set to App Service: there’s no value called Function App in that dropdown.
Select Function Execution Count in the Metrics dropdown, Sum as the aggregation type, and adjust the period selector as needed. You should now see a chart similar to this one:
In this particular case, there were about 4,940 executions in the last 30 minutes. You can view the stats per minute; in this example, all the executions come from a single spike—something that I might want to investigate.
Now, switch the metric to Function Execution Units. Alternatively, you can add it to the same chart, but the scale of the two metrics is so different that you won’t be able to see both lines at the same time:
The value conversion gets a bit tricky here. The chart shows a total of 634.13 million Function Execution Units consumed in the last hour. These are not the GB-seconds mentioned above, though: the metric is nominated in MB-milliseconds. To convert this to GB-seconds, divide it by 1,024,000. So, in this case, my Function App consumed 634,130,000 / 1,024,000 = 619 GB-seconds in the last half-an-hour.
Sum it up
Let’s estimate the monthly cost of the application based on the metrics above. We start with the half-an-hour calculation:
Execution Count = 4,940 * $0.20 / 1,000,000 = $0.000988
Execution Time = 634,130,000 / 1,024,000 * $16 / 1,000,000 = $0.009908
30 Min Total = Execution Count + Execution Time = $0.010896
If I keep the same average workload over a month, the cost is going to be:
Monthly Cost = 30 Min Total * 2 * 24 * 30 = $15.69
My application costs about 2 cents an hour, or 15 bucks a month.
Daniel Jordan
Fellow Consulting AG
If you have any question please contact me on this channels: