【问题标题】:Unable to fetch Subnet ID's | Terraform Plan无法获取子网 ID |地形规划
【发布时间】:2019-07-05 23:56:47
【问题描述】:

我在执行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.

【问题讨论】:

    标签: amazon-web-services terraform terraform-provider-aws


    【解决方案1】:

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

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

    【讨论】:

    • 这仍然是个问题
    【解决方案2】:

    aws_subnet data source 返回一个单个子网,而不是一个列表,而且您似乎没有用count 循环它。

    如果您希望返回多个子网,那么您可能需要aws_subnet_ids data source

    因此,如果您想在每个子网中创建一个与子网过滤器匹配的 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 挂载目标。

    【讨论】:

    • 我遇到了同样的错误。 Error: Invalid index on ../../modules/efs.tf line 18, in resource "aws_efs_mount_target" "master_mt":18: subnet_id = "${data.aws_subnet_ids.app_subnet.ids[count.index]}" |---------------- | count.index is 1 | data.aws_subnet_ids.app_subnet.ids is set of string with 2 elements.This value does not have any indices.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2023-04-03
    • 1970-01-01
    • 2012-06-28
    • 2020-11-12
    • 1970-01-01
    相关资源
    最近更新 更多