Moving Azure Functions from Consumption to Premium plans

In a demo at the Microsoft’s Reactor event in October I talked about the performance advantages on moving Azure Functions from Consumption to Premium plans when needed on certain scenarios.

When you create a function app in Azure, you must choose a hosting plan for your app. There are three basic hosting plans available for Azure Functions: 

The Azure Functions Premium plan (sometimes referred to as Elastic Premium plan) provides features like VNet connectivity, no cold start, and premium hardware. Multiple function apps can be deployed to the same Premium plan, and the plan allows you to configure compute instance size, base plan size, and maximum plan size. 

Both Consumption and Premium plans automatically add compute power when your code is running. Your app is scaled out when needed to handle load, and scaled in when code stops running.

In the Consumption plan, billing is based on number of executions, execution time, and memory used. Usage is aggregated across all functions within a function app. If no events and executions occours in a period of time, your function app may scale to zero instances and it’s triggered up when a new event arrives (and this can take a bit of time). This is what is called “cold start”.

In the Premium plan, billing is based on the number of core seconds and memory allocated across instances. There is no execution charge with the Premium plan. At least one instance must be allocated at all times per plan. This results in a minimum monthly cost per active plan, regardless if the function is active or idle. Keep in mind that all function apps in a Premium plan share allocated instances.

As normally I suggest, the Consumption plan is a good choice for many scenarios but if you need more power and features or if you need to avoid the “cold start”, you need to switch to a Premium plan. But, as said before, every premium plan will have at least one active (billed) instance at all times and so this could cost you money.

The ideal solution is to “switch” your function app from a Consumption to a Premium plan when needed and then go back to the Consumption plan at the end of the work. Ans this was exactly what I explained at the Reactor event. I promised a follow-up post to explain the details to do so and here it is.

To explain this, consider an Azure Function that I have on my subscription. This Azure Function app runs on a Consumption plan:

Here you can see the plan’s details:

To dinamically change the plan of this function app, we can use Azure CLI and more specifically we need to do the following steps:

  1. We create a Premium plan with the type and resources we want
  2. We move the Function app to the newly created Premium plan
  3. We scale back down the Function app to the Consumption plan at the end of the period of work we need
  4. We delete the Premium plan (don’t forget to do this!)

Here are the Azure CLI commands for each steps. We declare 4 variables that containes the names of the Resource Group where we have deployed our Function app, the name of the Function app itself, the name of the Consumption plan where actually the function is running and the name of the new Premium plan that we need to create:

 $resourceGroup = 'functionappdllmsrg'
 $functionAppName = 'FunctionAppDLLMS'
 $consumptionPlanName = 'FunctionAppDLLMSPlan'
 $premiumPlanName = 'sd_premium_plan'

In the step 1, we create a Premium plan (Elastic Premium 1 tier plan here) with the following command:

az functionapp plan create --name $premiumPlanName --sku EP1 --resource-group $resourceGroup --location 'West Europe'

You can also specify the numbers of minimum and maximum instances to have and other parameters to customize your workload needs. Now you have a Premium plan active on your subscription (and you start pay for it):

In the step 2, we move our Function app to the Premium plan with the following command:

az functionapp update --name $functionAppName --resource-group $resourceGroup --plan $premiumPlanName

As you can see, now our Function app is running on the newly created Premium plan:

At the end of our period of work (where we need the extra power) we can scale back down our Function app to the Consumption plan with the following command (step 3):

az functionapp update --name $functionAppName --resource-group $resourceGroup --plan $consumptionPlanName

And our Function app now runs again in the Consumption plan:

We don’t have to pay fixed costs now 🙂

As a last step (step 4), please don’t forget to delete the “temporary” Premium plan we have created or you will continue to pay for it. The command to delete the plan is the following:

az functionapp plan delete --resource-group $resourceGroup --name $premiumPlanName

This is a trick that in a real production environment where you have your workloads running in a serverless way in the cloud I suggest to evaluate and adopt when needed. You can improve performances a lot.

2 Comments

  1. thx for the script.
    some notes:
    the first ‘plan create’ script needs to be
    az functionapp plan create –name $premiumPlanName …
    and the warning that if you want to use an existing App Service Plan it must be in the same resource group, as the ‘az’ command is limited.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.