2

我正在尝试从地图中引用列表中的值,但似乎无法让 terraform 识别出它是一个字符串。

下面是我正在处理的模块以及定义的变量。

resource "aws_transfer_user" "aws_transfer_users" {

  for_each = var.transfer_users_and_keys

  server_id = aws_transfer_server.aws_transfer_service.id
  user_name = each.key
  role = aws_iam_role.aws_transfer_role.arn

  home_directory = format("/%s/%s",var.transfer_users_and_keys[each.value[1]],var.transfer_users_and_keys[each.key])

  tags = {
    Name = each.key
    Project = var.product_name
    Terraform = true
  }
}
variable "transfer_users_and_keys" {
  type = map(list(string))

}

出于某种原因,当我从列表中调用值时,它给了我以下错误:

on main.tf line 38, in resource "aws_transfer_user" "aws_transfer_users":
  38:   home_directory = format("/%s/%s",var.transfer_users_and_keys[each.value[1]],var.tran
sfer_users_and_keys[each.key])
    |----------------
    | each.value[1] is "bucket-dev-client"
    | var.transfer_users_and_keys is map of list of string with 2 elements 

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

这是我正在创建的变量:

transfer_users_and_keys = {
    format("key-%s",local.environment) = ["value.pub",tostring(local.sftp_bucket[0])]
    format("key-%s02",local.environment) = ["value02.pub",local.sftp_bucket]
}
sftp_bucket = [format("bucket-%s-client",local.environment)]

这里的目标是根据“transfer_users_and_keys”变量 (tostring(local.sftp_bucket[0])) 中的第二个值构建 home_directory。

4

1 回答 1

2

使用时for_each,您不需要继续引用变量并对其进行索引。改变:

home_directory = format("/%s/%s",var.transfer_users_and_keys[each.value[1]],var.transfer_users_and_keys[each.key])

简单地

home_directory = format("/%s/%s", each.value[1], each.key)
于 2020-06-09T20:37:08.480 回答