ljfranklin / terraform-resource

A concourse resource to create infrastructure via Terraform
MIT License
185 stars 85 forks source link

Using GCS bucket with pre-existing state file #145

Closed ralphpi closed 3 years ago

ralphpi commented 3 years ago

My tf creates a set of subnets and binds permission to them. The tf repo used to be ran locally using backend.tf to define the same bucket information in the resource below. So ive sinced removed the backend.tf from repo to utilize the bucket setup in this resource. When I run a plan only - put its not using the existing state file default.tfstate to determine whether or not resources need to be create\updated\deleted. It seems to create its own to based on what it seems like the env_name. Is this intended to use only in new tf configs?

pre-existing bucket image

backend.tf

terraform {
  required_version = "> 0.11.7"

  backend "gcs" {
    bucket  = "saas-terraform"
    prefix  = "dev/networks/us-east4/subnets/state"
  }
}

resources:
- name: terraform
  type: terraform
  source:
    env_name: development
    backend_type: gcs
    backend_config:
      bucket: saas-terraform
      prefix: dev/networks/us-east4/subnets/state
      credentials: ((gcp-json-key))
    env:
       GOOGLE_APPLICATION_CREDENTIALS: ((cred-path))
       GCP_JSON_KEY: ((gcp-json-key))
    vars:
      env: dev
ljfranklin commented 3 years ago

Take a look at Terraform workspaces. Your previous apply used the default workspace, so it created default.tfstate. The env_name field corresponds to the workspace name, so the resource created development.tfstate since you set env_name: development. If you want to use the existing statefile then set env_name: default.

ralphpi commented 3 years ago

Makes sense, I ended copying the existing default since we are just going to continue with how it uses workspaces.