Azure 上的 Terraform - 部署多个子网 [英] Terraform on Azure - Deploy multiple subnet

查看:15
本文介绍了Azure 上的 Terraform - 部署多个子网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个 Terraform 脚本来创建多个子网.

I'm trying to implement a Terraform script to create multiple subnets.

resource "azurerm_subnet" "test_subnet" {
    name = "testSUBNET"
    resource_group_name = "${local.resource_group_name}"
    virtual_network_name = "${azurerm_virtual_network.lab_vnet.name}"
    address_prefix = "10.0.1.0/24"
}

有没有办法对变量执行 for-each 或循环以同时创建它们?

Is there a way to do a for-each or a loop on a variable in order to create them at the same time?

最好的问候!

推荐答案

您可以使用变量和计数索引来实现这一点,如下所示:

You can achieve this using a variable and count index as follows:

variable "subnet_prefix" {
  type = "list"
  default = [
    {
      ip      = "10.0.1.0/24"
      name     = "subnet-1"
    },
    {
      ip      = "10.0.2.0/24"
      name     = "subnet-2"
    }
   ]
}

resource "azurerm_subnet" "test_subnet" {
    name = "${lookup(element(var.subnet_prefix, count.index), "name")}"
    count = "${length(var.subnet_prefix)}"
    resource_group_name = "${local.resource_group_name}"
    virtual_network_name = "${azurerm_virtual_network.lab_vnet.name}"
    address_prefix = "${lookup(element(var.subnet_prefix, count.index), "ip")}"
}

还有可用的预览功能 for-each 在新版本中

There is also preview feature available for-each in the new version

这篇关于Azure 上的 Terraform - 部署多个子网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆