Before executing your Terraform configuration, it's important to understand how Terraform manages state.
The terraform.tfstate file is created after running terraform apply for the first time. This file tracks the status of all sources, destinations, and connections managed by Terraform — it serves as a snapshot of your infrastructure's current state.
Important: Do not modify the .tfstate file manually. Terraform relies on this file to determine what changes are needed between your configuration files and the actual infrastructure.
For subsequent executions of terraform apply, Terraform compares the code in the main.tf file with the state stored in the .tfstate file. If you add or remove resources in main.tf, Terraform automatically updates both your deployment and the .tfstate file accordingly.
Best practice for version control: If you're using CI/CD or collaborating with team members, do not commit the .tfstate file to version control. Instead, use remote state storage (such as Terraform Cloud, S3, or Azure Blob Storage) to manage state securely across your team.