📌 Steps to Migrate Terraform State
✅ 1. Backup the Current State
Before making any changes, it’s always good practice to backup the current state:
|
|
This ensures we have a snapshot of the existing state, just in case anything goes wrong.
🛠️ 2. Create a New GCS Bucket
Next, I created a new Google Cloud Storage (GCS) bucket to store the Terraform state:
|
|
📂 3. Copy the State File to the New Bucket
Once the bucket was ready, I copied the existing Terraform state file from the old bucket to the new one:
|
|
📝 4. Update the Backend Configuration
Now, I updated the Terraform backend configuration in main.tf
to point to the new bucket:
|
|
🔄 5. Migrate the State
To finalize the migration, I ran the following command:
|
|
Terraform automatically detected the change in backend configuration and migrated the state to the new bucket.
🔍 Verifying the Migration
After migrating the state, I performed several verification steps to ensure everything was intact.
🧐 1. Check the New State File
Pulled the new state file to compare with the old one:
|
|
🔍 2. Compare Old and New State Files
Checked if there were any unexpected changes:
|
|
🔄 3. Run a Plan to Ensure No Drift
To confirm the migration didn’t introduce unintended changes, I ran:
|
|
✅ Expected Outcome: The terraform plan should show no changes, confirming that everything migrated successfully.
🛡️ Safety Checks
To ensure the integrity of the migration, I followed these best practices:
- Plan should show no changes after migration ✅
- State list should match before & after migration ✅
- Diff between state files should be minimal (only backend config changes) ✅
🎉 Conclusion
Migrating Terraform state can seem risky, but by following a structured approach and verifying each step, you can ensure a smooth transition without disrupting your infrastructure.
If you ever need to migrate your Terraform state, remember:
💡 Backup first
💡 Verify integrity after migration
💡 Run a terraform plan
to detect any unintended changes