Prompt
How do I migrate from CloudFormation to Terraform without breaking deployments?
Latest observation
Migrating from CloudFormation to Terraform without breaking deployments is mostly about adopting existing resources safely, avoiding resource replacement, and changing one layer at a time.
Recommended approach
1) Inventory everything first
Make a full list of:
- CloudFormation stacks
- Resources in each stack
- Resource dependencies
- Outputs, parameters, mappings, conditions
- Manual changes made outside CloudFormation
You want to know which resources are:
- safe to recreate
- must be preserved
- shared across stacks/accounts
- stateful, like RDS, EFS, S3 buckets, DynamoDB tables
2) Recreate the CloudFormation model in Terraform
Before touching prod, translate the stack into Terraform:
- VPC, subnets, route tables
- IAM roles/policies
- Security groups
- Load balancers
- ECS/EKS/EC2 resources
- DNS, certificates, alarms, etc.
Be careful with:
- implicit dependencies in CloudFormation
- default values that differ between tools
- naming differences
- resource ordering
3) Import existing resources into Terraform state
Do not create new resources if the old ones already exist.
Use:
terraform importfor each resource- or newer Terraform import blocks if available in your workflow
This lets Terraform manage existing infrastructure instead of replacing it.
Important:
- Import the exact real-world resource IDs
- Verify Terraform config matches current settings before applying
- If config differs, Terraform may try to change resources on the first plan
4) Keep CloudFormation and Terraform from fighting
During migration, do one of these:
Option A: Terraform takes over resource ownership
- Import resources into Terraform
- Stop CloudFormation from managing those resources
- Remove or disable the CloudFormation stack only after Terraform is fully aligned
Option B: Migrate stack pieces gradually
- Move low-risk resources first
- Keep critical stateful resources in CloudFormation until last
- Split stacks so boundaries are clear
Avoid having both tools manage the same resource.
5) Use a “no-op” plan as your safety check
Before applying Terraform:
- Run
terraform plan - Expect zero changes if you’re just adopting resources
- If changes appear, investigate before proceeding
Common causes of unwanted diffs:
- tags
- default security group rules
- ordering differences in lists
- computed attributes
- naming/formatting differences
- IAM policy JSON normalization
6) Handle stateful resources very carefully
For resources that store data, avoid replacement unless you have a migration strategy:
- RDS / Aurora
- EBS volumes
- EFS
- S3 buckets
- DynamoDB tables
- ElastiCache clusters
- OpenSearch domains
Check whether Terraform wants to recreate them because of:
- immutable fields
- naming changes
- unsupported CloudFormation settings
- provider differences
If needed, use:
lifecycle { prevent_destroy = true }ignore_changesfor safe drift you don’t want Terraform to manage immediately
7) Migrate outputs and dependencies
CloudFormation stack outputs often feed other stacks/apps. In Terraform:
- replace stack outputs with Terraform outputs
- update consumers to read from Terraform outputs, remote state, SSM, or parameter store
- make sure CI/CD pipelines use the new source of truth
8) Move deployment pipelines last
Don’t switch your app deployment process too early.
A safe sequence is:
- Terraform manages infrastructure
- Verify infrastructure is stable
- Update deployment pipeline to use Terraform outputs
- Decommission CloudFormation stack
9) Decommission CloudFormation only after validation
Once Terraform is authoritative:
- confirm resources are healthy
- confirm plans are stable
- confirm app deploys succeed
- confirm monitoring/alerts are intact
Then:
- remove CloudFormation stack resources carefully
- ensure deletion policy / retention is set for stateful resources if needed
- clean up leftover artifacts manually if necessary
Best practices to avoid outages
- Migrate in small batches
- Start with non-production first
- Freeze unrelated infrastructure changes during migration
- Use feature flags or blue/green deployments for app changes
- Keep backups for stateful systems
- Document every import and ownership change
- Review Terraform plans in CI before applying
Common mistakes to avoid
- Creating Terraform resources before importing existing ones
- Deleting the CloudFormation stack too early
- Letting both systems manage the same resource
- Ignoring drift between actual infrastructure and config
- Migrating stateful resources without backup/rollback plans
- Not updating downstream references to stack outputs
A practical migration sequence
A safe sequence usually looks like this:
- Pick one stack or environment
- Freeze changes
- Translate CFN to Terraform
- Import existing resources
- Run
terraform planuntil it’s clean - Apply only if necessary
- Update dependent pipelines/services
- Validate in production-like environment
- Remove CloudFormation ownership
- Repeat for the next stack
Rule of thumb
If you want the shortest safe answer:
Build Terraform to match the existing CloudFormation-managed infrastructure exactly, import the resources into Terraform state, verify a no-op plan, then gradually remove CloudFormation ownership only after confirming deployments still work.
If you want, I can also give you:
- a step-by-step migration checklist
- an example import workflow
- or a CloudFormation-to-Terraform mapping strategy for a specific AWS stack.