Is there a way to run terraform apply for a specific tfstate file? By default it takes terraform.tfstate but I need to rename it as I am managing multiple state files in azure.
-
2If not already, you could maybe try using terraform workspaces. – Marko E Oct 20 '22 at 11:06
2 Answers
Yes, you can name your state file as you please if you are using Azure storage to manage your remote state:
terraform {
backend "azurerm" {
resource_group_name = "<resource_group>"
storage_account_name = "<storage_group>"
container_name = "<container_name>"
key = "hello_world.tfstate" <-- tf state filename
}
}
Also, check on Terraform workspaces as that be helpful to you as well.
Locally run init and plan to make sure there are no changes expected before running apply.
Push change through CI/CD pipeline to merge into main with no changes for apply to worry about.
Once you are done you can delete the older copy of the state file from storage.

- 753
- 3
- 14
-
Hi Tom 1) I went through workspaces, but the thing is I am in a process of creating tfstate files for multiple resources and placing them in the same folder. Now when I make some change in the tfvars I need to call that specific tfstate file to check the current config.Workspaces will create folder for each resource and that's not exactly what I am looking after. 2) As of now the storage options for me are my machine in sync with git. – Ghost rider Oct 21 '22 at 05:18
According to the terraform apply
help there is the -state
flag:
-state=path Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".
Executing:
terraform plan -state=yourfilename
, and laterterraform apply -state=yourfilename
;
will let you specify the state filename.
Please note, the file extension does not need to be .tfstate
and you can use whatever file name.

- 113
- 9
-
Tried to run the command but its giving me the following error. I have the tfstate and the main.tf file in the same directory,do I still need to change it via -chdir ? Also I have not mentioned state-out anywhere. PS C:\Users\Desktop\Terraform> terraform plan -state=terraform.tfstate ------------------------------------------------------------------ ╷ │ Error: Too many command line arguments │ │ To specify a working directory for the plan, use the global -chdir flag. ╵ For more help on using this command, run: terraform plan -help PS C:\Users\Desktop\Terraform> – Ghost rider Oct 21 '22 at 05:01
-
@Ghostrider did you make sure there are no spaces when specifying the option. See https://stackoverflow.com/questions/71587927/too-many-command-line-arguments-terraform-plan – javier Oct 21 '22 at 07:25
-
-
@Ghostrider could you edit the question with the folder structure or something else helpful to see what could be the problem? – javier Oct 21 '22 at 17:45
-