How to achieve multiple gcs backends in terraform

Viewed 622

Within our team. We all have our own dev project, and then we have a test and prod environment.

We are currently in the process of migrating from deployment manager, and gcloud cli. Into terraform. however we havent been able to figure out a way to create isolated backends within gcs backend. We have noticed that the remote backends support setting a dedicated workspace but we havent been able to setup something similar within gcs.

Is it possible to state that terraform resource A, will have a configurable backend, that we can adjust per project, or is the equivalent possible with workspaces? So that we can use either tfvars, and vars parameters to switch between projects?

As stands everytime we attempt to make the backend configurable through vars, we get the error in terraform init of

Error: Variables not allowed

How does one go about creating isolated backends for each project.

Or if that isn't possible how can we guarantee that with multiple projects a shared backend state will not collide causing the state to be incorrect?

1 Answers

Your backend must been known when you run your terraform init command, I mean your backend bucket.

If you don't want to use workspace, you have to customize the backend value before running the init. We are use make to achieve this. According to the environment, make create a backend.tf file with the correct backend name. And run the init command.


EDIT 1

We have this piece of script (sh) which create the backend before triggering the terraform command. (it's our Make file that do this)

cat > $TF_export_dir/backend.tf << EOF
terraform {
    backend "gcs" {
        bucket = "$TF_subsidiary-$TF_environment-$TF_deployed_application_code-gcs-tfstatebackend"
        prefix = "terraform/state"
    }
}
EOF

Of course the bucket name pattern is dependent of our project. The $TF_environment is the most important because according to the env var set, the bucket reached will be different.

Related