When should you write Terraform configuration files for existing infrastructure that you want to start managing with Terraform?
A. Before you run terraform import.
You cannot import the resource unless the resource's basic block is not coded in the .tf file. Hence this is the 1st step. Once a basic block is written, you import the resources and enhance the code using the state details.
<b> Before you run terraform import </b>
Please refer to this link https://developer.hashicorp.com/terraform/cli/import/usage
It is said:
"To import a resource, first write a resource block for it in your configuration, establishing the name by which it will be known to Terraform
...
Now terraform import can be run to attach an existing instance to this resource configuration"
"To import a resource, first write a resource block for it in your configuration, establishing the name by which it will be known to Terraform"
Answer is A.
https://developer.hashicorp.com/terraform/cli/import/usage
1. Run terraform import to bring the existing resources under Terraform management. This command allows you to import the existing resources into the Terraform state without the corresponding Terraform configuration files.
2.After importing the resources, you should write the Terraform configuration files to define and manage the imported resources. These configuration files specify the desired state of the infrastructure and allow you to make changes to it in a controlled manner.
Condidering and existing infrastructurre on a cloud provider, in order to manage these resources on terraform you need:
1. foreach resource, add at least the resources definition using resource_type.resource_name {}
2. foreach resource, use terraform import resource_type.resource_name <id>
D:
You can use the import command to migrate existing resources into your Terraform state file. The import command does not currently generate the configuration for the imported resource, so you must write the corresponding configuration block to map the imported resource to it.
Importing infrastructure involves five steps:
1 Identify the existing infrastructure you will import.
2 Import infrastructure into your Terraform state file.
3 Write Terraform configuration that matches that infrastructure.
4 Review the Terraform plan to ensure the configuration matches the expected state and infrastructure.
5 Apply the configuration to update your Terraform state.
Source: https://developer.hashicorp.com/terraform/tutorials/state/state-import
Answer is A.
Source: https://developer.hashicorp.com/terraform/tutorials/state/state-import. Has been updated with new steps for Terraform v1.5
Using configuration to import resources involves the following steps:
Identify the existing infrastructure you will import.
Define an import block for the resources.
Run terraform plan to review the import plan and optionally generate configuration for the resources.
Prune generated configuration to only the required arguments.
Apply the configuration to bring the resource into your Terraform state file.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
Ni33
Highly Voted 1 year, 6 months agoblop213
Most Recent 6 months, 3 weeks agoTricejer
9 months, 3 weeks agoBlitz123
9 months, 3 weeks agouax
1 year agoAdriBFK
1 year, 2 months agoarunrkaushik
1 year, 3 months agoseifskl
1 year, 4 months agolukacs16
1 year, 4 months agoForam31
1 year, 5 months agoyubac
1 year, 5 months agoSydurrahman12
1 year, 6 months agosdm13168
1 year, 6 months agoSathisgm
1 year, 6 months agoMarshalLaw
1 year, 6 months ago[Removed]
1 year, 4 months agojoyboy23
1 year, 4 months agodbrosowskiCW
1 year, 6 months agoOleg_gol
1 year, 6 months ago