Terraform 在同一资源中有 for_each 和动态块?或者我可以在资源语句中有多个 for_each

时间:2021-03-02 19:43:21

标签: amazon-web-services terraform

我可以在同一个资源语句中使用 for_each 和动态块吗? 或者我可以在资源语句中有多个 for_each /// 创建 2 个 EC2 实例 ///

resource "aws_instance" "ABC" {
  count = 2
  ami           = var.AMIX
  instance_type = "t3.small"
  subnet_id = xxx
  vpc_security_group_ids = [data.aws_security_group.SG.id]
  key_name = var.AMIX-KEY
  tags = {
    Name = abc)
  }
}

/// 使用子网名称和子网 ID 创建本地字典 /// 这对于 ec2 实例 1 有 2 个子网,对于 ec2 实例 2 有 2 个子网

locals {
# With List 
  subnets = flatten([
    for subnet_details in aws_subnet.SUBNET : {
      subnet_name = subnet_details.tags.Name,
      subnet_id = subnet_details.id
      } if contains(["abc", "xyz"], subnet_details.tags.Name)
  ])
}

//// 本地子网输出。

 dev = [
      + {
          + subnet_id    = "A"
          + subnet_name  = "DEV1"
        },
      + {
          + subnet_id    = "B"
          + subnet_name  = "DEV1"
        },
      + {
          + subnet_id    = "C"
          + subnet_name  = "DEV2"
        },
      + {
          + subnet_id    = "D"
          + subnet_name  = "DEV2"
        },
    ]

/// 如何使用网络接口附加 EC2 实例??? 在附件中循环两次,因为我需要根据子网名称绑定两个 ec2 实例 2 个子网获取 ec2-1 和 2 个子网获取 ec2-2 并基于此它们将获取 device_index ///

resource "aws_network_interface" "NICS" {
  for_each = {
    for subnet_id, subnet_name in local.subnets : subnet_id => subnet_name
  }
  subnet_id = each.value.subnet_id
  security_groups = [data.aws_security_group.SG.id]
  tags = {
    Name = each.value.subnet_name
  }
  attachment {
  instance = ?
  device_index = 1
  } 
}

1 个答案:

答案 0 :(得分:0)

您的代码的以下版本怎么样。我还没有运行代码,但你可以考虑一下它背后的想法:

variable "instance_name" {
  default = ["Y", "Z"]
}

resource "aws_instance" "ABC" {

  for_each = var.instance_name

  ami           = var.AMIX
  instance_type = "t3.small"
  subnet_id = xxx
  vpc_security_group_ids = [data.aws_security_group.SG.id]
  key_name = var.AMIX-KEY

  tags = {
    Name = each.key
  }
}

然后,您将根据 local.subnets 创建四个 NIC,当您 还指定实例名称。此外,我会使用 aws_network_interface_attachment:

将 NIC 的创建与其附件分开
locals {

  subnets = [
       {
           subnet_id    = "A"
           subnet_name  = "DEV1"
           instance_name = "Y"
        },
       {
           subnet_id    = "B"
           subnet_name  = "DEV1"
           instance_name = "Y"           
        },
       {
           subnet_id    = "C"
           subnet_name  = "DEV2"
           instance_name = "Z"                      
        },
       {
           subnet_id    = "D"
           subnet_name  = "DEV2"
           instance_name = "Z"                                 
        }
    ]
}


resource "aws_network_interface" "NICS" {
  
  for_each = {for subnet in local.subnets : (subnet.subnet_name) => subnet}

  subnet_id       = each.value.subnet_id
  security_groups = [data.aws_security_group.SG.id]

  tags = {
    Name = each.value.subnet_name
  }
}

resource "aws_network_interface_attachment" "test" {

  for_each = {for subnet in local.subnets : (subnet.subnet_name) => subnet}

  instance_id          = aws_instance.ABC[each.value.instance_name].id
  network_interface_id = aws_network_interface.NICS[each.key].id
  device_index         = 0
}