Create a Front Door (classic) using Terraform
This quickstart describes how to use Terraform to create a Front Door (classic) profile to set up high availability for a web endpoint.
The steps in this article were tested with the following Terraform and Terraform provider versions:
Prerequisites
- Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
Implement the Terraform code
Create a directory in which to test the sample Terraform code and make it the current directory.
Create a file named
providers.tf
and insert the following code:# Configure the Azure provider terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 3.27.0" } random = { source = "hashicorp/random" version = "~> 3.4.3" } } required_version = ">= 1.1.0" } provider "azurerm" { features {} }
Create a file named
resource-group.tf
and insert the following code:resource "azurerm_resource_group" "my_resource_group" { name = var.resource_group_name location = var.location } resource "random_id" "front_door_name" { byte_length = 8 }
Create a file named
front-door.tf
and insert the following code:locals { front_door_name = "afd-${lower(random_id.front_door_name.hex)}" front_door_frontend_endpoint_name = "frontEndEndpoint" front_door_load_balancing_settings_name = "loadBalancingSettings" front_door_health_probe_settings_name = "healthProbeSettings" front_door_routing_rule_name = "routingRule" front_door_backend_pool_name = "backendPool" } resource "azurerm_frontdoor" "my_front_door" { name = local.front_door_name resource_group_name = azurerm_resource_group.my_resource_group.name frontend_endpoint { name = local.front_door_frontend_endpoint_name host_name = "${local.front_door_name}.azurefd.net" session_affinity_enabled = false } backend_pool_load_balancing { name = local.front_door_load_balancing_settings_name sample_size = 4 successful_samples_required = 2 } backend_pool_health_probe { name = local.front_door_health_probe_settings_name path = "/" protocol = "Http" interval_in_seconds = 120 } backend_pool { name = local.front_door_backend_pool_name backend { host_header = var.backend_address address = var.backend_address http_port = 80 https_port = 443 weight = 50 priority = 1 } load_balancing_name = local.front_door_load_balancing_settings_name health_probe_name = local.front_door_health_probe_settings_name } routing_rule { name = local.front_door_routing_rule_name accepted_protocols = ["Http", "Https"] patterns_to_match = ["/*"] frontend_endpoints = [local.front_door_frontend_endpoint_name] forwarding_configuration { forwarding_protocol = "MatchRequest" backend_pool_name = local.front_door_backend_pool_name } } }
Create a file named
variables.tf
and insert the following code:variable "location" { type = string default = "westus2" } variable "resource_group_name" { type = string default = "FrontDoor" } variable "backend_address" { type = string }
Create a file named
terraform.tfvars
and insert the following code, being sure to update the value to your own backend hostname:backend_address = "<your backend hostname>"
Initialize Terraform
Run terraform init to initialize the Terraform deployment. This command downloads the Azure modules required to manage your Azure resources.
terraform init
Create a Terraform execution plan
Run terraform plan to create an execution plan.
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. - To read more about persisting execution plans and security, see the security warning section.
Apply a Terraform execution plan
Run terraform apply to apply the execution plan to your cloud infrastructure.
terraform apply main.tfplan
Key points:
- The
terraform apply
command above 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
Use the Azure portal, Azure CLI, or Azure PowerShell to list the deployed resources in the resource group.
Sign in to the Azure portal.
Select Resource groups from the left pane.
Select the FrontDoor resource group.
Select the Front Door you created and you'll be able to see the endpoint hostname. Copy the hostname and paste it on to the address bar of a browser. Press enter and your request will automatically get routed to the web app.
Clean up resources
When you no longer need the resources created via Terraform, do the following steps:
Run terraform plan and specify the
destroy
flag.terraform plan -destroy -out main.destroy.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. - To read more about persisting execution plans and security, see the security warning section.
- 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
Next steps
In this quickstart, you deployed a simple Front Door (classic) profile using Terraform. Learn more about Azure Front Door.
Feedback
Submit and view feedback for