Skip to main content

Command Palette

Search for a command to run...

How to quickly import existing AWS infrastructure into Terraform

Updated
2 min readView as Markdown
How to quickly import existing AWS infrastructure into Terraform

When working on new backend projects, you get the opportunity to start provisioning the infrastructure with Terraform from the get-go. This makes it easier to onboard new team members and to manage subsequent changes to your infrastructure after your project is launched, all of which are version-controlled. However, most people don’t work on new projects; they maintain existing systems. In the past, transitioning from using the AWS console to Terraform for provisioning resources was not simple.

The terraform import command only imported the resource into your state file, meaning you had to cross-check with the console and write the corresponding Terraform code by hand. You could both generate the Terraform code and state file using terraformer (which is no longer maintained). However, this gets tricky if you already have part of your infrastructure in Terraform and want to import the rest that are not.

I recently discovered that Terraform has introduced a new approach that solves both issues with terraform import and terraformer. All you need to do is specify the ID of the resource in AWS, like the example below

Then, run the following commands.

And that’s it!. That resource has been imported into your Terraform state. You can now manage it using Terraform.