DEV Community

loading...
Cover image for Terraform Associate Certification: Terraform Import

Terraform Associate Certification: Terraform Import

Daniel Huerta
DevOps Engineer at DigitalOnUs by TechMahindra
Updated on ・3 min read

Even when we are using an Infrastructure as Code tool such as Terraform can be a member of a team (including us 😅) that will ignore all this workflow and will create manually some resources directly in the cloud provider. It is unnecessary to say that it can be a really headache for the team, mainly because all the required changes for that resource need to be manually as well and ... that's why Terraform exists!! 🤯.

So, in this scenario there's a solution in TF and it is the import command 😎.

Terraform Import

This is a simple command that will help us in this tricky situations, it is going to (as its name says) import existing infrastructure to be managed by Terraform from now on.

But be careful, there is an important thing to mention here, Terraform will not create the configuration file of the resource that was created manually.

Because of this, it is necessary to first write manually a resource configuration block for the resource (with the same properties as the one that is already created), to which the imported object will be linked.

So let's do an example with an AWS EC2 instance:

  1. Create a file with the provider configuration (in this case AWS).
  2. Create a file in which we need to configure the resource that is already created in the Cloud with the same properties.
  3. Run terraform import

EC2 instance manually

Let's start creating the EC2 instance manually with some basic configuration.

Screen Shot 2021-07-19 at 12.58.19

Here it is important to review all the important properties that this resource has because they need to be the same as the ones that we are goin to set in Terraform to be able to match both resources.

Creating configuration file

See that just some attributes are required to mapped both resources:

Screen Shot 2021-07-19 at 13.11.23

Once we have created the configuration file for the resources let's run:

terraform import aws_instance.myec2 i-0c598199bf2771676

Change the name of the resources and the id according to yours.

Now the resource that was created manually has been mapped to the local one that we set with Terraform, it means that if we change something in the file, the change will be reflected in AWS.

Let's change the Name tag from manual to manual2:

Screen Shot 2021-07-19 at 13.15.25

Even when we run the plan command we can see that any resource will be created but just modified (it means that there's already an existing created resource with that configuration).

terraform plan

terraform apply -auto-approve

Then, we can see that the changes were reflected in AWS:

Screen Shot 2021-07-19 at 13.17.31

Now we can manage that resource using Terraform 😉.

Closure

The import command is very useful, these situations where a resource is created manually occurs very frequently 😅 fortunately exists this solution so don't hesitate to use it when it happens to you.

Oh! And just one last thing, Terraform will include the feature of creating the configuration file when we run the import command in future releases, so this tricky part will be just for a few moment 😉💙.

Discussion (0)