Interpolation issue

Hi,
can some one assist me by fixing below error for me please?

Warning: Interpolation-only expressions are deprecated

  on locals.tf line 20, in locals:
  20:    environmentvars = "${contains(keys(local.env), terraform.workspace) ? terraform.workspace : "default"}"

I want to make use of Handling environmental variables in Terraform Workspaces | by Miles Collier | Medium

thanks
Sj

Hi @27sumit,

What you’ve shown here is a warning rather than an error, so you should still be able to work with this configuration even without fixing it, but it is warning that this particular expression is using a legacy style that was from an earlier version of Terraform which we no longer recommend.

One way you might be able to address this pretty easily is to run the command terraform fmt, which will update your configuration to be formatted in the recommended style. Along with some simple layout changes, that command is also able to repair legacy expression formats in many (but not all) cases, and so it might make this warning and others like it go away automatically.

If terraform fmt doesn’t automatically fix it then you could fix it manually by rewriting this particular line as follows:

   environmentvars = contains(keys(local.env), terraform.workspace) ? terraform.workspace : "default"
1 Like

hey thanks,
so for eg. if I use what you recommended , does below look like correct tf file?
######################################################

locals {
   env = {
      default = {}
      dev = {
         api_key        = "NRAK-XXXX"
         acc_id         = 266XXX
      }      
      nonprod = {
         api_key        = "NRAK-YYYY"
         acc_id         = 266XXX
      }      
      prod = {
        api_key        = "NRAK-ZZZZ"
        acc_id         = 266XXX
      }
   }
 
  environmentvars = contains(keys(local.env), terraform.workspace) ? terraform.workspace : "default"

}

######################################################

now how do I pass api_key in my main.tf
for e.g i am working on dev workspace, what should be my api key like

provider "newrelic" {
  region     = "US"
  api_key    = ??
  account_id = ??
}

thanks
Sumit

Hi @27sumit,

Assuming that api_key here is an authentication credential, I’d typically recommend passing that directly to the provider by setting the NEW_RELIC_API_KEY environment variable rather than including it in your configuration, because this value will presumably vary depending on who or what is running terraform apply over time.

With that said, you can refer to your environmentvars local value using an expression like local.environmentvars. For example:

provider "newrelic" {
  region     = "US"
  account_id = local.env[local.environmentvars].acc_id
}

If you want to keep your API keys in configuration rather than in the environment variable as I recommended then you can use a similar pattern to achieve that.

1 Like

Hey mate,
you have been absolutely brilliant. thanks for your help.
however following is what i did and it worked too.

I created a aws-secrets.tf file as below

data "aws_secretsmanager_secret_version" "api" {
  # Fill in the name you gave to your secret
  secret_id = "nr_env_api"
}

locals {
  api_key = {
    dev = jsondecode(data.aws_secretsmanager_secret_version.api.secret_string).nr_ops_dev
    nonprod = jsondecode(data.aws_secretsmanager_secret_version.api.secret_string).nr_ops_nonprod
    prod = jsondecode(data.aws_secretsmanager_secret_version.api.secret_string).nr_ops_prod
  }
}

and then I passed the api key values as below

provider "newrelic" {
  region     = "US"
  api_key    = local.api_key[terraform.workspace]
}

thanks