Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this quickstart, you use Terraform to create an App Service Environment, single-tenant deployment of Azure App Service. You use it with an Azure virtual network. You need one subnet for a deployment of App Service Environment, and this subnet can't be used for anything else. You create a resource group, virtual network, and a subnet to configure an Azure App Service Environment v3.
In this article, you learn how to:
- Create an Azure resource group with a unique name.
- Establish a virtual network with a specified name and address.
- Generate a random name for the subnet, and create a subnet in the virtual network.
- Delegate the subnet to the Microsoft.Web/hostingEnvironments service.
- Generate a random name for the App Service Environment v3, and create an App Service Environment v3 in the subnet.
- Set the internal load-balancing mode for the App Service Environment v3.
- Set cluster settings for the App Service Environment v3.
- Tag the App Service Environment v3.
- Output the names of the resource group, virtual network, subnet, and App Service Environment v3.
Prerequisites
- An Azure account with an active subscription. You can create an account for free.
- Terraform. For more information, see Install and configure Terraform.
Important
If you're using the 4.x azurerm provider, you must explicitly specify the Azure subscription ID to authenticate to Azure before running the Terraform commands.
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.
For more information, see the Azure provider reference documentation.
Implement the Terraform code
The sample code for this article is located in the Azure Terraform GitHub repo. You can view the log file containing the test results from current and previous versions of Terraform. See more articles and sample code showing how to use Terraform to manage Azure resources.
Create a directory in which to test and run the sample Terraform code, and make it the current directory.
Create a file named
main.tf, and insert the following code:# Create a random pet name to use as a part of the resource group name # for uniqueness resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } # Create a resource group for organizing the App Service Environment resources resource "azurerm_resource_group" "rg" { location = var.resource_group_location name = random_pet.rg_name.id } # A random value for the virtual network name is used if the # virtual_network_name variable is not set resource "random_string" "azurerm_virtual_network_name" { length = 13 lower = true numeric = false special = false upper = false } # Define the virtual network for the App Service Environment resource "azurerm_virtual_network" "example" { name = coalesce(var.virtual_network_name, "vnet-${random_string.azurerm_virtual_network_name.result}") resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location address_space = ["10.0.0.0/16"] } # A random value for the subnet is used if the subnet_name variable is not set resource "random_string" "azurerm_subnet_name" { length = 13 lower = true numeric = false special = false upper = false } # Define a subnet within the virtual network for the App Service Environment resource "azurerm_subnet" "ase" { name = coalesce(var.subnet_name, "subnet-${random_string.azurerm_subnet_name.result}") resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.example.name address_prefixes = ["10.0.1.0/24"] delegation { name = "delegation" service_delegation { name = "Microsoft.Web/hostingEnvironments" actions = ["Microsoft.Network/virtualNetworks/subnets/action"] } } } # A random value for the App Service Environment name is used if the # app_service_environment_v3_name variable is not set resource "random_string" "azurerm_app_service_environment_v3_name" { length = 13 lower = true numeric = false special = false upper = false } # Define the App Service Environment v3 resource resource "azurerm_app_service_environment_v3" "example" { name = coalesce(var.app_service_environment_v3_name, "asev3-${random_string.azurerm_app_service_environment_v3_name.result}") resource_group_name = azurerm_resource_group.rg.name subnet_id = azurerm_subnet.ase.id internal_load_balancing_mode = "Web, Publishing" cluster_setting { name = "DisableTls1.0" value = "1" } cluster_setting { name = "InternalEncryption" value = "true" } cluster_setting { name = "FrontEndSSLCipherSuiteOrder" value = "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" } tags = { env = "production" terraformed = "true" } }Create a file named
outputs.tf, and insert the following code:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "virtual_network_name" { value = azurerm_virtual_network.example.name } output "subnet_name" { value = azurerm_subnet.ase.name } output "app_service_environment_v3_name" { value = azurerm_app_service_environment_v3.example.name }Create a file named
providers.tf, and insert the following code:terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }Create a file named
variables.tf, and insert the following code:variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." } variable "resource_group_location" { type = string default = "eastus" description = "Location of the resource group." } variable "virtual_network_name" { type = string description = "The name of the virtual network resource. The value will be randomly generated if blank." default = "" } variable "subnet_name" { type = string description = "The name of the virtual network subnet. The value will be randomly generated if blank." default = "" } variable "app_service_environment_v3_name" { type = string description = "The name of the App Service Environment v3 resource. The value will be randomly generated if blank." default = "" }
Initialize Terraform
Run terraform init to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
terraform init -upgrade
Key points:
- The
-upgradeparameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.
Create a Terraform execution plan
Run terraform plan to create an execution plan.
terraform plan -out main.tfplan
Key points:
- The
terraform plancommand creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources. - The optional
-outparameter allows you to specify an output file for the plan. Using the-outparameter ensures that the plan you reviewed is exactly what is applied.
Apply a Terraform execution plan
Run terraform apply to apply the execution plan to your cloud infrastructure.
terraform apply main.tfplan
Key points:
- The example
terraform applycommand assumes you previously ranterraform plan -out main.tfplan. - If you specified a different filename for the
-outparameter, use that same filename in the call toterraform apply. - If you didn't use the
-outparameter, callterraform applywithout any parameters.
Verify the results
Get the Azure resource group name.
resource_group_name=$(terraform output -raw resource_group_name)Get the virtual network name.
virtual_network_name=$(terraform output -raw virtual_network_name)Get the subnet name.
subnet_name=$(terraform output -raw subnet_name)Run
az appservice ase showto view the App Service Environment v3.az appservice ase show --name $app_service_environment_v3_name --resource-group $resource_group_name
Clean up resources
When you no longer need the resources created via Terraform, do the following steps:
Run terraform plan and specify the
destroyflag.terraform plan -destroy -out main.destroy.tfplanKey points:
- The
terraform plancommand creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources. - The optional
-outparameter allows you to specify an output file for the plan. Using the-outparameter ensures that the plan you reviewed is exactly what is applied.
- The
Run terraform apply to apply the execution plan.
terraform apply main.destroy.tfplan
Troubleshoot Terraform on Azure
Troubleshoot common problems when using Terraform on Azure.