Terraform <> Heroku

Hi I am trying to use terraform with heroku and use the postgres backend option.

I am trying to segment the code into different folders for different environments but the state file in postgres seems to store everything in one… when I switch from folder a to folder b and tun terraform plan , it tries to delete resources from folder a and create resources from folder b.

Is there any guides on separating the backend config so each folder has its own state file ?

You have already had to configure storing the state in Postgres - so just configure different state locations for different environments.

my question was how ? the provider.tf config section doesnt allow for functions

│ on provider.tf line 12, in terraform:
│ 12: key = “${path_relative_to_include()}”

│ Functions may not be called here.

… do I have to manually edit each provider file with a unique path in each folder?

I recommend you post your complete Terraform code in a GitHub repo and link it here. It’s not really possible to help you with the very limited amount of information you’re currently sharing.

here is the github link. You will see 2 seperate folders for creating 2 different environment however with using backend pg {} somehow the state file for both points to the same location so if I switch folders and run terraform plan , it tries to delete whatever resource that is not in the current folder.

When I used to play with terraform and GCP we would differentiate folders and state file using :

remote_state {
backend = “gcs”

config = {
  bucket  = "${get_env("ENV", "default")}-tf-states"
  prefix  = "${path_relative_to_include()}"
  project = "${get_env("GOOGLE_PROJECT", "terraform")}"
}

}

this created a unique statefile for every folder.

I think you’re using some third-party wrapper for Terraform and not including relevant information about it in your posts, so I am unable to help you.

not sure what other info you need besides the repo link ? it has two heroku app folders each with similar setup just name diff for app.

you can check provider.tf

terraform {
required_version = “>= 1.0”

required_providers {
heroku = {
source = “heroku/heroku”
version = “~> 5.0”
}
}
backend “pg” {
}
}

I have another heroku app that has the postgres instance where I would like to store the statefiles :

export DATABASE_URL=heroku config:get DATABASE_URL --app ex-terraform

terraform init -backend-config=“conn_str=$DATABASE_URL”

The reference to google setup was something I have done in the past but the heroku setup is as basic as it gets.

and here is the issue I am talking about :

Plan: 44 to add, 0 to change, 44 to destroy.

44 resources were created in folder staging-7 , when I navigate to folder staging-8 and run terraform plan it tries to destroy 44 resources from staging-7 and create 44 for staging-8 ( likely because they are pointing to same statefile)

what I am trying to solve for : Each folder gets its unique statefile

Some code snippets you have shared imply you may be using Terragrunt. However, you’ve left all Terragrunt configuration out of the Git repository I suggested you share.

I therefore feel I don’t understand enough about your setup to give good advice.

I am not using terragrunt in the current setup. Terragrunt was something I used in the previous setup with google cloud and I was trying to mimic the same behavior with heroku <> terraform.

However, my terraform setup for heroku is all in that repo I shared. No use of terragrunt since that would complicate the setup. I just wanted to use terraform out of the box and use their recommendation on using postgres instance to store states.

Is there a way to have a state file for each folder in a vanilla terraform setup ? I want to create several apps and have them each be independent from a statefile perspective.

Thank you for removing the ambiguity of whether Terragrunt was involved.

In that case, I think the most promising option might be for you to explicitly configure a different schema_name in your backend "pg" {} block in each directory. See https://developer.hashicorp.com/terraform/language/settings/backends/pg#schema_name

here is what I tried to do right now :

`terraform {
  required_version = ">= 1.0"
  backend "pg" {}
  required_providers {
    heroku = {
      source  = "heroku/heroku"
      version = "~> 5.0"
    }
  }
}

data "terraform_remote_state" "staging_7" {
  backend = "pg"

  config = {
    prefix = "envs/staging/staging7"
   }
  
}

and terraform init complains :

   with data.terraform_remote_state.reforge_staging_7,
│   on provider.tf line 15, in data "terraform_remote_state" "reforge_staging_7":
│   15:   config = {
│   16:     prefix = "envs/staging/staging7"
│   17:    }
│ 
│ The given configuration is not valid for backend "pg": unexpected attribute "prefix".