The Ops Community ⚙️

Luiz Bernardo
Luiz Bernardo

Posted on

Import Terraform

It's unlikely that you will deal only with greenfield in cloud infrastructure, so how can you continue to use IAC with the components that are already in production?

You can import existing infrastructure resources into Terraform with terraform import.

In the provider's resource documentation, there is always an example of how to import that resource into Terraform.

For example:

In the aws_instance resource documentation (https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance), it shows an example of importing instances:

$ terraform import aws_instance.web i-12345678
Enter fullscreen mode Exit fullscreen mode

The current implementation (terraform_1.3.4) of Terraform's import can only import resources into the state. It does not generate configuration. A future version of Terraform will also generate configuration.

Therefore, before running terraform import, it is necessary to manually write a resource configuration block to which the imported object will be mapped.

Although this may seem tedious, it still offers Terraform users a path to import existing resources.

The following link is a practical tutorial on how to import resources into Terraform:
https://developer.hashicorp.com/terraform/tutorials/state/state-import?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS

Top comments (0)