Search code examples
google-cloud-platformterraformgoogle-cloud-storageterraform-provider-gcpterraform-state

Separate GCS terraform backends to deploy multiple resources in respective projects


I have three GCP projects let's say project-01, project-02 and project-03 and I'm trying to create Compute Instance in those respective projects using single terraform module. Below is the terraform.tfvars file

compute_instances = {
  compute_instance_01 = {
    project_id                   = "project-01"
    instance_name                = "instance_01"
    zone                         = "asia-south1-a"
    machine_type                 = "e2-small"
  }

  compute_instance_02 = {
    project_id                   = "project-02"
    instance_name                = "instance_02"
    zone                         = "asia-south1-a"
    machine_type                 = "e2-medium"
  }

  compute_instance_03 = {
    project_id                   = "project-03"
    instance_name                = "instance_03"
    zone                         = "asia-south1-a"
    machine_type                 = "e2-large"
  }
 }

How do I keep terraform state file in remote GCS bucket in their respective projects in single backends.tf file ?


Solution

  • Not sure I understand correctly what you would like to achieve ultimately, and I guess it might not be possible to get that 100% accurately, but something can be done.

    From the best of my understanding, one can define a Terraform state file location running the terraform init command.

    For example:

            terraform init \
            -backend-config=bucket="<<gcs bucket name>>" \
            -backend-config=prefix="<<a path prefix inside the bucket>>"
    

    Under assumption, that the backend.tf file looks like:

    terraform {
      backend "gcs" {
      }
    }
    

    So, while it might not be possible to create many terraform state files from one terraform init/apply command set, it may be possible to

    • choose what is to be deployed and what is not (deployed) in one or another terraform execution, or probably better to
    • define names of resources (including identifiers of the target projects for deployment, names of resources to be deployed, and target buckets/prefixes for the terraform state files storage) in some variables (in bash outside of the terraform files and/or inside terraform files), so when the init is executed, the correct state file location is chosen; and when the apply is executed, the correct resource names are calculated.

    Let me know if you would like further hints on how to achieve the later, or the above information is enough.