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.
- 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 = "chinanorth3"
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](https://www.terraform.io/docs/commands/init.html) to initialize the Terraform deployment. This command downloads the Azure provider required to manage your Azure resources.
```console
terraform init -upgrade
Key points:
- The
-upgrade
parameter upgrades the necessary provider plugins to the newest version that complies with the configuration's version constraints.
## Create a Terraform execution plan
```terraform
Run [terraform plan](https://www.terraform.io/docs/commands/plan.html) to create an execution plan.
```console
terraform plan -out main.tfplan
Key points:
- The
terraform plan
command 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
-out
parameter allows you to specify an output file for the plan. Using the-out
parameter ensures that the plan you reviewed is exactly what is applied.```
Apply a Terraform execution plan
Run [terraform apply](https://www.terraform.io/docs/commands/apply.html) to apply the execution plan to your cloud infrastructure.
```console
terraform apply main.tfplan
Key points:
- The example
terraform apply
command assumes you previously ranterraform plan -out main.tfplan
. - If you specified a different filename for the
-out
parameter, use that same filename in the call toterraform apply
. - If you didn't use the
-out
parameter, callterraform apply
without any parameters.
## Verify the results
### [Azure CLI](#tab/azure-cli)
1. Get the Azure resource group name.
```console
resource_group_name=$(terraform output -raw resource_group_name)
```
1. Get the virtual network name.
```console
virtual_network_name=$(terraform output -raw virtual_network_name)
```
1. Get the subnet name.
```console
subnet_name=$(terraform output -raw subnet_name)
```
1. Run `az appservice ase show` to view the App Service Environment v3.
```azurecli
az appservice ase show --name $app_service_environment_v3_name --resource-group $resource_group_name
```
### [Azure PowerShell](#tab/azure-powershell)
1. Get the Azure resource group name.
```console
$resource_group_name=$(terraform output -raw resource_group_name)
```
1. Get the virtual network name.
```console
$virtual_network_name=$(terraform output -virtual_network_name)
```
1. Get the subnet name.
```console
$subnet_name=$(terraform output -subnet_name)
```
1. Run `Get-AzAppServiceEnvironment` to view the AKS cluster within the Azure Extended Zone.
```azurepowershell
Get-AzAppServiceEnvironment -Name $app_service_environment_v3_name -ResourceGroupName $resource_group_name
```
---
## Clean up resources
```terraform
When you no longer need the resources created via Terraform, do the following steps:
1. Run [terraform plan](https://www.terraform.io/docs/commands/plan.html) and specify the `destroy` flag.
```console
terraform plan -destroy -out main.destroy.tfplan
```
[!INCLUDE [terraform-plan-notes.md](terraform-plan-notes.md)]
1. Run [terraform apply](https://www.terraform.io/docs/commands/apply.html) to apply the execution plan.
```console
terraform apply main.destroy.tfplan
```
Troubleshoot Terraform on Azure
Troubleshoot common problems when using Terraform on Azure.