How do I "export" my manually configure infrastructure into IaC

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)?


Related discussions:

How to configure CloudWatch Log Groups in CloudFormation for already deployed Lambda functions?
I'm finally through with CloudFormation and moving to Terraform. There are too many new features (like this issue) that go MONTHS without support. I'm using outdated workarounds to deal with solved problems that AWS chooses not to support in their IA
Questions on CloudFormation and enterprise use
Has anyone used ADF?
What to include in CloudFormation template?
Cloudformation doesn’t support ACM certificate validation, WTF?
Best way to get around the 60 Parameter Limit in CloudFormation
Use CloudFormation StackSets for Multiple Accounts in an AWS Organization
Using SSM Parameter Store with CloudFormation
AWS development environment/workflows for large teams
rain - A development workflow tool for working with AWS CloudFormation
For Infra creation - which is better (or easy) - AWS CDK vs CloudFormation vs AWS CLI
Scale your Cloudformation: success tactics for getting more out of Infrastructure as Code on AWS (2021)
Pull in VPC and Subnet IDs via CloudFormation
Anyone using CloudFormation for critical databases?
Feedback for AWS: improvements for CloudFormation
Learning CloudFormation, just created a yaml template file and am going through the stack creation steps. How hard will it be to delete all the created resources once I'm done?
Is there a way to generate a CloudFormation template of current infrastructure without recreating?
Publish a simple, cheap, CDN backed, static website using a single AWS CloudFormation script
Install AWS CLI With No Internet
CloudFormation best practices: fewer, larger stacks or more, smaller stacks?
In a CloudFormation template, is there a way to create a condition that checks if a resource (e.g. a DynamoDB table with a given name) already exists?
How do you go about debugging aws-based application in VSCODE?
Good course for TypeScript with focus on CDK?
How do multiple developers work on the same AWS SAM or CloudFormation Stack?
How to incorporate CloudFormation to my existing Github Action CI/CD to deploy a dockerize application to EC2?
Roast My Architecture (ECS Fargate)
Is lambda layers the right approach to this problem?
IaC: accidentally deleting Database service
Viewing all resources deployed by Terraform
Cloudformation passing "CustomJSON" to a layer..
Possible to 'export' current AWS configuration into CloudFormation format?
Alternatives/compliments to CloudFormation
Building a tool to create, manage and deploy cloudformation stacks to AWS, written in GO. Constructive criticism and/or pull requests craved! ^_^ Thanks in Advance! :-)
YAML in CloudFormation Designer
CloudFormation deployment tools and practices
Emulate console CloudFormation stack update on CLI
Guide on how to use codepipeline to compile and deploy a couple of lambdas?
A proxy for Elastic Search in a VPC
CloudFormation stack management tool
Automate Your Static Hosting Environment With AWS CloudFormation
CloudFormation - Windows EC2 UserData Powershell
A project to help generate CloudFormation or AWS CLI scripts for security configurations
auto scaling policy: how to get more control over the cloudwatch alarms?
CloudFormation - What tools do you use?
where to start cloudformation?

Terraform import is a command and there’s also the terraformer tool

For Terraform you use import block
You have to add the resources ids manually nevertheless its more accurate than other approaches

https://developer.hashicorp.com/terraform/language/import no but

My lesson 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

You can use terraformer: https://github.com/GoogleCloudPlatform/terraformer

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.

Look at Cloudformation/CDK import if you want an AWS-specific solution

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.

Both tofu and terraform also have an option to generate HCL from existing resources. It’s experimental, but decent: https://opentofu.org/docs/language/import/generating-configuration/

Here’s a good starter article on using tf import: https://spacelift.io/blog/importing-exisiting-infrastructure-into-terraform

IaC generator https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/generate-IaC.html

There are four primary options:

  1. 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 many import blocks, and it takes a while to look up all those IDs manually.

  2. 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.

  3. 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.

  4. 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.

ew no, just use tf import.

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

Use [IaC generator](IaC generator https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/generate-IaC.html). Then use Visual Studio Q integration to ask questions about the code, improve, explain thoroughly and even ask Q to transform to terraform. Deploy clean in another account preferably.

Learn some type of IaC. Eventually you will need it for consistency and scale. This has been my experience with… pretty much everything.

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.

Slow, laborious process of manual importing with the import block in Terraform. It’s tedious and boring but worth it.

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 :raising_hands:t2: 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.