'Combine Each.Value with String text?

Working on an AWS SFTP solution with custom IDP. I have this s3 object block, which is intended to create a folder in s3:

resource "aws_s3_bucket_object" "home_directory" {
  for_each = var.idp_users
  bucket       = aws_s3_bucket.s3.id
  key          = each.value["HomeDirectory"]
}

And this map variable input for idp_users:

idp_users = {
  secret01 = {
    Password      = "password",
    HomeDirectory = "test-directory-1",
    Role          = "arn:aws:iam::XXXXXXXXXXXX:role/custom_idp_sftp_role",
  },
  secret02 = {
    Password      = "password",
    HomeDirectory = "test-directory-2",
    Role          = "arn:aws:iam::XXXXXXXXXXXX:role/custom_idp_sftp_role",
  }
}

What I need is to simply add a "/" to the end of the HomeDirectory value in the aws_s3_bucket_object block, which will create a folder with the specific name in the s3 bucket. I know it could just be typed into the variable, but in the spirit of automation I want Terraform to append it manually and save us the hassle. I've monkeyed around with join and concatenate but can't figure out how to simply add a "/" to the end of the HomeDirectory value in the s3 object block. Can anyone provide some insight?



Solution 1:[1]

You can do that using string templating:

resource "aws_s3_bucket_object" "home_directory" {
  for_each = var.idp_users
  bucket       = aws_s3_bucket.s3.id
  key          = "${each.value["HomeDirectory"]}/"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dan Monego