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
Step-by-step guide to migrate Terraform state between backends. Covers local to S3, S3 to Terraform Cloud, and cross-account migrations safely.
Migrating Terraform state between backends is a common operation as teams evolve their infrastructure management practices. Whether moving from local state to S3, or from S3 to Terraform Cloud, this guide covers the process safely.
terraform plan shows no changes)cp terraform.tfstate terraform.tfstate.backupresource "aws_s3_bucket" "terraform_state" {
bucket = "my-terraform-state-bucket"
lifecycle {
prevent_destroy = true
}
}
resource "aws_s3_bucket_versioning" "terraform_state" {
bucket = aws_s3_bucket.terraform_state.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_dynamodb_table" "terraform_locks" {
name = "terraform-locks"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "production/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}terraform init -migrate-stateTerraform will ask:
Do you want to copy existing state to the new backend?
Enter a value: yesterraform plan
# Should show: No changes. Your infrastructure matches the configuration.Configure your Terraform Cloud organization and workspace.
terraform {
cloud {
organization = "my-org"
workspaces {
name = "production"
}
}
}terraform init -migrate-stateterraform state pull > state.jsonterraform {
backend "s3" {
bucket = "new-account-state-bucket"
key = "production/terraform.tfstate"
region = "us-east-1"
role_arn = "arn:aws:iam::ACCOUNT_ID::role/TerraformStateAccess"
dynamodb_table = "terraform-locks"
encrypt = true
}
}terraform init -migrate-stateBefore any migration:
terraform plan — confirm no pending changesterraform planError: Backend configuration changedRun terraform init -reconfigure to reinitialize with the new backend.
terraform force-unlock LOCK_IDRelated: How to install AWS CLI on macOS using Homebrew — set up AWS CLI in minutes.
State migration is straightforward when done carefully. Always backup first, verify after, and test with non-production environments. The terraform init -migrate-state command handles most scenarios automatically.
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.
Understand the Terraform state file, its purpose, and best practices for managing it. Learn remote state, locking, and recovery techniques for teams.