Managing Terraform state
Terraform uses a state file to track what it has deployed and what resources it is managing. The state file is essential as it records all the infrastructure Terraform maintains. If you lose it, Terraform will lose track of what it has done so far and start treating resources as new and needing to be created again. Therefore, you should protect your state as code.
Terraform stores state in backends. By default, Terraform stores the state file as terraform.tfstate
within the workspace
directory, which is called the local backend. However, that is not the best way of managing the state. There are a couple of reasons why you should not store state in a local system:
- Multiple admins cannot work on the same infrastructure if the state file is stored within someone’s local directory
- Local workstations are not backed up; therefore, the risk of losing the state file is high even if you have a single admin doing the job
You might argue that...