0

I'm running terraform from a GCP compute instance to create projects using https://github.com/terraform-google-modules/terraform-google-project-factory

The definition seems correct--terraform creates a bare state file in the GCS bucket, but then after the apply, terraform has only created a local state file and doesn't update the remote backend statefile.

data "terraform_remote_state" "tfstate-gcp" {
  backend = "gcs"
    config = {
    bucket = "project-provisioning-state-5309"
    prefix = "project_${var.project_name}"
    }

}

As noted, running terraform init creates the bare backend state file in the correct bucket location:

{
  "version": 4,
  "terraform_version": "1.4.6",
  "serial": 1,
  "lineage": "ee20ff2d-f55d-aae0-7c4b-cbxyzxyzxead8",
  "outputs": {},
  "resources": [],
  "check_results": null
}

Running terraform apply, a terraform.tfstate file is created in the local directory, but the backend state file is not updated.

Any ideas what is going sideways?

ronin22
  • 1
  • 1

1 Answers1

0

The configuration you've shown is using the terraform_remote_state data source to retrieve the output values from another configuration's state.

To specify where to store the state for the current configuration, you will need to write a backend configuration.

Martin Atkins
  • 62,420
  • 8
  • 120
  • 138
  • Thanks Martin, I'll check that out again and let you know. My previous attempt to add the backend to the config didn't work because I use a variable to create the bucket prefix. – ronin22 Aug 31 '23 at 18:43
  • I moved the backend config as noted in the documentation, however I got an error saying variables aren't allowed in the backend definition--which is why my predecessor used the data block I'm assuming. ╷ │ Error: Variables not allowed │ │ on main.tf line 38, in terraform: │ 38: prefix = "state_file_${var.project_name}" │ │ Variables may not be used here. Will keep looking at this problem. – ronin22 Sep 01 '23 at 16:16