Quickstart: Create a public IP address prefix using Terraform

Learn about a public IP address prefix and how to create, change, and delete one. A public IP address prefix is a contiguous range of standard SKU public IP addresses.

When you create a public IP address resource, you can assign a static public IP address from the prefix and associate the address to virtual machines, load balancers, or other resources. For more information, see Public IP address prefix overview.

If you don't have an Azure trail subscription, create a trial subscription before you begin.

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 pet name for the Azure resource group name using random_pet
  • Create an Azure resource group using azurerm_resource_group
  • Create a standard zone-redundant public IPv4 address prefix named myIPv4
  • Create a standard zonal public IPv4 address named myIPv4Zonal
  • Create a standard non-zonal public IPv4 address named myIPv4NonZonal
  • Create a standard public IPv4 address named myIPv4RPInternet that supports the Routing Preference feature
  • Create a standard zone-redundant public IPv6 address prefix named myIPv6
  • Create a standard zonal public IPv6 address named myIPv6Zonal
  • Create a standard non-zonal public IPv6 address named myIPv6NonZonal
  • Create a static public IP IPv4 address from an IP prefix
  • Create a static public IP IPv6 address from an IP prefix

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.

Create a resource group

An Azure resource group is a logical container into which Azure resources are deployed and managed.

# Random pet resource to generate a unique name for the resource group
resource "random_pet" "rg_name" {
  prefix = var.resource_group_name_prefix
}

# Create a resource group
resource "azurerm_resource_group" "example" {
  location = var.resource_group_location
  name     = random_pet.rg_name.id
}

Create a public IP address prefix

In this section, you create a zone redundant, a zonal, and a non-zonal public IP prefix using Azure PowerShell.

The prefixes in the examples are:

  • IPv4 - /28 (16 addresses)

  • IPv6 - /124 (16 addresses)

For more information on available prefix sizes, see Prefix sizes.

IPv4

To create an IPv4 public IP prefix, specify IPv4 as the ip_version value. To create a zone redundant IPv4 prefix, specify ["1", "2", "3"] as the zone value.

# Create a public IP prefix: IPv4 Zone redundant
resource "azurerm_public_ip_prefix" "my_ipv4" {
  name                = "myIPv4"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_version    = "IPv4"
  prefix_length = 28

  zones = ["1", "2", "3"]
}

To create an IPv4 public IP prefix with routing preference set to Internet, add RoutingPreference=Internet to the tags block.

# Create a public IP prefix: IPv4 with Routing Preference set to Internet
resource "azurerm_public_ip_prefix" "my_ipv4_rp_internet" {
  name                = "myIPv4RPInternet"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_version    = "IPv4"
  prefix_length = 28

  tags = {
    RoutingPreference = "Internet"
  }
}

IPv6

To create an IPv6 public IP prefix, specify IPv6 as the ip_version value. To create a zone redundant IPv6 prefix, specify ["1", "2", "3"] as the zone value.

# Create a public IP prefix: IPv6 Zone redundant
resource "azurerm_public_ip_prefix" "my_ipv6" {
  name                = "myIpv6"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_version    = "IPv6"
  prefix_length = 124

  zones = ["1", "2", "3"]
}

Create a static public IP address from a prefix

Once you create a prefix, you can create static IP addresses from the prefix. In this section, you see how to create a prefix and then create an address that points to the prefix.

# Create a public IP prefix: IPv4
resource "azurerm_public_ip_prefix" "my_public_ip_prefix_ipv4" {
  name                = "myPublicIpPrefix1"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_version    = "IPv4" # Default
  prefix_length = 28
}

# Create a public IP (IPv4) and specify the public IP prefix
resource "azurerm_public_ip" "my_public_ip_ipv4" {
  name                = "myPublicIPIPv4"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  sku                 = "Standard"
  allocation_method   = "Static"

  ip_version = "IPv4"

  public_ip_prefix_id = azurerm_public_ip_prefix.my_public_ip_prefix_ipv4.id
}

Note

Only static public IP addresses created with the standard SKU can be assigned from the prefix's range. To learn more about public IP address SKUs, see public IP address.

Delete a prefix

In this section, you learn how to delete a prefix at the command line.

To delete a public IP prefix, use az network public-ip prefix delete.

  az network public-ip prefix delete \
    --resource-group <resource_group_name>
    --name <public_ip_prefix_name> \

Clean up resources

When you no longer need the resources created via Terraform, do the following steps:

  1. 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.
  1. 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