Switching between Azure Functions Consumption and Premium Plans with Bicep

Azure Functions offer great flexibility with the consumption plan, being able to scale out and handle huge amounts of events while not costing anything if there’s no traffic. But sometimes you need even better performance or eliminate potential cold start times. That’s when it makes sense to switch to a Premium Plan, which can be achieved using Azure Bicep.

The following script shows how to swtich back and forth between a consumption plan Y1 and a premoum plan EP1 simply by passing a boolean to the module. There are some parameters for scaling that need to be set correctly for it to work, so note the comments in the Bicep file.

param isPremium bool = false
param location string = resourceGroup().location
var storageAccountName = storageaccountname'
var functionAppName = 'functionappname'
var hostingPlanName = 'functionapphostingplanname'
resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: storageAccountName
location: location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
}
resource hostingPlan 'Microsoft.Web/serverfarms@2022-09-01' = {
name: hostingPlanName
location: location
sku: {
name: isPremium ? 'EP1' : 'Y1'
capacity: isPremium ? 6 : 0 // must be smaller or equal to maximumElasticWorkerCount
}
properties: {
maximumElasticWorkerCount: 6 // "Maximum Burst", ignored for Y1 as consumption apps scale independently
elasticScaleEnabled: isPremium
}
}
resource functionApp 'Microsoft.Web/sites@2022-09-01' = {
name: functionAppName
location: location
kind: 'functionapp'
identity: {
type: 'SystemAssigned'
}
properties: {
serverFarmId: hostingPlan.id
siteConfig: {
appSettings: [
{
name: 'AzureWebJobsStorage'
value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
}
{
name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
}
{
name: 'WEBSITE_CONTENTSHARE'
value: toLower(functionAppName)
}
{
name: 'FUNCTIONS_EXTENSION_VERSION'
value: '~4'
}
{
name: 'WEBSITE_NODE_DEFAULT_VERSION'
value: '~18'
}
{
name: 'FUNCTIONS_WORKER_RUNTIME'
value: 'node'
}
]
functionAppScaleLimit: 7 // only for consumption
}
httpsOnly: true
}
}
Bicep two switch back and forth between Consumption and Premium Plans for Azure Functions

Leave a Reply

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