terraform {
required_version = ">= 0.13"
required_providers {
google = {
source = "hashicorp/google
version = "~> 4.9.0"
}
}
backend = "gcs" {
bucket = "my-bucket"
prefix = "/tfstate/{_NONLIVE_OR_LIVE}/{country}"
}
}
I try to parametrize "terraform init" so that I can use the same main.tf file for six different use cases (_PREPROD_OR_PROD can be either pre-prod or prod; country can be either Portugal, Italy or Spain).
In the cloudbuild.yaml file I have to the same terraform_apply.yaml file for all six usecases and I want to specify it like "terraform init -backend-config="PREPROD_OR_PROD=${_PREPROD_OR_PROD}" -backend-config="{COUNTRY=${_COUNTRY}"
How can I implement this in terraform and Google Cloud/ GCP?
You're probably not using the
-backend-configproperly, see Partial configuration for more details.You should use with with this format:
Where:
KEYis the configuration option to set (likeprefixorbucket)VALUEis the value to set it to (like/tfstate/{_NONLIVE_OR_LIVE}/{country}).Consider the following backend:
I can change the
pathusing the following command:This will generate a
prod.tfstatein the local folder.Now, back to your code:
I haven't tested this but I suppose you can do something like:
As an alternative, you can use terragrunt to automatically generate the remote backend for each environment.