2

I am trying to configure a Terraform enterprise workspace in Jenkins on the fly. To do this, I need to be able to set the remote backend workspace name in my main.tf dynamically. Like this:

# Using a single workspace:
terraform {
  backend "remote" {
    hostname = "app.xxx.xxx.com"
    organization = "YYYY"


    # new workspace variable
    workspaces {
      name = "${var.workspace_name}"
    }
  }
}

Now when I run:

    terraform init -backend-config="workspace_name=testtest"

I get:

Error loading backend config: 1 error(s) occurred:

* terraform.backend: configuration cannot contain interpolations

The backend configuration is loaded by Terraform extremely early, before
the core of Terraform can be initialized. This is necessary because the backend
dictates the behavior of that core. The core is what handles interpolation
processing. Because of this, interpolations cannot be used in backend
configuration.

If you'd like to parameterize backend configuration, we recommend using
partial configuration with the "-backend-config" flag to "terraform init".

Is what I want to do possible with terraform?

2 Answers2

4

You cann't put any variables "${var.workspace_name}" or interpolations into the Backend Remote State Store. However, you can create a file beside with your Backend values, it could look like this into the main.tf file:

# Terraform backend State-Sotre
terraform {
  backend "s3" {}
}

and into a dev.backend.tfvars for instance:

bucket         = "BUCKET_NAME"

encrypt        = true

key            = "BUCKET_KEY"

dynamodb_table = "DYNAMODB_NAME"

region         = "AWS_REGION"

role_arn       = "IAM_ROLE_ARN"

You can use partial configuration for s3 Backend as well. Hope it'll help.

  • This answer is for a S3 backend? I am trying to do a remote backend connected to terraform enterprise. – Nicolas Le Gorrec Apr 03 '19 at 22:40
  • With this detail `backend "s3" {}` yes it is. I don't really know Terraform Enterprise but I guess it's the same functioning as the open-source version. With the GCP provider it will be `terraform { backend "gcs" {} }` for instance – Claire Bellivier Apr 04 '19 at 02:48
  • I actually need the backend to be remote terraform enterprise. I feel like there has to be a way to create a workspace with a dynamic name on TFE with this setup. One option would be to do API calls but this remote backend setup is so clean and easy to understand. Plus it doesnt duplicate workspaces and all that. Would love to be able to do it this way. – Nicolas Le Gorrec Apr 04 '19 at 16:38
  • Thank you - this is the first place I found a proper example of TF partial configuration using a file rather than CLI variables. – Guy Rawsthorn May 19 '21 at 10:13
0

Hey I found the correct way to do this:

While the syntax is a little tricky, the remote backend supports partial backend initialization. What this means is that the configuration can contain a backend block like this:

terraform {
  backend "remote" { }
}

And then Terraform can be initialized with a dynamically set backend configuration like this (replacing ORG and WORKSPACE with appropriate values):

terraform init -backend-config "organization=ORG" -backend-config 'workspaces=[{name="WORKSPACE"}]'