2

I'm trying to reference a value within a list from a map but can't seem to get terraform to recognize that its a string.

Below is my module that I'm working on along with the variable defined.

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

}

For some reason when I call the value from the list it gives me the following error:

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.

Here is my variable that I'm creating:

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)]

The goal here is to build out the home_directory based on the 2nd value in the "transfer_users_and_keys" variable (tostring(local.sftp_bucket[0])).

TravelingLex
  • 399
  • 1
  • 3
  • 16

1 Answers1

2

When using for_each, you don't need to keep referencing the variable and indexing it. Change:

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

to simply

home_directory = format("/%s/%s", each.value[1], each.key)
Ben Whaley
  • 32,811
  • 7
  • 87
  • 85