|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create and deploy Azure Functions resources from Terraform' |
| 3 | +description: In this quickstart article, you create a function app in a Flex Consumption plan, along with the resource group, storage account, and blob storage container required by the app. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 05/01/2025 |
| 6 | +ms.custom: devx-track-terraform |
| 7 | +ms.service: azure-functions |
| 8 | +author: ggailey777 |
| 9 | +ms.author: glenga |
| 10 | +#customer intent: As a Terraform user, I want to learn how to create a function app in a Flex Consumption plan along with required storage account and blob storage container used for deployments. |
| 11 | +content_well_notification: |
| 12 | + - AI-contribution |
| 13 | +--- |
| 14 | + |
| 15 | +# Quickstart: Create and deploy Azure Functions resources from Terraform |
| 16 | + |
| 17 | +In this quickstart, you use Terraform to create a function app in a Flex Consumption plan in Azure Functions, along with other required Azure resources. The Flex Consumption plan provides serverless hosting that lets you run your code on demand without explicitly provisioning or managing infrastructure. It's used for processing data, integrating systems, internet-of-things computing, and building simple APIs and microservices. The resources created in this configuration include a unique resource group, a storage account, a blob storage container, the Flex Consumption plan, and the function app itself. The function app runs on Linux and is configured to use blob storage for code deployments. |
| 18 | + |
| 19 | +[!INCLUDE [About Terraform](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 20 | + |
| 21 | +> [!div class="checklist"] |
| 22 | +> * Create an Azure resource group with a unique name. |
| 23 | +> * Generate a random string of 13 lowercase letters to name resources. |
| 24 | +> * Create a storage account in Azure. |
| 25 | +> * Create a blob storage container in the storage account. |
| 26 | +> * Create a Flex Consumption plan in Azure Functions. |
| 27 | +> * Create a function app with a Flex Consumption plan in Azure. |
| 28 | +> * Output the names of the resource group, storage account, service plan, function app, and Azure Functions Flex Consumption plan. |
| 29 | +
|
| 30 | +## Prerequisites |
| 31 | + |
| 32 | +- Create an Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 33 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure). |
| 34 | + |
| 35 | +## Implement the Terraform code |
| 36 | + |
| 37 | +The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-azure-functions). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/101-azure-functions/TestRecord.md). See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform). |
| 38 | + |
| 39 | +1. Create a directory in which to test and run the sample Terraform code, and make it the current directory. |
| 40 | + |
| 41 | +1. Create a file named `main.tf`, and insert the following code: |
| 42 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-functions/main.tf"::: |
| 43 | + |
| 44 | +1. Create a file named `outputs.tf`, and insert the following code: |
| 45 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-functions/outputs.tf"::: |
| 46 | + |
| 47 | +1. Create a file named `providers.tf`, and insert the following code: |
| 48 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-functions/providers.tf"::: |
| 49 | + |
| 50 | +1. Create a file named `variables.tf`, and insert the following code: |
| 51 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-functions/variables.tf"::: |
| 52 | + |
| 53 | +> [!IMPORTANT] |
| 54 | +> If you are using the 4.x azurerm provider, you must [explicitly specify the Azure subscription ID](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/4.0-upgrade-guide#specifying-subscription-id-is-now-mandatory) to authenticate to Azure before running the Terraform commands. |
| 55 | +> |
| 56 | +> One way to specify the Azure subscription ID without putting it in the `providers` block is to specify the subscription ID in an environment variable named `ARM_SUBSCRIPTION_ID`. |
| 57 | +> |
| 58 | +> For more information, see the [Azure provider reference documentation](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#argument-reference). |
| 59 | +
|
| 60 | +## Initialize Terraform |
| 61 | + |
| 62 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 63 | + |
| 64 | +## Create a Terraform execution plan |
| 65 | + |
| 66 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 67 | + |
| 68 | +## Apply a Terraform execution plan |
| 69 | + |
| 70 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 71 | + |
| 72 | +## Verify the results |
| 73 | + |
| 74 | +### [Azure CLI](#tab/azure-cli) |
| 75 | + |
| 76 | +1. Get the Azure resource group name. |
| 77 | + |
| 78 | + ```console |
| 79 | + resource_group_name=$(terraform output -raw resource_group_name) |
| 80 | + ``` |
| 81 | + |
| 82 | +1. Get the storage account name. |
| 83 | + |
| 84 | + ```console |
| 85 | + sa_name=$(terraform output -raw sa_name) |
| 86 | + ``` |
| 87 | + |
| 88 | +1. Get the service plan name. |
| 89 | + |
| 90 | + ```console |
| 91 | + asp_name=$(terraform output -raw asp_name) |
| 92 | + ``` |
| 93 | + |
| 94 | +1. Get the function app plan name. |
| 95 | + |
| 96 | + ```console |
| 97 | + fa_name=$(terraform output -raw fa_name) |
| 98 | + ``` |
| 99 | + |
| 100 | +1. Run `az functionapp show` to view the Azure Functions Flex Consumption plan. |
| 101 | + |
| 102 | + ```azurecli |
| 103 | + az functionapp show --name $function_app_name --resource-group $resource_group_name |
| 104 | + ``` |
| 105 | + |
| 106 | +### [Azure PowerShell](#tab/azure-powershell) |
| 107 | + |
| 108 | +1. Get the Azure resource group name. |
| 109 | + |
| 110 | + ```console |
| 111 | + $resource_group_name=$(terraform output -raw resource_group_name) |
| 112 | + ``` |
| 113 | + |
| 114 | +1. Get the storage account name. |
| 115 | + |
| 116 | + ```console |
| 117 | + $sa_name=$(terraform output -raw sa_name) |
| 118 | + ``` |
| 119 | + |
| 120 | +1. Get the service plan name. |
| 121 | + |
| 122 | + ```console |
| 123 | + $asp_name=$(terraform output -raw asp_name) |
| 124 | + ``` |
| 125 | + |
| 126 | +1. Get the function app plan name. |
| 127 | + |
| 128 | + ```console |
| 129 | + $fa_name=$(terraform output -raw fa_name) |
| 130 | + ``` |
| 131 | + |
| 132 | +1. Run `Get-AzFunctionApp` to view the Azure Functions Flex Consumption plan. |
| 133 | + |
| 134 | + ```azurepowershell |
| 135 | + Get-AzFunctionApp -Name $function_app_name -ResourceGroupName $resource_group_name |
| 136 | + ``` |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +Open a browser and enter the following URL: **https://<fa_name>.azurewebsites.net**. Replace the placeholder `<fa_name>` with the value output by Terraform. |
| 141 | + |
| 142 | +:::image type="content" source="media/functions-create-first-function-terraform/function-app-terraform.png" alt-text="Screenshot of Azure Functions app 'Welcome page'." border="false"::: |
| 143 | + |
| 144 | +## Clean up resources |
| 145 | + |
| 146 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 147 | + |
| 148 | +## Troubleshoot Terraform on Azure |
| 149 | + |
| 150 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot). |
| 151 | + |
| 152 | +## Next steps |
| 153 | + |
| 154 | +[!INCLUDE [functions-quickstarts-infra-next-steps](../../includes/functions-quickstarts-infra-next-steps.md)] |
0 commit comments