Search code examples
terraformterraform-provider-awsterraform-enterprise

Terraform partial remote backend cannot contain interpolations?


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?


Solution

  • 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.