norrin
norrin

Reputation: 171

How to use terraform import with resource configuration

Prior to terraform import, I have defined:

# instance.tf

resource "aws_instance" "appserver" {

}

Then I ran: terraform import aws_instance.appserver <instance-id> and went smoothly, which I can see the imported ec2 resource by using terraform show. However, the mystery to me is to "transfer" this existing terraform state into the terraform config (instance.tf above) so that I can manage it as an Infrastructure as a Code (or at least that how I understood it). I added the ami and instance_type keys and their corresponding values but every time I issue terraform plan, terraform seems to want to "replace" my existing instance.

1) Why is terraform want to replace that instance?

2) How can I "transfer" the instance's terraform state into the config? (is this possible?)

3) For you guys seasoned veterans, how were you guys able to manage an existing aws infrastructure in terraform?

Upvotes: 1

Views: 2816

Answers (2)

Brent Bradburn
Brent Bradburn

Reputation: 54869

Terraform 1.5 introduced import blocks for "config-driven import". This allows plan/apply operations to seamlessly manage the state of your preexisting infrastructure if you simply provide the import ID values as part of your configuration.

Example import block:

import {
  to = aws_instance.example
  id = "i-abcd1234"
}

To assist with config generation, Terraform now offers terraform plan -generate-config-out=PATH. In the tutorial Import Terraform Confiuration, importing existing resources is described as a five-step process including...

  1. Run terraform plan to review the import plan and optionally generate configuration for the resources.
  2. Prune generated configuration to only the required arguments.
  3. Apply the configuration to bring the resource into your Terraform state file.

The tutorial includes this associated recommendation...

We recommend that you prune the generated configuration to only required arguments and arguments whose values differ from defaults, to reduce the size of your configuration.

Upvotes: 1

Gilad Neiger
Gilad Neiger

Reputation: 643

First of all, terraform wants to replace your instance because terraform didn't do the 'link' you expected between the resource configuration and the current existing instance.

Terraform official documentation: (https://www.terraform.io/docs/import/index.html)

The current implementation of Terraform import can only import resources into the state. It does not generate configuration. A future version of Terraform will also generate configuration.

Because of this, prior to running terraform import it is necessary to write manually a resource configuration block for the resource, to which the imported object will be mapped.

While this may seem tedious, it still gives Terraform users an avenue for importing existing resources. A future version of Terraform will fully generate configuration, significantly simplifying this process.

After understanding the written above, I would use the following steps:

  1. First, write your terraform resource configuration. Should look like this:

    resource "aws_instance" "example" {
         # ...instance configuration...
    }
    
  2. terraform import aws_instance.example i-abcd1234 in order to import existing infrastructure to your state and attach it to the resource configuration you've created above.

Detailed source for more: https://www.terraform.io/docs/import/usage.html

Upvotes: 2

Related Questions