执行Terraform plan
时出现错误。我在这里遵循模块的概念。
下面是我的资源aws_efs_mount_target
和aws_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.
答案 0 :(得分:1)
根据我对您代码的了解,您正在尝试为VPC中的每个子网(每个可用区一个)为EFS创建一个挂载点,并且要这样做,您希望Terraform自动解析可用性区域,然后为安装目标分配子网ID? 显然,如果您只想将aws_subnet.app_subnet_0.1添加到aws_subnet.app_subnet_0.x,则您编写的代码试图将子网属性之一与计数器的值进行匹配
aws_subnet.app_subnet_0.${count.index}.id
答案 1 :(得分:1)
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挂载目标。