Azure Function Flex Consumption is a great way to scale your serverless functions, allowing you to pay only for the resources you use. This approach is perfect for applications with varying workloads.
To get started, you'll need to create an Azure Function App and choose the Consumption plan. This plan is designed to automatically scale based on demand, so you don't have to worry about provisioning or managing servers.
With Flex Consumption, you can scale your functions up or down as needed, without having to manually adjust settings. This flexibility is especially useful for applications with unpredictable workloads.
A fresh viewpoint: Azure Function Flex Consumption
Prerequisites
To set up Azure Function Flex Consumption, you'll need to meet some prerequisites. You'll need an Azure account with an active subscription, which you can create for free if you don't already have one.
To create and manage resources in Azure, you'll need to use the Azure CLI, which should be version 2.60.0 or later. Alternatively, you can use Azure Cloud Shell, which has the correct Azure CLI version.
Here's an interesting read: Azure Function Change Runtime Version
For development and deployment, you'll need Visual Studio Code, which should also have the latest Azure Functions extension installed. The Azure Tools extension pack is also recommended.
To deploy a code project to Azure, you'll need a code project with an HTTP triggered function. This can be created using one of the quickstart articles, which will guide you through the process.
Here's a summary of the prerequisites:
- Azure account with an active subscription
- Azure CLI version 2.60.0 or later
- Visual Studio Code with the latest Azure Functions extension
- A code project with an HTTP triggered function
Azure Function Flex Consumption Setup
To set up an Azure Function in the Flex Consumption plan, you'll need to create a Service Plan with a Flex Consumption SKU, a Storage account, and the Function App itself. This plan combines benefits from the Consumption plan and additional features like always-already instances or VNet integration.
The Azure resources to provision include a Service Plan, a Storage account, and the Function App. You'll also need to create a Managed Identity for the Function App and a Blob Container to contain the deployment package.
Here's a summary of the required resources:
- Service Plan with a Flex Consumption SKU
- Storage account
- Function App
- Managed Identity for the Function App
- Blob Container for the deployment package
Flex App Creation
To create a Flex App in Azure, you'll need to set up a Service Plan with a Flex Consumption SKU. This plan allows you to deploy your application package to a blob container, which your function app will run from.
You'll also need a Storage account to store the deployment package. In the diagram, you can see that a Managed Identity for the Function App will be created, as well as a Blob Container to contain the deployment package.
To create the necessary resources, start by creating a resource group. This will be the parent container for all your Azure resources.
You'll then need to create the blob container that will contain the application package. I named mine "deploymentpackage", but you can choose any name you like.
Here's a list of the resources you'll need to provision:
- Service Plan with a Flex Consumption SKU
- Storage account
- Function App itself
By using Azure RBAC, you can assign the storage blob data contributor role to the Function App managed identity, making it more secure than adding an application setting with the storage connection string.
Consumption/Premium Plans
In a Consumption or Elastic Premium plan, you can specify a lower maximum limit for your app by modifying the value of the functionAppScaleLimit site configuration setting.
The functionAppScaleLimit can be set to 0 or null for unrestricted, or a valid value between 1 and the app maximum.
This flexibility is a welcome addition, as it allows you to fine-tune your app's scaling behavior to suit your specific needs.
For example, if you set the functionAppScaleLimit to 10, your app will be limited to a maximum of 10 instances, giving you more control over resource utilization.
By setting this value, you can ensure that your app doesn't exceed a certain threshold, helping you maintain a healthy balance between scaling and resource usage.
Worth a look: Azure App Insights vs Azure Monitor
Configuration
You can configure your Azure Function Flex Consumption plan by setting the instance memory size and always ready instance counts. You can explicitly set the instance memory size when creating your app using the az functionapp create command with the --instance-memory parameter.
To set the instance memory size, you can specify a value in megabytes, such as 4096, which is the size used in the example. You can also change the instance memory size setting at any point using the az functionapp scale config set command, like in the example that changes the instance memory size setting to 4,096 MB.
Always ready instance counts can be set to keep your functions loaded and ready to execute. You can use the --always-ready-instances parameter with the az functionapp create command to define one or more always ready instance designations, such as setting the always ready instance count for all HTTP triggered functions to 5.
Configure Deployment Settings
In the Flex Consumption plan, you can choose your preferred authentication method by configuring your app's deployment settings.
By default, the same storage account used to store internal host metadata is also used as the deployment container, but you can use an alternative storage account.
Check this out: Azure Function Disappeared after Deployment
You no longer need to worry about app settings influencing deployment behavior, as the streamlined deployment path takes care of it.
To use an alternative storage account, you'll need to configure your app's deployment settings accordingly.
The deployment container is a blob storage container that holds your project code, which is built and zipped into an application package.
For more insights, see: Azure Linux Function App
Configure Instance Memory
Configure instance memory by specifying the --instance-memory parameter in your az functionapp create command, such as creating a C# app with an instance size of 4096.
You can choose your instance memory size in the Instance size field in the Basics tab when creating your app in the Azure portal. The default size is used if you're creating your app in a Flex Consumption plan.
The default instance memory size is 2048 MB, but you can change it to 4096 MB if your app requires more concurrency or higher processing power. You can change the instance memory size at any time.
To change the instance memory size setting used by your app, use the az functionapp scale config set command, such as changing the instance memory size setting to 4,096 MB. You can't currently change the instance memory size setting for your app using Visual Studio Code.
Here are the available instance memory size options:
- 2048 MB (default)
- 4096 MB
Understanding Scaling Behaviors
Scaling behaviors in Azure Functions Flex Consumption can be a bit complex, but don't worry, I've got you covered.
Maximum instances are limited by the plan, but a single instance can process more than one message or request at a time. You can specify a lower maximum to throttle scale as required.
New instance rates vary by trigger type: HTTP triggers can get new instances at most once per second, while non-HTTP triggers can get new instances at most once every 30 seconds. Scaling is faster when running in a Premium plan.
Target-based scaling is a fast and intuitive scaling model, currently supported for Service Bus queues and topics, Storage queues, Event Hubs, Apache Kafka, and Azure Cosmos DB extensions. Make sure to review target-based scaling to understand their scaling behavior.
Discover more: Http Triggered Azure Function
Per-function scaling groups functions together based on trigger types, except for HTTP triggers, Blob storage triggers, and Durable Functions, which have their own shared instances.
Here's a list of per-function scaling groups and their corresponding trigger types:
- http - all the HTTP triggered functions in the app scale together into their own instances.
- durable - all the Durable triggered functions (Orchestration, Activity, Entity) in the app scale together into their own instances.
- blob - all the blob (Event Grid) triggered functions in the app scale together into their own instances.
Maximum monitored triggers are limited to 100, so if your app has more than 100 event-based triggers, scale decisions are made based on only the first 100 triggers that execute.
Readers also liked: Azure Functions Triggers
Cost and Billing
In the Flex Consumption plan, your costs are determined by two billing modes: On Demand and Always Ready. On Demand mode bills you only for the time your function code is executing on your available instances.
You're billed for the total amount of memory provisioned while each on demand instance is actively executing functions, minus a free grant of GB-s per month. The minimum billable execution period for On Demand mode is 1,000 ms, and past that, the billable activity period is rounded up to the nearest 100 ms.
Always Ready mode bills you for the total amount of memory provisioned across all of your always ready instances, known as the baseline, and the total amount of memory provisioned during the time each always ready instance is actively executing functions. There are no free grants in Always Ready billing.
You can incur costs for data transfer depending on the direction and scenario of the data movement. This includes costs for data transfer through network bandwidth.
Here are the details of the related costs you should consider when estimating the overall cost of running your functions in the Flex Consumption plan:
Metrics and Monitoring
To get a clear picture of your Azure Function's performance and costs, you'll want to dive into metrics and monitoring. You can use Azure Monitor to view cost-related metrics, such as Function Execution Count and Function Execution Units, which can be used to calculate the cost of your function app.
Take a look at this: Azure Functions Costing
To access these metrics, navigate to your function app in the Azure portal, scroll down to Monitoring, and choose Metrics. From Metric, select Function Execution Count and Sum for Aggregation to add the sum of execution counts during a chosen period to the chart.
You can also use the Azure CLI or Azure PowerShell to retrieve metrics. The az monitor metrics list command returns hourly data, while the Get-AzMetric command returns the actual metric values.
One important thing to note is that Function Execution Units are a combination of execution time and memory usage, making it a difficult metric for understanding memory usage. To optimize memory usage, you can use performance counter data collected by Application Insights, which can be enabled in your function app.
Here are the ways to access metrics and monitoring data:
- Azure Monitor metrics explorer in the Azure portal
- Azure CLI using the az monitor metrics list command
- Azure PowerShell using the Get-AzMetric command
By using these tools, you can get a better understanding of your function app's performance and costs, and make informed decisions to optimize its usage. For example, you can use the Function Execution Units metric to calculate the cost of your function app, and use the performance counter data to optimize memory usage.
Flex Plan Details
The Flex Consumption plan is a new hosting plan for Azure Functions that combines benefits from the Consumption plan and additional features like always-already instances or VNet integration.
You can deploy a Function App in the Flex Consumption plan using Infrastructure as Code, which is a game-changer for managing your apps.
By default, apps running in a Flex Consumption plan have a limit of 100 overall instances.
The lowest maximum instance count value is 40, and the highest supported maximum instance count value is 1000.
You can change the maximum instance count of Flex Consumption apps up to 1000 using the az functionapp create command with the --maximum-instance-count parameter.
This example creates an app with a maximum instance count of 200.
You can also change the maximum instance count for an existing app to 150 using the az functionapp scale config set command.
Keep in mind that your apps will reach a quota limit before reaching the maximum instance count of 1000, so be sure to review Regional subscription memory quotas for more details.
Intriguing read: Ms Azure App
Regions and Quotas
Each region in a given subscription has a memory limit of 512,000 MB for all instances of apps running on Flex Consumption plans.
This quota can be increased if your apps require a larger limit, and you can do this by creating a support ticket.
To give you a better idea, here are some examples of what would reach the quota limit:
- You have one 2,048 MB app scaled to 100 and a second 2,048 MB app scaled to 150 instances
- You have one 2,048 MB app that scaled out to 250 instances
- You have one 4,096 MB app that scaled out to 125 instances
- You have one 4,096 MB app scaled to 100 and one 2,048 MB app scaled to 50 instances
Supported Regions
Currently supported regions for Flex Consumption plans are filtered out of the region list when you create an app in the Azure portal or by using Visual Studio Code.
You can view the list of regions that currently support Flex Consumption plans by creating an app in the Azure portal or by using Visual Studio Code.
Check this out: Azure Data Studio vs Azure Data Explorer
Regional Subscription Quotas
Regional Subscription Quotas are a crucial factor to consider when scaling your apps. Each region in a given subscription has a memory limit of 512,000 MB for all instances of apps running on Flex Consumption plans.
Explore further: Azure Functions vs Logic Apps
This quota can be exceeded if you have a combination of instance memory sizes and counts that add up to more than the limit. For example, running one 2,048 MB app scaled to 100 instances and a second 2,048 MB app scaled to 150 instances would mean the quota has been reached.
If you have one 2,048 MB app that scaled out to 250 instances, the quota would also be exceeded. Similarly, having one 4,096 MB app that scaled out to 125 instances would also hit the quota limit.
You can check if your apps are within the quota limit by looking at the number of instances and memory sizes. For instance, running one 4,096 MB app scaled to 100 instances and one 2,048 MB app scaled to 50 instances would also exceed the quota.
Here are some examples of combinations that would hit the quota limit:
- You have one 2,048 MB app scaled to 100 and a second 2,048 MB app scaled to 150 instances
- You have one 2,048 MB app that scaled out to 250 instances
- You have one 4,096 MB app that scaled out to 125 instances
- You have one 4,096 MB app scaled to 100 and one 2,048 MB app scaled to 50 instances
Benefits and Considerations
The Flex Consumption plan offers several benefits over the Consumption plan, including always-ready instances, virtual network integration, and fast scaling based on concurrency for both HTTP and non-HTTP apps.
With Flex Consumption, you can scale to zero, and the scale behavior is event driven, but even faster than the Consumption plan. You can also choose from multiple instance memory sizes.
Here are some key benefits of Flex Consumption compared to Consumption:
However, there are some considerations to keep in mind when using Flex Consumption. For example, the host has a 30-second timeout for app initialization, and Azure Storage is the only supported storage provider for Durable Functions.
Benefits
The Flex Consumption plan offers several benefits over the Consumption plan. It provides always-ready instances, which can help mitigate cold starts.
Virtual network integration is also supported in Flex Consumption, making it a more robust option. This feature allows for better network management and organization.
Fast scaling based on concurrency for both HTTP and non-HTTP apps is another advantage of Flex Consumption. This means that your app can scale quickly and efficiently in response to changing traffic patterns.
Multiple choices for instance memory sizes are available in Flex Consumption. This gives you more flexibility when selecting the resources you need for your app.
Here's a comparison of the features of Flex Consumption and the Consumption hosting plan:
Considerations
When creating and managing function apps in the Flex Consumption plan, there are some important considerations to keep in mind.
The host has a 30 second timeout for app initialization, which means that if your function app takes longer than 30 seconds to start, you might see gRPC-related System.TimeoutException entries logged.
Azure Storage is the only supported storage provider for Durable Functions when hosted in the Flex Consumption plan.
To integrate your function app with a virtual network, ensure that the Microsoft.App Azure resource provider is enabled for your subscription by following the instructions provided.
All triggers are fully supported except for Kafka and Azure SQL triggers, and the Blob storage trigger only supports the Event Grid source.
Currently, not all regions are supported for function apps in the Flex Consumption plan, so be sure to check the supported regions before deployment.
The lowest maximum scale for function apps in the Flex Consumption plan is 40, while the highest currently supported value is 1000.
Here are some key limitations to keep in mind:
- Deployment slots are not currently supported.
- Managed dependencies in PowerShell aren't supported by Flex Consumption.
- Diagnostic settings are not currently supported.
- Loading certificates with the WEBSITE_LOAD_CERTIFICATES app setting is currently not supported.
- Key Vault references in app settings don't work when Key Vault is network access restricted.
Sources
- https://learn.microsoft.com/en-us/azure/azure-functions/flex-consumption-how-to
- https://learn.microsoft.com/en-us/azure/azure-functions/flex-consumption-plan
- https://techwatching.dev/posts/flex-consumption-plan
- https://learn.microsoft.com/en-us/azure/azure-functions/event-driven-scaling
- https://learn.microsoft.com/en-us/azure/azure-functions/functions-consumption-costs
Featured Images: pexels.com