QuickStart: Create and configure Azure DDoS Network Protection using Terraform
Article
This QuickStart describes how to use Terraform to create and enable a distributed denial of service (DDoS) protection plan and Azure virtual network (VNet). An Azure DDoS Network Protection plan defines a set of virtual networks that have DDoS protection enabled across subscriptions. You can configure one DDoS protection plan for your organization and link virtual networks from multiple subscriptions to the same plan.
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 a random value for the Azure resource group name using random_pet
Create a file named variables.tf and insert the following code:
Terraform
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"vnet_address_prefix" {
type = string
description = "Specify the virtual network address prefix"
default = "172.17.0.0/16"
}
variable"subnet_prefix" {
type = string
description = "Specify the virtual network subnet prefix"
default = "172.17.0.0/24"
}
variable"ddos_protection_plan_enabled" {
type = bool
description = "Enable DDoS protection plan."
default = true
}
Create a file named outputs.tf and insert the following code:
Terraform
output"resource_group_name" {
value = azurerm_resource_group.rg.name
}
output"ddos_protection_plan_name" {
value = azurerm_network_ddos_protection_plan.ddos.name
}
output"virtual_network_name" {
value = azurerm_virtual_network.vnet.name
}
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.