🚀 Deploy Azure Functions Flex Consumption Using Terraform
Are you ready to supercharge your cloud applications? Azure Functions, paired with the new Flex Consumption Plan, are here to give you unmatched scalability, cost-efficiency, and operational flexibility! In this article, we’ll explore what makes Azure Functions and the Flex Consumption Plan unique, followed by a step-by-step Terraform walkthrough to deploy your Azure Function in just minutes.
Why Azure Functions? 🧩
Flex Consumption is a Linux-based Azure Functions hosting plan that builds on the Consumption pay for what you use serverless billing model. It gives you more flexibility and customizability by introducing private networking, instance memory size selection, and fast/large scale-out features still based on a serverless model.
Benefits
The Flex Consumption plan builds on the strengths of the Consumption plan, which include dynamic scaling and execution-based billing. With Flex Consumption, you also get these extra features:
- Always-ready instances
- Virtual network integration
- Fast scaling based on concurrency for both HTTP and non-HTTP apps
- Multiple choices for instance memory sizes
What Will You Learn in This Guide? 🛠️
By the end of this tutorial, you’ll have:
âś… A deployed Azure Function using the Flex Consumption Plan.
âś… A reusable Terraform configuration to streamline deployments.
âś… Insights into best practices for serverless architecture.
Here’s the full Medium article, including catchy phrases and detailed explanations about Azure Flex Consumption Plan and Azure Functions, with a step-by-step guide for deploying an Azure Function using Terraform.
🚀 Deploy Azure Functions on the Flex Consumption Plan Using Terraform: A Complete Guide
Are you ready to supercharge your cloud applications? Azure Functions, paired with the new Flex Consumption Plan, are here to give you unmatched scalability, cost-efficiency, and operational flexibility! In this article, we’ll explore what makes Azure Functions and the Flex Consumption Plan unique, followed by a step-by-step Terraform walkthrough to deploy your Azure Function in just minutes.
Why Azure Functions? 🧩
Azure Functions is Microsoft’s serverless compute service that allows developers to focus solely on writing code without worrying about infrastructure. Whether you’re building lightweight APIs, automation scripts, or event-driven applications, Azure Functions lets you:
- Scale Automatically: Pay only for what you use.
- Simplify Development: No need to manage servers.
- Integrate Seamlessly: Connect with hundreds of Azure services like Storage, Key Vault, and Logic Apps.
What Is the Azure Flex Consumption Plan? đź’ˇ
The Flex Consumption Plan is a new pricing model for Azure Functions designed for high-performance workloads. Unlike the regular Consumption Plan, Flex provides:
- Dedicated Memory and Scaling: Control memory allocation for each instance (e.g., 2GB or 4GB per instance).
- Custom Instance Count: Set a maximum number of instances for better predictability.
- Enhanced Performance: Ideal for demanding workloads that require both elasticity and consistent throughput.
In short, it’s flexibility redefined for serverless applications!
What Will You Learn in This Guide? 🛠️
By the end of this tutorial, you’ll have:
âś… A deployed Azure Function using the Flex Consumption Plan.
âś… A reusable Terraform configuration to streamline deployments.
âś… Insights into best practices for serverless architecture.
Prerequisites
Before you begin, ensure the following tools are installed on your local system:
- Azure CLI: Install Azure CLI.
- Terraform: Download Terraform.
You’ll also need an Azure account. If you don’t have one, start with a free account.
Step 1: Setting Up Azure CLI
1.1 Log in to Azure
Open a terminal and log in using the Azure CLI:
az login
Follow the browser-based authentication process.
1.2 Set Your Subscription
If you have multiple subscriptions, set the active one:
az account set --subscription "<your-subscription-id>"
Step 2: Initialize Terraform
2.1 Verify Installation
Ensure Terraform is installed:
terraform version
2.2 Initialize Terraform
Run the following command in your project directory to download the required providers:
terraform init
Step 3: Terraform Configuration Files
Let’s break down the necessary Terraform files:
variables.tf
: Define the Input Variables
variable "resourceGroupName" {
description = "The Azure Resource Group name in which all resources in this example should be created."
}
---
variable "location" {
description = "The Azure Region in which all resources in this example should be created."
}
variable "applicationInsightsName" {
description = "Your Application Insights name."
}
variable "logAnalyticsName" {
description = "Your Log Analytics name."
}
variable "functionAppName" {
description = "Your Flex Consumption app name."
}
variable "functionPlanName" {
description = "Your Flex Consumption plan name."
}
variable "storageAccountName" {
description = "Your storage account name."
}
variable "maximumInstanceCount" {
default = 100
description = "The maximum instance count for the app."
}
variable "instanceMemoryMB" {
default = 512
description = "The instance memory for the instances of the app: 2048 or 4096."
}
variable "functionAppRuntime" {
default = "python"
description = "The runtime for your app. One of the following: 'dotnet-isolated', 'python', 'java', 'node', 'powershell'."
}
variable "functionAppRuntimeVersion" {
default = "3.11"
description = "The runtime and version for your app. One of the following: '3.10', '3.11'."
}
variables.tfvars
: Set Variable Values
resourceGroupName = "my-resource-group"
location = "East US"
applicationInsightsName = "my-app-insights"
logAnalyticsName = "my-log-analytics"
functionAppName = "my-function-app"
functionPlanName = "my-function-plan"
storageAccountName = "mystorageaccount123"
maximumInstanceCount = 100
instanceMemoryMB = 512
functionAppRuntime = "dotnet-isolated"
functionAppRuntimeVersion = "8.0"
main.tf
: Define Resources
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
}
azapi = {
source = "Azure/azapi"
}
}
}
provider "azurerm" {
features {}
}
# Resource Group
resource "azurerm_resource_group" "rg" {
location = var.location
name = var.resourceGroupName
}
# Function Plan (Flex Consumption)
resource "azapi_resource" "serverFarm" {
type = "Microsoft.Web/serverfarms@2023-12-01"
schema_validation_enabled = false
location = var.location
name = var.functionPlanName
parent_id = azurerm_resource_group.rg.id
body = {
kind = "functionapp",
sku = {
tier = "FlexConsumption",
name = "FC1"
},
properties = {
reserved = true
}
}
}
# Storage Account for Function App
resource "azurerm_storage_account" "storageAccount" {
name = var.storageAccountName
resource_group_name = azurerm_resource_group.rg.name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}
# Storage Container for Deployment
resource "azurerm_storage_container" "storageContainer" {
name = "deploymentpackage"
storage_account_name = azurerm_storage_account.storageAccount.name
container_access_type = "private"
}
# Log Analytics Workspace
resource "azurerm_log_analytics_workspace" "logAnalyticsWorkspace" {
name = var.logAnalyticsName
location = var.location
resource_group_name = azurerm_resource_group.rg.name
sku = "PerGB2018"
retention_in_days = 30
}
# Application Insights
resource "azurerm_application_insights" "appInsights" {
name = var.applicationInsightsName
location = var.location
resource_group_name = azurerm_resource_group.rg.name
application_type = "web"
workspace_id = azurerm_log_analytics_workspace.logAnalyticsWorkspace.id
}
locals {
blobStorageAndContainer = "${azurerm_storage_account.storageAccount.primary_blob_endpoint}deploymentpackage"
}
# Function App Deployment
resource "azapi_resource" "functionApps" {
type = "Microsoft.Web/sites@2023-12-01"
schema_validation_enabled = false
location = var.location
name = var.functionAppName
parent_id = azurerm_resource_group.rg.id
body = {
kind = "functionapp,linux",
identity = {
type = "SystemAssigned"
}
properties = {
serverFarmId = azapi_resource.serverFarm.id,
functionAppConfig = {
deployment = {
storage = {
type = "blobContainer",
value = local.blobStorageAndContainer,
authentication = {
type = "SystemAssignedIdentity"
}
}
},
scaleAndConcurrency = {
maximumInstanceCount = var.maximumInstanceCount,
instanceMemoryMB = var.instanceMemoryMB
},
runtime = {
name = var.functionAppRuntime,
version = var.functionAppRuntimeVersion
}
},
siteConfig = {
appSettings = [
{
name = "AzureWebJobsStorage__accountName",
value = azurerm_storage_account.storageAccount.name
},
{
name = "APPLICATIONINSIGHTS_CONNECTION_STRING",
value = azurerm_application_insights.appInsights.connection_string
}
]
}
}
}
depends_on = [ azapi_resource.serverFarm, azurerm_application_insights.appInsights, azurerm_storage_account.storageAccount ]
}
data "azurerm_linux_function_app" "fn_wrapper" {
name = azapi_resource.functionApps.name
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_role_assignment" "storage_roleassignment" {
scope = azurerm_storage_account.storageAccount.id
role_definition_name = "Storage Blob Data Owner"
principal_id = data.azurerm_linux_function_app.fn_wrapper.identity.0.principal_id
}
Step 4: Deploy Your Azure Function
Run the following commands to deploy:
Initialize Terraform:
terraform init
Validate the configuration:
terraform validate
Plan the deployment:
terraform plan -var-file="variables.tfvars"
Apply the deployment:
terraform apply -var-file="variables.tfvars"
Conclusion
Congratulations! 🎉 You’ve successfully deployed an Azure Function using the Flex Consumption Plan. This approach gives you fine-grained control over your serverless environment while leveraging Terraform for repeatable deployments. Start building scalable, cost-efficient serverless solutions today!