繁体   English   中英

无法获取子网ID。 地形图

[英]Unable to fetch Subnet ID's | Terraform Plan

我在执行Terraform plan时遇到错误。 我在这里遵循模块的概念。

以下是我的资源aws_efs_mount_targetaws_subnet块。

efs.tf:-

resource "aws_efs_file_system" "master_efs" {
  creation_token   = "master"
  performance_mode = "generalPurpose"
  kms_key_id       = "${data.terraform_remote_state.kms_ebs.outputs.key_arn}"
  encrypted        = "true"
  tags = {
    Name        = "master"
    Environment = "${var.environment}"
    Terraform   = "true"
  }
}
resource "aws_efs_mount_target" "master_mt" {
  file_system_id  = "${aws_efs_file_system.master_efs.id}"
  count           = "${length(var.availability_zones)}"
  subnet_id      = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
  security_groups = [ "${aws_security_group.sg.id}" ]
}

data "aws_subnet" "app_subnet_0" {
  vpc_id = "${data.aws_vpc.cng.id}"
  filter {
    name    = "tag:Name"
    values  = ["${var.search_pattern_app}-0"]
  }
}

错误:索引无效

     on ../../modules/efs.tf line 16, in resource "aws_efs_mount_target" "master_mt":
      16:   subnet_id      = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
        |----------------
        | count.index is 2
        | data.aws_subnet.app_subnet_0 is object with 15 attributes

    The given key does not identify an element in this collection value.


Error: Invalid index



     on ../../modules/efs.tf line 16, in resource "aws_efs_mount_target" "master_mt":
      16:   subnet_id      = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
        |----------------
        | count.index is 1
        | data.aws_subnet.app_subnet_0 is object with 15 attributes

    The given key does not identify an element in this collection value.

Error: Invalid index

  on ../../modules/efs.tf line 35, in resource "aws_efs_mount_target" "worker_mt":
  35:   subnet_id      = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
    |----------------
    | count.index is 1
    | data.aws_subnet.app_subnet_0 is object with 15 attributes

The given key does not identify an element in this collection value.


Error: Invalid index

  on ../../modules/efs.tf line 35, in resource "aws_efs_mount_target" "worker_mt":
  35:   subnet_id      = "${data.aws_subnet.app_subnet_0.*.id[count.index]}"
    |----------------
    | count.index is 2
    | data.aws_subnet.app_subnet_0 is object with 15 attributes

The given key does not identify an element in this collection value.

据我从您的代码中了解到的,您正在尝试为VPC中的每个子网(每个可用区一个)为EFS创建一个挂载点,并且要这样做,您希望Terraform自动解析可用区,然后为安装目标分配子网ID? 显然,如果您只想将aws_subnet.app_subnet_0.1添加到aws_subnet.app_subnet_0.x,则您编写的代码试图将子网属性之一与计数器的值相匹配。也许您可以这样说

aws_subnet.app_subnet_0.${count.index}.id

aws_subnet数据源返回单个子网,而不是列表,并且您似乎没有在count循环。

如果您希望返回多个子网,则可能需要aws_subnet_ids数据源

因此,如果要在与子网过滤器匹配的每个子网中创建EFS挂载目标,则可以使用以下命令:

resource "aws_efs_file_system" "master_efs" {
  creation_token   = "master"
  performance_mode = "generalPurpose"
  kms_key_id       = "${data.terraform_remote_state.kms_ebs.outputs.key_arn}"
  encrypted        = "true"

  tags = {
    Name        = "master"
    Environment = "${var.environment}"
    Terraform   = "true"
  }
}

data "aws_subnet_ids" "app_subnet" {
  vpc_id = "${data.aws_vpc.cng.id}"

  tags = {
    Name = "${var.search_pattern_app}"
  }
}

resource "aws_efs_mount_target" "master_mt" {
  file_system_id  = "${aws_efs_file_system.master_efs.id}"
  count           = "${length(var.availability_zones)}"
  subnet_id       = "${data.aws_subnet.app_subnet.ids[count.index]}"
  security_groups = ["${aws_security_group.sg.id}"]
}

这将找到名称与search_pattern_app变量匹配的所有子网,并在每个子网中创建EFS安装目标。

暂无
暂无

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

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