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 ?
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 :
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.
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".