Help needed with Terraform backend setup across environments
I’m struggling to set up my Terraform backend for different environments. I’ve got a backend.tf file for each environment, but when I run terraform init, it’s not picking up the backend block. Here’s what I’m dealing with:
Project structure has separate folders for dev and stage environments
Each env folder has its own backend.tf and terraform.tfvar
It seems the issue lies in how you’re specifying the backend configuration. When using -backend-config, Terraform expects a file with key-value pairs, not a full backend block. Instead, try creating a separate file (e.g., ‘dev-backend.hcl’) with just the configuration values:
Keep the terraform block with the backend type in your main configuration, but remove the specific settings. This approach allows for more flexibility when switching between environments and should resolve your backend configuration issues.
hey, have you considered using workspace variables for different environments? it could simplify your setup. what if you create a single backend.tf with placeholders like ${workspace_name} for the bucket and key? then use terraform workspace commands to switch between envs. curious to hear your thoughts on this approach!