Use variable in S3 backend configuration

I am using Terraform snowflake plugins. I want to use ${terraform.workspace} variable in terraform scope.

terraform {
  required_providers {
    snowflake = {
      source  = "chanzuckerberg/snowflake"
      version = "0.20.0"
    }
  }
  backend "s3" {
    bucket         = "data-pf-terraform-backend-${terraform.workspace}"
    key            = "backend/singlife/landing"
    region         = "ap-southeast-1"
    dynamodb_table = "data-pf-snowflake-terraform-state-lock-${terraform.workspace}"
  }
}

But I got this error. Variables are not available in this scope?

Error: Variables not allowed

  on provider.tf line 9, in terraform:
   9:     bucket         = "data-pf-terraform-backend-${terraform.workspace}"

Variables may not be used here.


Error: Variables not allowed

  on provider.tf line 12, in terraform:
  12:     dynamodb_table = "data-pf-snowflake-terraform-state-lock-${terraform.workspace}"

Variables may not be used here.

I also posted the same question to stackoverflow.

1 Like

Iā€™m also facing similar kind of issue, no fix found as of today.

As you see variables are not allowed within a backend block.

One feature that might be useful is shown here: Backend Configuration - Configuration Language - Terraform by HashiCorp which allows you to pass in a -backend-config parameter to terraform init which allows you to override parameters such as the bucket with a value in another file.

I came up with a relatively simple workaround. In my repo, I renamed the state.tf file to state.tf.template.

terraform {
  backend "s3" {
    bucket = "your-statefile-bucket-ENV"
    key = "sharestatefile/terraform.tfstate"
    region = "your-region"
    dynamodb_table = "your-statefile-table"
    encrypt = true
  }
}

In the build environment, I added an environment variable called ENV (values: develop, staging, production, etc).

In our CI/CD build script, I added:

- echo "Generating state.tf for ${ENV}"
- sed s/ENV/${ENV}/ < state.tf.template > state.tf
- cat state.tf

The end result is a state.tf file generated by the build process with a dynamically assembled S3 bucket name.

2 Likes