Prompt
How do I migrate from manual cloud setup to infrastructure as code?
Latest observation
Migrating from manual cloud setup to infrastructure as code (IaC) is mostly about recreating what you already have in code, safely, without breaking production. The key is to do it incrementally.
A practical migration plan
1. Inventory your current infrastructure
Start by documenting everything that exists:
- Networks/VPCs, subnets, routing, firewalls/security groups
- Compute instances, autoscaling groups, containers, serverless functions
- Databases, storage buckets, load balancers, DNS
- IAM/users/roles/policies
- Monitoring, logging, backups, secrets
Useful outputs:
- A dependency map
- A list of “critical vs noncritical” resources
- Which resources are mutable vs should be replaced
2. Pick your IaC tool
Choose based on your environment and team skills:
- Terraform / OpenTofu: good for multi-cloud and broad ecosystem
- CloudFormation: AWS-native
- Bicep: Azure-native
- Pulumi: general-purpose with real programming languages
- Ansible: better for configuration on machines than cloud provisioning
If you’re unsure, Terraform/OpenTofu is often the easiest starting point for a typical cloud migration.
3. Set up your IaC project structure
Create a clean repo structure such as:
modules/for reusable componentsenvironments/dev,environments/staging,environments/prodvariablesandoutputs- remote state backend and locking
Also define:
- naming conventions
- tagging standards
- secrets handling
- code review rules
4. Start with a small, low-risk slice
Don’t try to import everything at once.
A good first target:
- a dev environment
- a noncritical network component
- a new service or sandbox stack
This helps you validate:
- provider configuration
- state management
- CI/CD workflows
- review and apply process
5. Decide whether to import or recreate
For existing resources, you usually have two options:
Import existing resources into IaC state
Best when:
- the resource is already in production
- you want to avoid replacement
- the cloud resource is stable and well-understood
You’ll typically:
- write the code to match the existing resource
- import the resource into the state
- run a plan to detect drift
- adjust config until plan is clean
Recreate resources from scratch
Best when:
- the resource is easy to replace
- it’s noncritical
- the existing setup is messy or inconsistent
This is often cleaner for ephemeral or test environments.
6. Move one layer at a time
A common sequence:
- Identity and access
- Networking
- Shared services
- Compute/app tier
- Databases/storage
- DNS/load balancing
- Observability and backups
This order reduces blast radius and dependency issues.
7. Add CI/CD early
Treat IaC like software:
- lint/format on commit
- validate/plan in pull requests
- require approval before apply
- use separate environments for plan and apply if possible
Typical pipeline:
fmtvalidateplan- approval
apply
8. Manage state carefully
State is critical:
- use remote storage
- enable locking
- restrict access
- back it up
Never keep important state only on a developer laptop.
9. Watch for drift
Manual changes will happen during the transition. That’s normal.
To manage drift:
- regularly run
plan - compare actual vs desired state
- either codify intentional changes or revert manual edits
- set policy that production changes go through IaC
10. Clean up after migration
Once a resource is managed by IaC:
- remove one-off scripts or manual runbooks that duplicate it
- document how to change it via code
- enforce code review for changes
- tag the resource as managed by IaC
Common pitfalls
- Trying to “boil the ocean” and migrate everything at once
- Importing resources without matching configuration exactly
- Forgetting dependencies between resources
- Ignoring secrets and state security
- Not planning for rollback
- Allowing manual changes to continue indefinitely
Recommended transition strategy
A strong pattern is:
- Freeze major manual changes
- Document current state
- Build IaC for one subsystem
- Import or recreate carefully
- Validate and compare behavior
- Repeat subsystem by subsystem
- Make IaC the only approved path
Example rule of thumb
If a resource is:
- stable and critical → import it
- easy to replace → recreate it
- messy or inconsistent → consider rebuilding it cleanly
If you want, I can also give you:
- a step-by-step migration checklist
- a Terraform migration example
- or a migration plan for AWS/Azure/GCP specifically