Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

snowflake cloud data platform - Terraform: "Variables may not be used here" during terraform init

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.
question from:https://stackoverflow.com/questions/65838989/terraform-variables-may-not-be-used-here-during-terraform-init

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seems like a specific instance of a more common problem in Terraform: Concatenating variables.

Using locals to concatenate should fix it. See https://www.terraform.io/docs/configuration/locals.html

An example from https://stackoverflow.com/a/61506549/132438:

locals {
  BUCKET_NAME = [
    "bh.${var.TENANT_NAME}.o365.attachments",
    "bh.${var.TENANT_NAME}.o365.eml"
  ]
}

resource "aws_s3_bucket" "b" {
  bucket = "${element(local.BUCKET_NAME, 2)}"
  acl    = "private"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...