Quickstart: Create an Azure Front Door (classic) using Terraform
Important
Azure Front Door (classic) will be retired on March 31, 2027. To avoid any service disruption, it is important that you migrate your Azure Front Door (classic) profiles to Azure Front Door Standard or Premium tier by March 2027. For more information, see Azure Front Door (classic) retirement.
This quickstart describes how to use Terraform to create a Front Door (classic) profile to set up high availability for a web endpoint.
In this article, you learn how to:
- Create a random value for the Azure resource group name using random_pet.
- Create an Azure resource group using azurerm_resource_group.
- Create a random value for the Front Door endpoint host name using random_id.
- Create a Front Door (classic) resource using - azurerm_frontdoor.
Prerequisites
- Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
Implement the Terraform code
Note
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 the sample Terraform code and make it the current directory.
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
main.tf
and insert the following code:resource "random_pet" "rg-name" { prefix = var.resource_group_name_prefix } resource "azurerm_resource_group" "rg" { name = random_pet.rg-name.id location = var.resource_group_location } resource "random_id" "front_door_name" { byte_length = 8 } 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" "main" { name = local.front_door_name resource_group_name = azurerm_resource_group.rg.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 } backend_pool_settings { backend_pools_send_receive_timeout_seconds = 0 enforce_backend_pools_certificate_name_check = false } 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 "resource_group_location" { type = string description = "Location for all resources." default = "eastus" } variable "resource_group_name_prefix" { type = string description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." default = "rg" } variable "backend_address" { type = string description = "Backend address." default = "www.bing.com" }
Create a file named
outputs.tf
and insert the following code, being sure to update the value to your own backend hostname:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "frontDoorEndpointHostName" { value = azurerm_frontdoor.main.frontend_endpoint[0].host_name }
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
-upgrade
parameter 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 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 to apply the execution plan to your cloud infrastructure.
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
Get the Front Door endpoint:
terraform output -raw frontDoorEndpointHostName
Paste the endpoint into a browser.
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.
- 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
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for