|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create a public IP address prefix - Terraform' |
| 3 | +titleSuffix: Azure Virtual Network |
| 4 | +description: Learn how to create a public IP address prefix using Terraform. |
| 5 | +services: virtual-network |
| 6 | +author: mbender-ms |
| 7 | +ms.author: mbender |
| 8 | +ms.service: azure-virtual-network |
| 9 | +ms.subservice: ip-services |
| 10 | +ms.topic: quickstart |
| 11 | +ms.date: 02/18/2025 |
| 12 | +ms.custom: mode-api, devx-track-terraform |
| 13 | +--- |
| 14 | + |
| 15 | +# Quickstart: Create a public IP address prefix using Terraform |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +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](public-ip-address-prefix.md). |
| 20 | + |
| 21 | +[!INCLUDE [quickstarts-free-trial-note](~/reusable-content/ce-skilling/azure/includes/quickstarts-free-trial-note.md)] |
| 22 | + |
| 23 | +[!INCLUDE [Terraform abstract](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 24 | + |
| 25 | +In this article, you learn how to: |
| 26 | + |
| 27 | +> [!div class="checklist"] |
| 28 | +> * Create a random pet name for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) |
| 29 | +> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) |
| 30 | +> * Create a standard zone-redundant public IPv4 address prefix named **myIPv4** |
| 31 | +> * Create a standard zonal public IPv4 address named **myIPv4Zonal** |
| 32 | +> * Create a standard non-zonal public IPv4 address named **myIPv4NonZonal** |
| 33 | +> * Create a standard public IPv4 address named **myIPv4RPInternet** that supports the Routing Preference feature |
| 34 | +> * Create a standard zone-redundant public IPv6 address prefix named **myIPv6** |
| 35 | +> * Create a standard zonal public IPv6 address named **myIPv6Zonal** |
| 36 | +> * Create a standard non-zonal public IPv6 address named **myIPv6NonZonal** |
| 37 | +> * Create a static public IP IPv4 address from an IP prefix |
| 38 | +> * Create a static public IP IPv6 address from an IP prefix |
| 39 | +
|
| 40 | +> [!NOTE] |
| 41 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-virtual-network-public-ip-prefix). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/101-virtual-network-public-ip-prefix/TestRecord.md). |
| 42 | +> |
| 43 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform). |
| 44 | +
|
| 45 | +## Create a resource group |
| 46 | + |
| 47 | +An Azure resource group is a logical container into which Azure resources are deployed and managed. |
| 48 | + |
| 49 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="1-10"::: |
| 50 | + |
| 51 | +## Create a public IP address prefix |
| 52 | + |
| 53 | +In this section, you create a zone redundant, a zonal, and a non-zonal public IP prefix using Azure PowerShell. |
| 54 | + |
| 55 | +The prefixes in the examples are: |
| 56 | + |
| 57 | +* **IPv4** - /28 (16 addresses) |
| 58 | + |
| 59 | +* **IPv6** - /124 (16 addresses) |
| 60 | + |
| 61 | +For more information on available prefix sizes, see [Prefix sizes](public-ip-address-prefix.md#prefix-sizes). |
| 62 | + |
| 63 | +## IPv4 |
| 64 | + |
| 65 | +# [Zone redundant IPv4 prefix](#tab/ipv4-zone-redundant) |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="12-22" highlight="7-8,10"::: |
| 70 | + |
| 71 | +# [Zonal IPv4 prefix](#tab/ipv4-zonal) |
| 72 | + |
| 73 | +To create an IPv4 public IP prefix, specify **IPv4** as the `ip_version` value. To create a zonal IP prefix in zone 2, specify **["2"]** as the `zone` value. |
| 74 | + |
| 75 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="24-34" highlight="7-8,10"::: |
| 76 | + |
| 77 | +>[!NOTE] |
| 78 | +>For more information about availability zones, see [What are availability zones?](../../reliability/availability-zones-overview.md?toc=%2fazure%2fvirtual-network%2ftoc.json). |
| 79 | +
|
| 80 | +# [Non-zonal IPv4 prefix](#tab/ipv4-non-zonal) |
| 81 | + |
| 82 | +To create an IPv4 public IP prefix, specify **IPv4** as the `ip_version` value. To create a non-zonal IP prefix, omit the `zone` field. |
| 83 | + |
| 84 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="36-44" highlight="7-8"::: |
| 85 | + |
| 86 | +The omission of the `zone` field is valid in all regions. |
| 87 | + |
| 88 | +The omission of the `zone` field is the default selection for standard public IP addresses in regions without [Availability Zones](../../reliability/availability-zones-overview.md?toc=%2fazure%2fvirtual-network%2ftoc.json). |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +# [Routing Preference Internet IPv4 prefix](#tab/ipv4-routing-pref) |
| 93 | + |
| 94 | +To create an IPv4 public IP prefix with routing preference set to Internet, add **RoutingPreference=Internet** to the `tags` block. |
| 95 | + |
| 96 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="46-58" highlight="7-8,10-12"::: |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## IPv6 |
| 101 | + |
| 102 | +# [Zone redundant IPv6 prefix](#tab/ipv6-zone-redundant) |
| 103 | + |
| 104 | +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. |
| 105 | + |
| 106 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="60-70" highlight="7-8,10"::: |
| 107 | + |
| 108 | +# [Zonal IPv6 prefix](#tab/ipv6-zonal) |
| 109 | + |
| 110 | +To create an IPv6 public IP prefix, specify **IPv6** as the `ip_version` value. To create a zone redundant IPv6 prefix, specify **["2"]** as the `zone` value. |
| 111 | + |
| 112 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="72-82" highlight="7-8,10"::: |
| 113 | + |
| 114 | +>[!NOTE] |
| 115 | +>For more information about availability zones, see [What are availability zones?](../../reliability/availability-zones-overview.md?toc=%2fazure%2fvirtual-network%2ftoc.json). |
| 116 | +
|
| 117 | +# [Non-zonal IPv6 prefix](#tab/ipv6-non-zonal) |
| 118 | + |
| 119 | +To create an IPv6 public IP prefix, specify **IPv6** as the `ip_version` value. To create a non-zonal IP prefix, omit the `zone` field. |
| 120 | + |
| 121 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="84-92" highlight="7-8"::: |
| 122 | + |
| 123 | +The omission of the `zone` field is valid in all regions. |
| 124 | + |
| 125 | +The omission of the `zone` field is the default selection for standard public IP addresses in regions without [Availability Zones](../../reliability/availability-zones-overview.md?toc=%2fazure%2fvirtual-network%2ftoc.json). |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## Create a static public IP address from a prefix |
| 130 | + |
| 131 | +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. |
| 132 | + |
| 133 | +# [IPv4 address](#tab/ipv4-address) |
| 134 | + |
| 135 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="107-115,93-105" highlight="2,21"::: |
| 136 | + |
| 137 | +# [IPv6 address](#tab/ipv6-address) |
| 138 | + |
| 139 | +:::code language="terraform" source="~/terraform_samples/quickstart/101-virtual-network-public-ip-prefix/main.tf" range="130-138, 116-128" highlight="2,21"::: |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +>[!NOTE] |
| 144 | +>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](public-ip-addresses.md#public-ip-addresses). |
| 145 | +
|
| 146 | +## Delete a prefix |
| 147 | + |
| 148 | +In this section, you learn how to delete a prefix at the command line. |
| 149 | + |
| 150 | +# [Azure CLI](#tab/azure-cli) |
| 151 | + |
| 152 | +To delete a public IP prefix, use [az network public-ip prefix delete](/cli/azure/network/public-ip/prefix#az-network-public-ip-prefix-delete). |
| 153 | + |
| 154 | +```azurecli |
| 155 | + az network public-ip prefix delete \ |
| 156 | + --resource-group <resource_group_name> |
| 157 | + --name <public_ip_prefix_name> \ |
| 158 | +``` |
| 159 | + |
| 160 | +# [Azure PowerShell](#tab/azure-powershell) |
| 161 | + |
| 162 | +To delete a public IP prefix, use [Remove-AzPublicIpPrefix](/powershell/module/az.network/remove-azpublicipprefix). |
| 163 | + |
| 164 | +```azurepowershell |
| 165 | +Remove-AzPublicIpPrefix -ResourceGroupName <resource_group_name> ` |
| 166 | + -Name <public_ip_prefix_name> |
| 167 | +``` |
| 168 | + |
| 169 | +>[!NOTE] |
| 170 | +>If addresses within the prefix are associated with public IP address resources, delete the public IP address resources first. For more information about deleting a public IP address, see [delete a public IP address](virtual-network-public-ip-address.md#view-modify-settings-for-or-delete-a-public-ip-address). |
| 171 | +
|
| 172 | +--- |
| 173 | + |
| 174 | +## Clean up resources |
| 175 | + |
| 176 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 177 | + |
| 178 | +## Troubleshoot Terraform on Azure |
| 179 | + |
| 180 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot) |
| 181 | + |
| 182 | +## Next steps |
| 183 | + |
| 184 | +> [!div class="nextstepaction"] |
| 185 | +> [Create a public IP using Terraform](create-public-ip-terraform.md) |
0 commit comments