简体   繁体   English

无法获取子网ID。 地形图

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

I am getting below error while executing Terraform plan . 我在执行Terraform plan时遇到错误。 I am following modules concept here. 我在这里遵循模块的概念。

Below is my resource aws_efs_mount_target and aws_subnet blocks. 以下是我的资源aws_efs_mount_targetaws_subnet块。

efs.tf:- 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"]
  }
}

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 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.

From what I'm understanding from your code you're are trying to create a mounting point for your EFS for each subnet in VPC (one per availability zone), and to do that you want to Terraform to automatically parse the availability zone and then assign the subnet id for the mount target ? 据我从您的代码中了解到的,您正在尝试为VPC中的每个子网(每个可用区一个)为EFS创建一个挂载点,并且要这样做,您希望Terraform自动解析可用区,然后为安装目标分配子网ID? Apparently the code you wrote is trying to match one of the subnet attribute with the value of the counter, if you just want to add aws_subnet.app_subnet_0.1 to aws_subnet.app_subnet_0.x maybe you can put it that way 显然,如果您只想将aws_subnet.app_subnet_0.1添加到aws_subnet.app_subnet_0.x,则您编写的代码试图将子网属性之一与计数器的值相匹配。也许您可以这样说

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

The aws_subnet data source returns a single subnet, not a list and you don't appear to be looping over it with count . aws_subnet数据源返回单个子网,而不是列表,并且您似乎没有在count循环。

If you're expecting multiple subnets to be returned then you probably want the aws_subnet_ids data source instead. 如果您希望返回多个子网,则可能需要aws_subnet_ids数据源

So if you want to create an EFS mount target in each subnet that matches a subnet filter you could use the following: 因此,如果要在与子网过滤器匹配的每个子网中创建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}"]
}

This finds all the subnets with a name matching the search_pattern_app variable and creates EFS mount targets in each of them. 这将找到名称与search_pattern_app变量匹配的所有子网,并在每个子网中创建EFS安装目标。

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

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