How to Use Terraform with GitHub Actions - CICD Pipeline Guide
Set up Terraform CI-CD with GitHub Actions. Covers plan on PR, apply on merge, state locking, secrets management, and environment protection.
Terraform
Understand the Terraform state file, its purpose, and best practices for managing it. Learn remote state, locking, and recovery techniques for teams.
The Terraform state file is one of the most critical components of any Terraform workflow. It maps your configuration to real-world resources and tracks metadata that Terraform needs to function correctly.
Understanding state is essential for working effectively with Terraform, especially in team environments.
Terraform state is a JSON file (typically terraform.tfstate) that stores the mapping between your Terraform configuration and the actual infrastructure resources. Every time you run terraform apply, Terraform updates this file.
terraform plan{
"resources": [
{
"mode": "managed",
"type": "aws_instance",
"name": "web",
"instances": [
{
"attributes": {
"id": "i-0abc123def456",
"ami": "ami-0c55b159cbfafe1f0",
"instance_type": "t3.micro",
"public_ip": "54.123.45.67"
}
}
]
}
]
}Without state, Terraform cannot:
By default, Terraform stores state in a local file:
project/
├── main.tf
├── variables.tf
└── terraform.tfstate ← local stateProblems with local state:
Store state in a shared backend:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}Benefits:
State locking prevents concurrent operations that could corrupt state:
# DynamoDB table for state locking
resource "aws_dynamodb_table" "terraform_locks" {
name = "terraform-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}If a lock gets stuck:
terraform force-unlock LOCK_IDEssential terraform state commands:
# List all resources in state
terraform state list
# Show details of a specific resource
terraform state show aws_instance.web
# Move a resource (rename)
terraform state mv aws_instance.old aws_instance.new
# Remove a resource from state (without destroying)
terraform state rm aws_instance.web
# Pull remote state to local
terraform state pull > state.json
# Push local state to remote
terraform state push state.jsonIf your state file is corrupted:
terraform.tfstate.backup# Import an existing resource
terraform import aws_instance.web i-0abc123def456For large projects, split state across workspaces:
terraform workspace new production
terraform workspace new staging
terraform workspace select productionterraform state commandsThe Terraform state file is the bridge between your code and your infrastructure. Managing it properly — with remote backends, locking, and encryption — is essential for production Terraform workflows.
Set up Terraform CI-CD with GitHub Actions. Covers plan on PR, apply on merge, state locking, secrets management, and environment protection.
Master Terraform workspaces for managing dev, staging, and production environments. Covers workspace commands, state isolation, and CI/CD integration.
Configure Terraform backends for remote state. Complete guide for S3 + DynamoDB, Azure Blob, GCS, Terraform Cloud, and Consul with encryption and locking.
Master Terraform variables with practical examples. Learn input, output, local, and environment variables for flexible infrastructure as code configurations.