nerdexam
HashiCorp

TA-002-P · Question #156

Matt wants to import a manually created EC2 instance into terraform so that he can manage the EC2 instance through terraform going forward. He has written the configuration file of the EC2 instance…

The correct answer is D. terraform import aws_instance.matt_ec2 i-0260835eb7e9bd40. The terraform import command takes a resource address and a provider-specific resource ID as two space-separated arguments to map existing infrastructure into state.

Use the Terraform CLI (terraform plan, apply, destroy, fmt, init, validate, workspace, import, taint, providers, output)

Question

Matt wants to import a manually created EC2 instance into terraform so that he can manage the EC2 instance through terraform going forward. He has written the configuration file of the EC2 instance before importing it to Terraform. Following is the code:

resource "aws_instance" "matt_ec2" { ami = "ami-bg2640de" instance_type = "t2.micro" vpc_security_group_ids = ["sg-6ae7d613", "sg-53370035"] key_name = "mysecret" subnet_id = "subnet-9e3cfbc5" } The instance id of that EC2 instance is i-0260835eb7e9bd40 How he can import data of EC2 to state file?

Options

  • Aterraform import aws_instance.id = i-0260835eb7e9bd40
  • Bterraform import i-0260835eb7e9bd40
  • Cterraform import aws_instance.i-0260835eb7e9bd40
  • Dterraform import aws_instance.matt_ec2 i-0260835eb7e9bd40

How the community answered

(30 responses)
  • A
    10% (3)
  • B
    3% (1)
  • C
    7% (2)
  • D
    80% (24)

Why each option

The terraform import command takes a resource address and a provider-specific resource ID as two space-separated arguments to map existing infrastructure into state.

Aterraform import aws_instance.id = i-0260835eb7e9bd40

Using an equals sign between the resource address and instance ID is not valid syntax for terraform import - the two arguments must be separated by a space, not an assignment operator.

Bterraform import i-0260835eb7e9bd40

Providing only the instance ID without a resource address is invalid because Terraform cannot determine which configuration block to associate with the imported resource.

Cterraform import aws_instance.i-0260835eb7e9bd40

Joining the resource name and instance ID with a dot creates a malformed single argument - the instance ID must be a separate positional argument following the resource address.

Dterraform import aws_instance.matt_ec2 i-0260835eb7e9bd40Correct

The command terraform import aws_instance.matt_ec2 i-0260835eb7e9bd40 uses the correct syntax: the resource address (type.name) is the first argument and the real-world resource ID is the second argument separated by a space. Terraform writes the imported resource into the state file and associates it with the configuration block named matt_ec2.

Concept tested: Terraform import command syntax and resource address format

Source: https://developer.hashicorp.com/terraform/cli/commands/import

Topics

#Terraform import#Terraform CLI#State management#AWS EC2

Community Discussion

No community discussion yet for this question.

Full TA-002-P Practice