How do I add a variable to an s3endpoint

I want to be able to add a variable to the s3 endpoint so that it is created based on an instancename or applicationset

this works but is fixed
key = “global/s3/instance/terraform.tfstate”
this does not
key = “global/s3/“var.instancename”/terraform.tfstate”

Can I achieve this


If you want to add interpolation into your syntax with terraform 0.12 you still need to use the ${} interpolation syntax to append a variable output to a string.

Consider the following example. Notice that the local base can uses simple assignment of a string. However; when I want to include the output of a variable in a string I need to use the ${} syntax around my variable to tell Terraform to return the output not the string literal. This way I can just embed variables inside strings. Concatenation like your example “global/s3/“var.instancename”/terraform.tfstate” is not supported.

variable "instance" {
  default = "something"

locals {
  base         = "global/s3/instance/terraform.tfstate"
  interpolated = "global/s3/${var.instance}/terraform.tfstate"

output "local_base" {
  value = local.base

output "local_interpolated" {
  value = local.interpolated

This config would produce the following output:

➜ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.


local_base = global/s3/instance/terraform.tfstate
local_interpolated = global/s3/something/terraform.tfstate

Hope this helps,


unfortunately the terraform backend { does not allow variables

Error: Variables not allowed

on line 14, in terraform:
14: key = local.interpolated

code below:

locals {
base = “global/s3/instance/terraform.tfstate”
interpolated = “global/s3/${var.instancename}/terraform.tfstate”


terraform {
backend “s3” {
# Replace this with your bucket name!
# need to strip white spaces
bucket = “xxxx”
key = local.interpolated
region = “eu-west-2”

Aha ok, I should have asked where you were trying to do the interpolation.

As you said you can not use interpolation inside of backend config, this is due to the load order of the configuration.

You can however use partial configurations.

Using this approach you could pass the value of bucket in the terraform init command.

terraform init \

Thanks I will use that method going forward.