Single developer, sole founder here working on an MVP. I made the decision during planning the system architecture to NOT go with IaC (CloudFormation, AWS Serverless Application Model) early on and use the GUI to configure my infrastructure. Reasoning was to reduce complexity and increase development speed. I used SAM on a previous project and while it was great when it worked, I spent a lot of time writing template code instead of application code (the code that’s most necessary to get the product to market).
I’m always thinking ahead and I was reading posts here that people really liked Terraform. I’ve never used it but it got me thinking more about my IaC decision.
My question for feedback is simply, how easy is it to transform my manually configured infrastructure into IaC code? Who here has done it and what was your experience (e.g. how, success/failure, lessons learned)?
Just start fresh and clean with terraform using your click ops built mvp as the model. Use terraform to recreate each resource type and take the time to understand things and add improvements like consistent tagging or whatever.
If you are new to terraform this is gonna be better than starting with a massive import or trying to punt on the hard work by letting some transformation or conversion engine make decisions for you that you don’t understand until well down the learning curve road at which point it may be hard to alter to ,wet your actual future needs
Honestly might be an unpopular opinion here because if GenAI hate but I did this recently and it worked shockingly well — use a CLI tool like Claude AI and give it permission to use a read only role on the AWS CLI. Let it come up with a skeleton infra as code based on what actually exists in the account. It helps if you can prompt it with what actual services you used.
Some parts are easy, but the journey to doing this is not straightforward. If you are in a position to simply re-create your resources using IaC from the start, that is a much easier task, frankly.
I use tofo, but the commands are the same for terraform, so I’m just referring to the tool as “tf” below.
The rinse-and-repeat cycle that’s worked for me is:
Identify a resource that you want to codify
Starting with the major components of the resource, create your HCL code describing the resource.
Use tf import to import the unmanaged resource you described into your state
Run tf plan to see if you missed anything
Repeat these steps until you have completely managed all aspects of the resource
There are tools like terraformer that can help translating existing resources into HCL, but in my experience these are just shortcuts to the cycle I described above, and you still want to format your code and split it up based on your deployment lifecycle.
Write Terraform code, then use import blocks. Write your Terraform code, and then add import blocks to import the existing infrastructure into the state file, so you can manage that infrastructure as code. This lets you write the code exactly as you want, but it is tedious and time consuming, as you typically need manyimport blocks, and it takes a while to look up all those IDs manually.
Use import blocks to generate code. In this approach, you add a bunch of import blocks and you run terraform plan -generate-config-out=PATH to generate the corresponding Terraform configuration for the imported resources. This saves you time on writing the Terraform code, but you still have to create all the import blocks manually, and the Terraform code you get back might not be organized the way you would’ve done it from scratch.
Use Terraformer to generate code. In this approach, you use the open source tool Terraformer to read the data from your cloud account and generate the Terraform code for the resources it finds. You can have it create code for all resources it finds or just a subset. This saves you time in that you don’t have to write code or import blocks, but again, the generated code is not likely to be written the way you would’ve done it yourself.
Write Terraform code, and use it to deploy new infrastructure. In this approach, you write Terraform code, and run apply to deploy a completely new set of infrastructure. You then manually undeploy your existing infrastructure. The advantage here is that this lets you write the code exactly as you want, gives you a clean deployment (typically, the original, manual deployment is quite messy and you want to change stuff anyway), and you don’t have to spend time tediously trying to import existing stuff.
The option I’ve seen used the most, and the one that seems to work the best, is a combination of (4) and (1). That is, you write Terraform code and use it to deploy new infrastructure for most things, and you add import blocks just for the handful of resources that you can’t deploy from scratch (e.g., an RDS DB that has real data in it you don’t want to lose).
It really depends on the complexity of the resources but importing into CloudFormation or Terraform is relatively simple.
I personally prefer terraform over cloudformation but it’s dealer’s choice. If you’re going to stay in only AWS then staying with CloudFormation is fine.
I’ve used this project to generate terraform projects. Can import most things in the aws account and get you started.
https://github.com/cycloidio/terracognita GitHub - cycloidio/terracognita: Reads from existing public and private cloud providers (reverse Terraform) and generates your infrastructure as code on Terraform configuration
Second this. It took about a year for us to fully import 5 environments into terraform but it was worth it.
Some resources can’t be imported (target group attachment comes to mind) but it’s otherwise painless. Write the terraform you expect, import the resource, let it determine and apply differences.
Cloudformation(CDK) import: don’t spend any time, maybe if it’s a single resource. Redeploy and migrate
Terraform: I have good experience using data sources and file generation to create the resources and import statements. Only works for many of the same resource type
My experience (and I’ve done dozens of these): Import stateful resources like S3 buckets, databases and networking. Redeploy stateless things like ECS tasks, roles and security groups.
As someone who has been there in the past, one possible caveat:
ClickOps does more than IaC. For instance, the first time you use a service via click ops, the interface creates service-linked roles and fixes various configurations, which doesn’t happen if you start straight out in IaC.
So if you start with clickops, then move to IaC, same account, it works. But then you go into a DR, or expansion, and try to use the IaC again on a fresh account, and it all fails… That’s why.
There’s ways on TF and CloudFormation to import them into a state that you can then move forward with. Just make sure you persist the state and keep it safe and you’ll be golden there’s already some great suggestions in this thread. I just wanted to make sure to mention you need to keep your state safe if this is your first time on IaC.