0

我正在尝试创建从映射用户到 SSH 密钥的传输密钥

content_users = {
  "master" = [
    "ssh-rsa ...",
    "ssh-rsa ...",
    "ssh-rsa ...",
  ]
  "test" = [
    "ssh-rsa ...",
    "ssh-rsa ...",
  ]
}

aws_transfer_user部分很简单

resource "aws_transfer_user" "content" {
  for_each  = var.content_users
  server_id = aws_transfer_server.content.id
  user_name = each.key
  role      = aws_iam_role.transfer.arn
}

但我试图弄清楚如何做aws_transfer_key只接受一个 ssh 密钥

resource "aws_transfer_ssh_key" "content" {
  for_each = var.content_users
 server_id = aws_transfer_server.content.id
  user_name = each.key
  body      = "... SSH key ..."
}

我认为这是我必须遵循的https://www.terraform.io/docs/configuration/functions/flatten.html#flattening-nested-structures-for-for_each

4

1 回答 1

0
resource "aws_transfer_ssh_key" "content" {
  for_each = toset(flatten([
    for user, keys in var.content_users : [
      for key in keys : "${user} ${key}"
    ]
  ]))
  server_id = aws_transfer_server.content.id
  user_name = regex("(\\S+)\\s(.+)", each.value)[0]
  body      = regex("(\\S+)\\s(.+)", each.value)[1]
}
于 2020-12-14T17:59:52.910 回答