Bitzu

Terraform Associate 004 ยท Maintaining infrastructure

Importing existing infrastructure

import block with to/id, terraform plan -generate-config-out, legacy terraform import command, import brings state only, writing config (7a).

Key points

  • An import block has two key arguments: to (the address the object gets in your configuration and state) and id (the real object's identifier at the provider).
  • Running terraform plan -generate-config-out=<new file> drafts matching resource blocks.
  • Applying an import only records the object in state; it never creates or changes the remote object by itself.
  • The older terraform import ADDRESS ID command does one object per run.

Source: https://developer.hashicorp.com/terraform/language/import

Practice questions

Single choice

Which argument of an import block names the address the imported object will have in Terraform?

  1. to
  2. id
  3. from
  4. for_each
Show answer

Answer: A. to

to is the destination address in configuration and state. id is the provider-side identifier of the real object, so it answers a different question.

Select all that apply

Which statements about import blocks and config generation are true?

  1. The import is previewed in terraform plan before it is applied
  2. -generate-config-out may point to an existing file, which Terraform appends to
  3. The to argument is the resource address the object will have
  4. Applying an import creates the object if it does not yet exist
  5. Removing an import block after a successful apply deletes the remote object
Show answer

Answer: A. The import is previewed in terraform plan before it is applied, C. The to argument is the resource address the object will have

Imports show up in the plan and to is the destination address. The output path must be a new file (an existing one causes an error), import only records state, and removing the block does nothing to the remote object.

True or false

The legacy terraform import command writes the matching resource block into your .tf files for you.

Show answer

Answer: False

False. The legacy command only records the object in state against an address; you write the configuration yourself. Generating drafts is a feature of plan -generate-config-out.

Practice 8 questions on this topic in Bitzu

Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.

Download on the App StoreGet it on Google Play

More Terraform Associate 004 topics in this module