Terraform Best Practices
Terraform Backend for storing Tfstate file
Terraform stores the infrastructure information in the tfstate file. By default, Terraform stores the tfstate files in the locally. Because tfstate file has all infra details, it’s better to store it remotely. To remotely store the tfstate file Terraform provides a feature called Terraform backend. Terraform backend allows you to store the tfstate files in S3, blob storage, cloud storage, etcd, jfrog, etc. Most of the Terraform backend also allows state locking. State locking means updating of terraform infrastructure can only be done sequentially. Updating parallelly of the same infrastructure is not allowed.
Terraform Workspaces for managing various environments
Terraform allows you to configure multiple environments using workspaces. Create the workspaces to isolate dev, stage and prod environments. Use terraform backend and Workspaces to isolate different environments.
Tag all the resources which you can. Tagging help search the resources easily. Also, mainly tagging can be used to identifying resources during billing.
Use TFVARS file
Store all the variable values in the Tfvars file. Hence you can use the same code with multiple Tfvars files to build different environments. Using default values in variables.tf is not a sustainable practice.
The open-source community provides predefined modules for creating the infrastructure. Understand and use them
If modules are not readily available, then create the custom modules. This helps with the reusability of the code.