你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Quickstart: Create a public IP address using Terraform

In this quickstart, you learn how to create an Azure public IP address. Public IP addresses in Azure are used for public connections to Azure resources. Public IP addresses are available in two SKUs: basic, and standard. Two tiers of public IP addresses are available: regional, and global. The routing preference of a public IP address is set when created. Internet routing and Microsoft Network routing are the available choices.

Diagram of an example use of a public IP address. A public IP address is assigned to a load balancer.

If you don't have an Azure subscription, create an Azure free account 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 named myStandardPublicIP
  • Create a basic static public IPv4 address named myBasicPublicIP
  • Create a standard zonal public IPv4 address in Zone 2 named myZonalStandardPublicIP
  • Create a non-zonal IP address named myNonZonalStandardPublicIP
  • Create a standard static public IPv4 address named myRoutingPreferenceStandardPublicIP that supports the Routing Preference feature
  • Create a standard static public IPv4 address named myGlobalTierStandardPublicIP that supports the Global Tier feature

备注

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 resource group

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

Terraform
# 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

Create public IP

备注

Standard SKU public IP is recommended for production workloads. For more information about SKUs, see Public IP addresses.

The following command snippet works for API version 2020-08-01 or later. For more information about the API version currently being used, see Resource Providers and Types.

Terraform
}

# Create a public IP: IPv4 Standard SKU
resource "azurerm_public_ip" "myStandardPublicIP" {
  name                = "myStandardPublicIP"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  allocation_method   = "Static"
  sku                 = "Standard"

重要

For versions of the API older than 2020-08-01, omit the zone field to create a zone-redundant IP address.

Create a zonal or no-zone IP address

In this section, you learn how to create a zonal and a non-zone public IP address.

The following code snippet creates a standard zonal public IPv4 address in Zone 2 named myZonalStandardPublicIP.

To create an IPv6 address, set the ip_version value to IPv6.

Terraform
  allocation_method   = "Static"
  sku                 = "Basic"

  ip_version          = "IPv4"
}

# Create a public IP: IPv4 Zonal
resource "azurerm_public_ip" "myZonalStandardPublicIP" {
  name                = "myZonalStandardPublicIP"

备注

For more information about availability zones, see What are availability zones?.

Routing Preference and Tier

Standard SKU static public IPv4 addresses support Routing Preference or the Global Tier feature.

By default, the routing preference for public IP addresses is set to Microsoft network, which delivers traffic over Microsoft's global wide area network to the user.

The selection of Internet minimizes travel on Microsoft's network, instead using the transit ISP network to deliver traffic at a cost-optimized rate.

For more information on routing preference, see What is routing preference (preview)?.

The following code snippet creates a new standard zone-redundant public IPv4 address with a routing preference of type Internet:

Terraform
resource "azurerm_public_ip" "myNonZonalStandardPublicIP" {
  name                = "myNonZonalStandardPublicIP"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  allocation_method   = "Static"
  sku                 = "Standard"

  ip_version          = "IPv4"
}

# Create a public IP: IPv4 with Routing Preference
resource "azurerm_public_ip" "myRoutingPreferenceStandardPublicIP" {
  name                = "myRoutingPreferenceStandardPublicIP"
  resource_group_name = azurerm_resource_group.example.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.
  2. 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