404 β€” Refactoring State

Advanced

Advanced state manipulation: importing existing resources, renaming without recreation, and moving resources between states.

Learning Objectives

1
Import existing resources with terraform import
2
Rename resources using the moved block
3
Move resources with terraform state mv
4
Safely refactor Terraform configurations
Step 1

Create existing resources to import

Create files manually that simulate pre-existing infrastructure.

Commands to Run

mkdir -p ~/terraform-practice/lesson-404
cd ~/terraform-practice/lesson-404
cat > existing-app.conf << 'EOF'
name = legacy-application
version = 1.0.0
port = 3000
EOF
cat > existing-db.conf << 'EOF'
host = legacy-db.internal
port = 5432
max_connections = 50
EOF
ls -la *.conf

What This Does

These files represent infrastructure that exists but wasn't created by Terraform. This is common when adopting Terraform for existing systems.

Expected Outcome

Two 'legacy' configuration files created manually.

Pro Tips

  • 1
    Import is how you bring existing infrastructure under Terraform management
Was this step helpful?

All Steps (0 / 8 completed)