Home > Enterprise >  Terraform : can I have 2 local block with same key but with different value
Terraform : can I have 2 local block with same key but with different value

Time:10-19

I have following local block, observe the domain key

locals {
  organization = "xxxx"
  domain       = "cs"
  env = {
    prod = "prod"
    stg  = "stg"
    dev  = "dev"
  }
}

locals {
  s3_artifact_bucket_name = {
    prod = join("-", [
      local.s3_artifact_bucket_name_prefix,
      local.env["prod"]
    ])
    stg = join("-", [
      local.s3_artifact_bucket_name_prefix,
      local.env["stg"]
    ])
    dev = join("-", [
      local.s3_artifact_bucket_name_prefix,
      local.env["dev"]
    ])
  }
  s3_artifact_bucket_name_prefix = join("-", [
    local.organization,
    local.domain,
    local.s3_bucket_awsresource,
    local.s3_artifact_bucket_purpose
  ])
  s3_bucket_awsresource = join("-", [
    "bucket",
    var.cd_account_id
  ])
  s3_artifact_bucket_purpose = "artifacts-iac"
}

local.domain ( cs) is being used to create some another local name.

now I want to add another local block with same domain but different value ( as there are 3 values for domain ) and create another local name for s3 bucket with common as value inside it.

locals {
  # placeholder for access logs bucket name
  domain = "common"
  s3_bucket_awsresource = join("-", [
    "bucket",
    var.cd_account_id
  ])
  s3_bucket_purpose         = "s3-access-logs"
  access_logs_bucket_region = "us-east-1"
}

here in the next step I want to reference local.domain as common and not cs

locals {
  s3_artifact_access_logs_bucket_name = join("-", [local.organization, local.domain, s3_bucket_awsresource, local.s3_bucket_purpose, local.access_logs_bucket_region])
}

I am afraid local.domain will point out to which domain key and value? is there a way to use domain as key but with different value.

CodePudding user response:

No, you cannot have a duplicate local in the same workspace.

Consider making a map instead (as you have done with env) or perhaps a list containing the three values, then using another control variable to select which value to use.

CodePudding user response:

It won’t work. Instead, you can define domain as a map, just like env, and create three different keys with three different values.

CodePudding user response:

Thank to @Ben and @Calin, I was able to solve it,

Posting it for better understanding, using a map for the same key solved my problem

locals {
  organization = "xxx"
  domain = {
    customer_service = "cs"
    common           = "common"
  }
  env = {
    prod = "prod"
    stg  = "stg"
    dev  = "dev"
  }
}





locals {
  s3_artifact_access_logs_bucket_name = join("-", [
    local.organization,
    local.domain["common"],
    local.s3_bucket_awsresource,
    local.s3_access_logs_bucket_suffix
  ])

  s3_access_logs_bucket_suffix = join("-", [
    local.s3_acess_logs_bucket_purpose,
    local.s3_access_logs_bucket_region
  ])
  s3_acess_logs_bucket_purpose = "s3-access-logs"
  s3_access_logs_bucket_region = "us-east-1"
}
  • Related