r/devops • u/Heavy_Motor8100 • Sep 17 '26
Tools Migrating existing CloudFormation resources to OpenTofu -recommended approach?
Hi everyone,
We’re planning to migrate a large number of existing AWS resources currently managed through CloudFormation to OpenTofu.
Our environments are fairly siloed per customer, and each environment can include resources such as EC2 instances, load balancers, Auto Scaling Groups, VPCs, security groups, and other AWS services.
I’m looking for recommendations from anyone who has gone through a similar migration.
Specifically:
- Are there any tools that can help convert or migrate existing CloudFormation stacks to OpenTofu?
- What is the best approach for importing existing resources into OpenTofu state?
- How did you handle dependencies between resources during the migration?
- Would you recommend migrating one CloudFormation stack/customer environment at a time?
- Are there any common pitfalls or state-management issues we should watch out for?
- What approach would minimize the risk of OpenTofu trying to modify or recreate existing production resources after import?
Our main priority is to make the migration as safe and predictable as possible, with minimal disruption to existing customer environments.
Would appreciate hearing about any tools, workflows, or lessons learned from real-world migrations.
8
u/pachirulis Sep 17 '26
You got a neat terraform generate config thing, that + a little help from an LLM to clean up and extract stuff to variables and do some logic and you can just import all the stuff into terraform state, ready for your next changes to be done via that IaC (sorry I keep saying terraform but muscle memory is a thing)
2
u/burlyginger Sep 17 '26
I've done this in the past with a relatively tight repeated configuration.
It may or may not help with your setup. But for our microservices we have a fairly consistent posture.
I used the cloudformation API (via boto3) to read all the resources in a stack and create imports or resource definitions based on our standard practice.
I scripted a terraform plan and I selected the drift for changes that mattered and ignored changes that didn't.
This condensed a lot of the process into a bunch of focused tables on screen. That got me to a point where I could start mangling and adjusting as necessary.
The tools that dump resources as terraform are ok, but you end up with a massive amount of statically defined resources and IMO that's not acceptable.
I would only tolerate that under the condition that the next step is to refactor that terraform into something more manageable.
We lean on modules heavily and everything is better for using them.
2
u/riickdiickulous Sep 17 '26
You’re making the right choice. It’s not really that hard to do, but will be tedious and there will be some level of special case handling.
Life lifecycle ignore option is going to be your best friend. Tofu is going to want to rename and recreate all sorts of stuff. Just make it ignore names. There are drawbacks to that longer term, but is the lesser of the evils.
You’ll want to develop reusable modules that you call from a root module. This will allow you to configure deployments separately and handle discrepancies between deployments between the root and reusable modules.
Do your 2 most different stacks of the same type first, ideally at the same time. That will help you identify what needs to be configurable between stacks and what is constant. It makes the initial build out more difficult, but will save you rework later and may avoid some sort of incompatible architectural decision.
You’re referring to production workloads, but obviously develop and test the tofu modules on lower environments before ever touching production.
Your tofu applies should only import, not modify or recreate any resources. You may end up needing the tofu to do some changes on initial apply but I would keep that to an absolute minimum, only allowing changes that significantly improve the tofu code as compared to if you had to not make those changes. If you do need to make changes try to have them in a phase 2, after the initial resource migration to tofu.
2
u/Hour-Swimmer7140 Sep 17 '26
the one that actually bites: set DeletionPolicy Retain on everything in the stack and deploy that change before you import anything.
otherwise the day you delete the stack, or just drop a resource from the template, cloudformation deletes the live resource tofu now thinks it owns. the import was fine, the state is correct, and the instance is gone.
do that first, per stack. the rest is the tedious part people are describing here rather than the dangerous part
1
u/AutonomousInfra 25d ago
Do it stack by stack and never let both tools own the same resource at once. For each stack, write the tofu config, use import blocks to pull the existing resources into state, and run plan until it shows zero changes. Then update the CFN stack to set DeletionPolicy Retain on everything and delete it, so CloudFormation lets go without destroying anything.
With per customer envs I’d pick one low risk customer, script the whole flow end to end there, then repeat it everywhere else. Turn on drift detection on the tofu side early too, because someone will make a console change mid migration.
1
u/Suitable-Ad5348 Sep 18 '26
+1 on empty state + one stack at a time.
tofu still tries to recreate dumb stuff after import (names, tags). `lifecycle { ignore_changes = [...] }` on those until you actually want to manage them.
1
u/Miserable_Hippo1503 Sep 22 '26
The way this is framed makes the migration sound a lot safer than it actually is. Importing resources is the easy part, keeping CloudFormation and OpenTofu from fighting over the same stuff is where the real mess starts. You've got dependencies, drift, names, deletion policies and all the weird little differences between customer stacks, so trying to figure those out during the production move is asking for trouble. This is where having someone who actually does migration work, even an outside provider similar to trusted tech, can be useful before touching the live environments. Prove the process on the most different stacks first and get a clean no-op plan, otherwise you're basically testing in production.
1
u/cloudfixer_dev Sep 25 '26
To echo the
DeletionPolicy: Retainadvice (which is absolutely the most critical step here), the golden rule for this migration is: Do not import and refactor at the same time.Your safest path looks exactly like this:
DeletionPolicyto Retain.lifecycle { ignore_changes }until you get a perfect, cleanNo changesplan.Trying to optimize your IaC structure while actively migrating state is how you accidentally nuke a production VPC. Treat the state takeover and the code cleanup as two completely separate projects.
1
u/transendingAI Sep 26 '26
I would start with one small customer environment and keep its OpenTofu state separate. Inventory the resources, their physical IDs, and any references from other stacks before importing. Any generated HCL still needs checking against the deployed configuration.
The ownership handoff needs its own plan. Importing a resource into OpenTofu does not remove it from CloudFormation management. Test a retain-and-remove procedure for the relevant resource types before doing it in production, and avoid changes through either system during the handoff.
For the initial migration, aim for a plan with no resource changes. Investigate every proposed update or replacement rather than adding broad ignore_changes rules to hide differences. Cross-stack references and dependencies need checking even when each customer has separate state.
2
u/[deleted] Sep 17 '26
[removed] — view removed comment