004 · Domain 7: Maintain infrastructure with Terraform · 12% of the exam
Task 7.1: Import existing infrastructure into your Terraform workspace
Bringing existing objects under management: import blocks planned and applied like any change, generated configuration with -generate-config-out, for_each in import blocks, and the terraform import command.
Study it
Importing existing infrastructure: import blocks, generated configuration, the import command
Lesson coming
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
A storage bucket was created by hand and must now be managed by Terraform. The team writes a matching resource block and wants to preview the import in a plan before anything is recorded. What should it add?
AA data block for the bucket, which Terraform converts to a managed resource on apply
BA moved block from the bucket's cloud ID to the resource address
CA lifecycle block with ignore_changes = all, so Terraform adopts the bucket as it is
DAn import block with to and id, then plan and apply
Show the answer and why
AA data block for the bucket, which Terraform converts to a managed resource on apply
Incorrect
Data sources only read objects; they never become managed resources.
BA moved block from the bucket's cloud ID to the resource address
Incorrect
moved changes addresses of objects already in state; it cannot bring in new objects.
CA lifecycle block with ignore_changes = all, so Terraform adopts the bucket as it is
Incorrect
ignore_changes affects updates of managed objects; it does not import anything.
DAn import block with to and id, then plan and apply
Correct
Import blocks are part of the configuration, so the import appears in the plan for review and is recorded in state on apply.
Import blocks make imports part of the normal plan and apply workflow.
A team must bring forty existing firewall rules under Terraform and has no configuration for them. It has written import blocks for all forty. Which command produces starting configuration for them?
Aterraform import -generate-config for each rule
Bterraform plan -generate-config-out=generated.tf
Cterraform show -json > generated.tf, then rename the attributes
Dterraform console with an export command
Show the answer and why
Aterraform import -generate-config for each rule
Incorrect
The import command only imports into state and never generates configuration.
Bterraform plan -generate-config-out=generated.tf
Correct
With import blocks for resources that have no configuration, plan -generate-config-out writes HCL for them to a new file; the feature is marked experimental.
Cterraform show -json > generated.tf, then rename the attributes
Incorrect
show -json prints JSON state, which is not usable as configuration.
Dterraform console with an export command
Incorrect
console evaluates expressions; it has no export of configuration.
Generated configuration is a template: review it, trim it and move it into the right files before applying.
An engineer runs terraform import aws_instance.legacy i-0abc123 in a configuration that has no aws_instance.legacy block. What happens?
AIt fails; terraform import needs an existing resource block
BThe instance is imported, and a matching resource block is written to imported.tf
CThe instance is imported into state, and the next plan keeps it
DTerraform creates a new instance with the same settings
Show the answer and why
AIt fails; terraform import needs an existing resource block
Correct
Before running terraform import you must write a resource block that describes where to map the imported object; the command does not generate configuration.
BThe instance is imported, and a matching resource block is written to imported.tf
Incorrect
The CLI command never writes configuration.
CThe instance is imported into state, and the next plan keeps it
Incorrect
Without a block, the address is invalid; even if state held it, a plan would destroy it.
DTerraform creates a new instance with the same settings
Incorrect
Import never creates infrastructure.
The import command writes state only, into an address your configuration already has.
Importing an EC2 instance uses its instance ID, but importing a DNS zone uses a zone ID. Where does a practitioner find the right import ID for a resource type?
AIn terraform.tfstate of another project
BIn the Terraform CLI configuration file
CIn that resource's provider documentation
DIt is always the resource's display name in the console
Show the answer and why
AIn terraform.tfstate of another project
Incorrect
Another project's state is not a reference for import formats.
BIn the Terraform CLI configuration file
Incorrect
The CLI configuration has no import settings.
CIn that resource's provider documentation
Correct
The import ID depends on the resource type; the provider documentation describes the format.
DIt is always the resource's display name in the console
Incorrect
Formats differ by resource type and are rarely just a name.
Import IDs are provider-specific; read the resource's page.
An engineer reruns the configuration-generation step of an import, and the output file named in the command is still in the directory from the previous attempt. What happens?
ATerraform returns an error, because the output path must not exist yet
BTerraform appends the new resource blocks to the end of the existing file
CTerraform overwrites the file with fresh configuration
DTerraform writes to a new file with a numbered suffix next to the old one
Show the answer and why
ATerraform returns an error, because the output path must not exist yet
Correct
You must supply a new file path; an existing file causes an error.
BTerraform appends the new resource blocks to the end of the existing file
Incorrect
Terraform does not append to existing files.
CTerraform overwrites the file with fresh configuration
Incorrect
It refuses to overwrite.
DTerraform writes to a new file with a numbered suffix next to the old one
Incorrect
There is no automatic renaming.
Generated configuration always goes to a new file you name.
A brand-new configuration contains only import blocks for some DNS records and no other resources from the DNS provider. Generating configuration fails because Terraform cannot determine which provider to use. What is missing?
AA moved block that names the DNS provider for the imported records
BA data source for each record
CAn output that exposes each record's ID
DA provider block, followed by terraform init
Show the answer and why
AA moved block that names the DNS provider for the imported records
Incorrect
moved blocks change addresses.
BA data source for each record
Incorrect
Data sources do not tell import which provider to use.
CAn output that exposes each record's ID
Incorrect
Outputs expose values; they do not select providers.
DA provider block, followed by terraform init
Correct
If no other resources use the provider, add a provider block so Terraform knows which provider generates the configuration, and re-run init.
Configuration generation still needs a provider to ask for attribute values.