简体   繁体   English

使用 terraform 在 Azure 中为每个可用区创建子网

[英]Creating subnet per availability zones in Azure using terraform

I am trying to create subnets per availability zones in Azure using terraform.我正在尝试使用 terraform 在 Azure 中为每个可用区创建子网。 I am using the code below to create a subnet.我正在使用下面的代码来创建子网。

resource "azurerm_subnet" "public_subnet" {
  name                 = "public_subnet"
  virtual_network_name = azurerm_virtual_network.vnet.name
  resource_group_name  = azurerm_resource_group.terraform_rg.name
  address_prefix       = "10.20.10.0/24"
}

My requirement is possible in AWS.我的要求在 AWS 中是可能的。 Since I am new to Azure I am not sure whether it is possible to do the same in Azure.由于我是 Azure 的新手,我不确定在 Azure 中是否可以这样做。 It would be great if some one render their hands to help me.如果有人伸出手来帮助我,那就太好了。

Thanks in advance!提前致谢!

The azure subnet is not a Zonal service (where a resource is pinned to a specific zone), refer to Azure services and regions that support Availability Zones . azure 子网不是区域服务(资源固定到特定区域),请参阅支持可用区的Azure 服务和区域 So you need to create the specific support services per availability zone.因此,您需要为每个可用区创建特定的支持服务。

For example, you can create an Azure VM or Azure public IP per availability zone.例如,您可以为每个可用区创建一个 Azure VM 或 Azure 公共 IP。

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "east us"
}

resource "azurerm_public_ip" "example" {
  name                = "acceptanceTestPublicIp1"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  allocation_method   = "Static"
  sku = "Standard"
  zones = ["1"]

}

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.example.id
  }
}

resource "azurerm_linux_virtual_machine" "example" {
  name                = "example-machine"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  size                = "Standard_F2"
  zone = "1"
  admin_username      = "adminuser"
  network_interface_ids = [
    azurerm_network_interface.example.id,
  ]

 admin_ssh_key {
    username   = "adminuser"
    public_key = file("~/.ssh/id_rsa.pub")
  }

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "16.04-LTS"
    version   = "latest"
  }
}

If you're interested in the zone option, you can look at this open issue .如果您对zone选项感兴趣,可以查看这个未解决的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM