Skip to content

002 · Domain 3: Infrastructure Lifecycle and Operations · 22% of the exam

Task 3.4: Import existing infrastructure to an infrastructure project

Bringing existing objects under management at scale: import blocks with for_each, generated configuration and its review, and the import command for one-off work.

Study it

Sample questions

Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.

Question 1 · choose 1

Forty existing buckets must come under a module that each team calls through one module block: module "team" { for_each = local.teams source = "./team-buckets" # inside: resource "aws_s3_bucket" "this" { for_each = ... } } local.inventory is a list of objects with team, name and bucket_id. The import must happen in one reviewed plan and apply. What should the team add to the root module?

  1. AA script that runs terraform import once per bucket with the module address and bucket ID
  2. BAn import block with to = module.team.aws_s3_bucket.this and id = local.inventory[*].bucket_id
  3. COne import block with for_each = local.inventory, id = each.value.bucket_id and to = module.team[each.value.team].aws_s3_bucket.this[each.value.name]
  4. DImport blocks inside ./team-buckets with for_each = local.inventory, one module per team
Show the answer and why
  • AA script that runs terraform import once per bucket with the module address and bucket ID

    Incorrect

    terraform import changes state one object at a time outside the plan, so the forty imports are never reviewed together in one plan.

  • BAn import block with to = module.team.aws_s3_bucket.this and id = local.inventory[*].bucket_id

    Incorrect

    to must name one instance address and id must be a single string, so the module and resource keys cannot be left out.

  • COne import block with for_each = local.inventory, id = each.value.bucket_id and to = module.team[each.value.team].aws_s3_bucket.this[each.value.name]

    Correct

    An import block's for_each can loop over a list of objects, and the to address can use each.value for both the module key and the resource key.

  • DImport blocks inside ./team-buckets with for_each = local.inventory, one module per team

    Incorrect

    local.inventory is defined in the root module, and local values are available only in the module that defines them, so the shared module cannot iterate over it.

for_each in import blocks lets one block import many objects into module instances and resource instances alike, all visible in a single plan before anything is written to state.

Question 2 · choose 1

An import block sets id = aws_lb.new.arn, where aws_lb.new is created in the same run. Why does planning fail?

  1. AImport blocks accept only literal strings
  2. BImport blocks must sit in a separate imports.tf file
  3. CThe ID must be known during the plan operation
  4. DOnly terraform import can use a resource attribute
Show the answer and why
  • AImport blocks accept only literal strings

    Incorrect

    The id can be an expression, such as a variable, if it evaluates to a string.

  • BImport blocks must sit in a separate imports.tf file

    Incorrect

    Import blocks can be in any configuration file.

  • CThe ID must be known during the plan operation

    Correct

    The id must evaluate to a string that is known at plan time, and an attribute of a resource created in the same run is unknown then.

  • DOnly terraform import can use a resource attribute

    Incorrect

    The CLI command takes a literal ID on the command line.

Pass known IDs through variables or data sources whose values are available while planning.

Question 3 · choose 1

A bucket to import lives in a region served by provider "aws" with alias = "east". Which import block argument makes Terraform use that configuration?

  1. Aregion = "us-east-1" inside the import block
  2. Balias = "east" inside the import block
  3. Cfor_each over the provider configurations
  4. Dprovider = aws.east
Show the answer and why
  • Aregion = "us-east-1" inside the import block

    Incorrect

    Import blocks have no region argument.

  • Balias = "east" inside the import block

    Incorrect

    alias belongs in provider blocks.

  • Cfor_each over the provider configurations

    Incorrect

    for_each repeats the import; it does not choose a provider.

  • Dprovider = aws.east

    Correct

    The provider meta-argument tells Terraform which provider configuration to import the resource with.

The resource block for the imported object should name the same provider configuration.

Question 4 · choose 1

A weekly job runs terraform query -generate-config-out=generated.tf to collect import candidates. The first run works; the next week's run fails before writing anything. What is the cause?

  1. Agenerated.tf already exists, and the command will not overwrite it
  2. BIn HCP Terraform workspaces the file is written remotely instead
  3. CThe flag works only once per directory until terraform init runs again
  4. DGenerated configuration needs a name ending in .tfquery.hcl
Show the answer and why
  • Agenerated.tf already exists, and the command will not overwrite it

    Correct

    Rerunning the command when the file already exists at that path results in an error, so remove the file before each run.

  • BIn HCP Terraform workspaces the file is written remotely instead

    Incorrect

    Even when connected to HCP Terraform, the command writes generated.tf on the local workstation.

  • CThe flag works only once per directory until terraform init runs again

    Incorrect

    No such limit exists; the existing file is the problem.

  • DGenerated configuration needs a name ending in .tfquery.hcl

    Incorrect

    .tfquery.hcl is for query files; the generated configuration is an ordinary .tf file.

The generated file holds resource and import blocks, including resource identities. Copy what you need into the configuration, then delete the file before the next run.

Question 5 · choose 1

terraform import fails because the provider block reads its region from a data source. What limitation applies?

  1. Aterraform import ignores configuration files and needs flags
  2. BThe provider configuration cannot depend on non-variable inputs
  3. CThe command works only with the local backend
  4. DProvider blocks must be removed before importing
Show the answer and why
  • Aterraform import ignores configuration files and needs flags

    Incorrect

    Terraform loads configuration files that configure the provider.

  • BThe provider configuration cannot depend on non-variable inputs

    Correct

    For import, provider configuration may use variables but must not depend on inputs such as a data source.

  • CThe command works only with the local backend

    Incorrect

    import works with remote backends too.

  • DProvider blocks must be removed before importing

    Incorrect

    The command uses provider blocks when they are present.

Pass the region through a variable for the import, or use an import block, which plans with the full configuration.

Practice domain 3 →Practice all domains →