State File and dependency lock file
Terraform State file
- Terraform keeps the remote state of the infrastructure
- It stores it in a file called
terraform.tfstate - There is also a backup of the previous state in
terraform.tfstate.backup - When you execute
terraform apply, a newterraform.tfstateand backup is created - This is how Terraform keeps track of the remote state
- If the remote state changes and you run
terraform applyagain, Terraform will make changes to meet the correct remote state again. - E.g., you manually terminate an instance that is managed by Terraform, after you run
terraform apply, it will be started again.
- If the remote state changes and you run
- You can keep the
terraform.tfstatein version control (e.g., git).- This will give you a history of your
terraform.tfstatefile (which is just a big JSON file) - This allows you to collaborate with other team members (however, you can get conflicts when two or more people make changes at the same time)
- This will give you a history of your
- Local state works well with simple setups. However, if your project involves multiple team members working on a larger setup, it is better to store your state remotely
- The Terraform state can be saved remotely, using the backend functionality in Terraform.
- Using a remote store for the Terraform state will ensure that you always have the latest version of the state.
- It avoids having commit and push the
terraform.tfstatefile to version control. - However, make sure the Terraform remote store you choose supports locking! (note: both s3 and consul support locking)
- The default state is a local backend (the local Terraform state file)
- Other backends include:
- AWS S3 (with a locking mechanism using DynamoDB)
- Consul (with locking)
- Terraform Enterprise (the commercial solution)
- Using the backend functionality has definite benefits:
- Working in a team, it allows for collaboration (the remote state will always be available for the whole team)
- The state file is not stored locally and possible sensitive information is only stored in the remote state
- Some backends will enable remote operations. The
terraform applywill then run completely remotely. These are called enhanced backends.
- There are two steps to configure a remote state:
- Add the back code to a
.tffile - Run the initialization process
- Add the back code to a
Terraform stores the state of the infrastructure that is being created from the TF files.
This state allows terraform to map real world resource to your existing configuration.
When you run terraform apply command to create an infrastructure on cloud, Terraform creates a state file called “terraform.tfstate”. This State File contains full details of resources in our terraform code. When you modify something on your code and apply it on cloud, terraform will look into the state file, and compare the changes made in the code from that state file and the changes to the infrastructure based on the state file.
Dependency Lock File :-
Terraform dependency lock file allows us to look to a specific version of the provider
If a particular provider already has a selection recorded in the lock file, Terraform will always re-select that version for installation, even if a newer version has become available.
We can override that behavior by adding the -upgrade option when we run terraform init,
Understanding the challenge
Terraform can create an infrastructure based on configuration you specified
It can happen that the infrastructure get modified manually.
The terraform refresh command will check the latest state of your infrastructure and update the state file accordingly.
Points to Note
We should not typically need to use this command, because Terraform automatically performs the same refreshing actions as a part of creating a plan in both the terraform plan and terraform apply commands.
Comments
Post a Comment