多块Terraform for_each

时间:2019-12-19 12:54:56

标签: terraform

我正在尝试创建多个AWS NLB,我的变量nlb是具有2个条目的键值,我的问题出在aws_subnets_ids和aws_subnet上,因为aws_subnets_ids必须以“ for_each”运行,这样才能获取每个LB和返回适当的子网,但是aws_subnet不能使用相同的“ for_each”运行,因此其不对应。

variable "nlb" {
  default = {
    "frontend" = "public"
    "backend" = "private"
  }
}

resource "aws_lb" "nlb" {
  for_each            = var.nlb
  name                = "digital-${var.envName}-${each.key}"
  internal            = false
  load_balancer_type  = "network"
  subnets             = "${data.aws_subnet.example.*.id}"

  enable_deletion_protection = true

  tags = {
    Service           = "digital-${each.key}"
  }
}

resource "aws_lb_target_group" "instance-tg" {
  for_each            = var.nlb
  name                = "digital-${var.envName}-${each.key}"
  port                = "8080"
  protocol            = "TCP"
  vpc_id              = "${data.aws_vpc.selected.id}"
}

resource "aws_lb_listener" "listeners"{
  for_each            = var.nlb
  load_balancer_arn   = "${each.key}"
  port                = "443"
  protocol            = "TCP"
  certificate_arn     = "arn:aws:acm:us-east-1:321321321:certificate/78687asd78-9cf9-4d6e-b15f-28392839assd8s989"

  default_action {
    type              = "forward"
    target_group_arn  = "${aws_lb_target_group.instance-tg[each.key].arn}"
  }
}


data "aws_subnet_ids" "public" {
  for_each            = var.nlb
  vpc_id              = "${data.aws_vpc.selected.id}"
  tags = {
    Tier              = "${each.value}"
   }
}



data "aws_subnet" "example" {
  id                  = [for i in aws_subnet_ids.public: i.ids]
}

data "aws_vpc" "selected" {
  id = "vpc-ds78"
}

那是错误消息:

Error: Reference to undeclared resource

  on elb.tf line 54, in data "aws_subnet" "example":
  54:   id                  = [for i in aws_subnet_ids.public: i.id]

A managed resource "aws_subnet_ids" "public" has not been declared in the root
module.

0 个答案:

没有答案