I am trying to setup terraform backend.tf
for each environment while running init
command but terraform init
is not able recognize backend
block inside backend.tf
file. Here is my folder structure
project --|
|- env/--|-dev/------|-backend.tf
|-terraform.tfvar
|- stage/---|-backend.tf
|-terraform.tfvar
|-main.tf
|-output.tf
|-variables.tf
|-modules/-|-lambda--|----python/
|----zip/
|--main.tf
|--output.tf
|--variables.tf
|--requirement.tf
I have kept backend.tf
for each environment and running my terraform command from env/
folder.
here is init command project/dev> terraform init -reconfigure -backend-config="dev/backend.tf"
however terraform is not able to recognize backend.tf
from dev
folder.
This is my backend.tf
file
terraform {
backend "s3" {
bucket = "bucket-terraform"
key = "service-513/terraform.tfstate"
region = "us-west-2"
dynamodb_table = "table-terraform"
}
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
After running Terraform init, here is the warning:
╷
│ Warning: Missing backend configuration
│
│ -backend-config was used without a "backend" block in the configuration.
│
│ If you intended to override the default local backend configuration,
│ no action is required, but you may add an explicit backend block to your
│ configuration to clear this warning:
│
│ terraform {
│ backend "local" {}
│ }
│
│ However, if you intended to override a defined backend, please verify that
│ the backend configuration is present and valid.
│
╵
No idea, why it is happening. I would appreciate if anyone can guide me in right direction.
2
Answers
When using
-backend-config
you supply the values of the keys for the partial configuration of the backend as described here.as stated above key=values and you must have state.tf in same directory where you run terraform, it doesnt look for tf files in subdirs
e.g.