Quickstart: Configure an Azure virtual network gateway with Terraform
Article
In this quickstart, you use Terraform to create an Azure ExpressRoute circuit with Equinix as the service provider. The circuit uses a Standard SKU with a bandwidth of 50 Mbps and the peering location of Washington, D.C. Private peering is enabled with a primary and secondary subnet of 192.168.10.16/30 and 192.168.10.20/30, respectively. The script also creates a virtual network and a HighPerformance ExpressRoute gateway.
Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.
In this article, you learn how to:
Create an Azure resource group with a unique name.
Create a virtual network with a subnet for the gateway.
Create a public IP for the gateway.
Create an ExpressRoute circuit and configure private peering.
Output the resource group name, ExpressRoute circuit ID, gateway name, gateway IP, and the service key.
Create a file named outputs.tf, and insert the following code:
Terraform
output"resource_group_name" {
value = azurerm_resource_group.rg.name
}
output"express_route_circuit_id" {
value = azurerm_express_route_circuit.circuit.id
}
output"gateway_name" {
value = azurerm_virtual_network_gateway.gateway.name
}
output"gateway_ip" {
value = azurerm_public_ip.gateway_ip.ip_address
}
output"service_key" {
value = azurerm_express_route_circuit.circuit.service_key
sensitive = true
}
Create a file named providers.tf, and insert the following code:
Terraform
terraform {
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:
Terraform
variable"resource_group_location" {
type = string
default = "eastus"
description = "Location of the resource group."
}
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."
}
Initialize Terraform
Run terraform init 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.
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.
Console
terraform apply main.tfplan
Key points:
The example terraform apply command assumes you previously ran terraform plan -out main.tfplan.
If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
If you didn't use the -out parameter, call terraform apply without any parameters.
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.