Terraform State File
What is the State File in Terraform?
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.
Key Points About Terraform State Files:-
1) Purpose of State Files:-
Store the state of your infrastructure.
Enable Terraform to map resources in your configuration to real-world resources.
Facilitate planning and applying changes.
2) How State Files Work:-
Terraform reads the state file to understand the current infrastructure state.
During terraform plan, it compares the desired state (from the configuration files) with the current state (from the state file).
Any differences are presented as an execution plan, which is then applied using terraform apply.
3) State File Locations:
Local:- Stored on your local machine in the default location "terraform.tfstate".
Remote:- Stored in remote backends like AWS S3, Azure Blob Storage, or HashiCorp Consul for collaboration and security.
4) Managing State Files:
State locking:- Prevents concurrent operations from corrupting the state file.
State file security:- Encrypt and secure state files, especially when using remote backends.
State file versioning:- Enable versioning to track changes and rollback if needed.
5) Example:- Creating and Working with a State File
1) Initial Configuration:
- Create file name called main.tf and write this code
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
2) Applying Configuration:
terraform init
terraform apply
- Running these commands initializes Terraform and applies the configuration, creating the terraform.tfstate file.
3) State File Contents:
{
"version": 4,
"terraform_version": "1.0.11",
"resources": [
{
"mode": "managed",
"type": "aws_instance",
"name": "example",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 1,
"attributes": {
"id": "i-0abcd1234efgh5678",
"ami": "ami-0c55b159cbfafe1f0",
"instance_type": "t2.micro",
"availability_zone": "us-west-2a"
}
}
]
}
]
}
- This JSON data represents the AWS EC2 instance created by Terraform.
4) Modifying Configuration:
- Change the instance type in the configuration
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.small"
}
5) Planning and Applying Changes:
terraform plan
terraform apply
- Terraform reads the state file, detects the change, and updates the instance type.
By leveraging the power of Terraform state files, you can maintain control over your infrastructure and ensure that changes are applied accurately and predictably. This knowledge will help you become more proficient in using Terraform to manage your infrastructure as code effectively.