Search code examples
listobjectterraformattributes

How to access a attribute in terraform


I have a local object created as:

       first   = {
          sns  = "first-rzv3-sns-prod"
          s3   = "first-rzv3-s3-prod"
          sqs  = "first-rzv3-sqs-prod"
        }
      second   = {
          s3   = "second-rzv3-s3-prod"
          ec2  = "second-rzv3-ec2-prod"
        }

variable user = [first, second]

I'm trying to create a resource with its name based on the username. For example

resource "create_s3_bucket" "example" {
  count = length(user)
  name  = local.hashed_names_prod."${var.user[count.index]}".s3
}

name should be "first-rzv3-s3-prod" and "second-rzv3-s3-prod"

Please help


Solution

  • This resource can be simplified with the for_each meta-argument such that var.user becomes unnecessary and everything generally becomes cleaner:

    resource "create_s3_bucket" "example" {
      for_each = local.hashed_names_prod
      name     = each.value.s3
    }