【发布时间】:2019-12-14 03:51:53
【问题描述】:
我有以下 Terraform 资源,旨在为每个仓库创建一个单独的加密 EFS 卷,然后在两个子网中为每个创建挂载目标:
resource "aws_efs_file_system" "efs-data-share" {
count = "${length(var.warehouses)}"
encrypted = "${var.encrypted}"
kms_key_id = "${element(data.aws_kms_key.encryption_key.*.arn,count.index)}"
performance_mode = "${var.performance_mode}"
tags {
Name = "${element(split(".",var.warehouses[count.index]),0)}-${var.name_suffix}"
Warehouse = "${element(split(".",var.warehouses[count.index]),0)}"
Environment = "${var.environment}"
Purpose = "${var.purpose}"
}
}
resource "aws_efs_mount_target" "mounts" {
count = "${length(var.subnets)}"
file_system_id = "${aws_efs_file_system.efs-data-share.*.id}"
subnet_id = "${element(var.subnets, count.index)}"
security_groups = ["${var.efs_security_groups}"]
}
data "aws_kms_key" "encryption_key" {
count = "${length(var.warehouses)}"
key_id = "alias/${element(split(".",var.warehouses[count.index]),0)}-${var.key_alias_suffix}"
}
EFS 本身启动正常,但挂载失败,因为 file_system_id 必须是单个资源,而不是列表。
* module.prod_multi_efs.aws_efs_mount_target.mounts[1]: file_system_id must be a single value, not a list
* module.prod_multi_efs.aws_efs_mount_target.mounts[0]: file_system_id must be a single value, not a list
我不确定在 Terraform 语法方面我有什么办法。在每种情况下我都需要两个挂载,所以我的计数是子网变量的长度,为此我传入了子网 123456 和子网 567890。如何让系统为每个 EFS 进行两次挂载(按仓库列表的长度计算总共七个),而不能在 aws_efs_mount_target 中使用第二个计数?
这是我调用模块的方式:
module "prod_multi_efs" {
source = "../../../../Modules/Datastore/MultiEFS"
efs_security_groups = ["${aws_security_group.prod-efs-group.id}"]
environment = "Prod"
name_suffix = "${module.static_variables.prod_efs_name}"
key_alias_suffix = "prod-efs-${module.static_variables.account_id}-us-east-1"
subnets = ["${module.prod_private_subnet_1.subnet_id}", "${module.prod_private_subnet_2.subnet_id}"]
warehouses = "${module.static_variables.warehouses}" #The list of seven warehouses
}
这是我的计划,尝试根据以下答案添加 [count.index] - 它执行两次安装,但仅安装到 7 个 FS ID 中的前 2 个:
+ module.stg_multi_efs.aws_efs_mount_target.mounts[0]
id: <computed>
dns_name: <computed>
file_system_arn: <computed>
file_system_id: "fs-123456"
ip_address: <computed>
network_interface_id: <computed>
security_groups.#: "1"
security_groups.4286231943: "sg-xxxxxxxxxx"
subnet_id: "subnet-aaaaaaaa"
+ module.stg_multi_efs.aws_efs_mount_target.mounts[1]
id: <computed>
dns_name: <computed>
file_system_arn: <computed>
file_system_id: "fs-567890"
ip_address: <computed>
network_interface_id: <computed>
security_groups.#: "1"
security_groups.4286231943: "sg-xxxxxxxxxxx"
subnet_id: "subnet-bbbbbbbbbb"
【问题讨论】:
标签: terraform terraform-provider-aws amazon-efs