When I start Terraform with terraform apply ../myEnvironment/ it successfully takes the main.tf file in ../myEnvironment/main.tf and creates a terraform.tfstate and a .terraform directory, but at the same time it creates a .terraform directory including a terraform.tfstate in the directory from where the Terraform command was executed.
Is there a way to say Terraform only create files and .terraform directory in the directory, where the main.tf file is located? so when executing terraform apply ../myEnvironment/ only create files in ../myEnvironment/main.tf?
I already included
terraform {
backend "local" {
path = "../myEnvironment/terraform.tfstate"
}
}
in my main.tf
So I found the solution.
As mentioned here you need to create the environment variable before starting Terraform:
TF_DATA_DIR=../myEnvironment/.terraform terraform init ../myEnvironment/
TF_DATA_DIR=../myEnvironment/.terraform terraform apply ../myEnvironment/
An answer mentioned in the documentation here specified the terraform.tfstate location in the main.tf file(s):
terraform {
backend "local" { path = "../myEnvironment/terraform.tfstate" }
}
This way you are independent as to where you execute Terraform to do its magic. :)
Maybe it will help someone to save some time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With