Introduction
You may automatically generate Terraform configuration using Plannable Imports as documented here. Care must be taken to understand that the automatically generated configuration still needs manual review prior to confirming a terraform apply
.
Scenario
You wish to automatically generate the Terraform configuration by using
terraform plan generate-config-out=generated_resources.tf
in conjunction with an import block such as the following:
import {
to = aws_instance.ec2
id = "i-REDACTED"
}
Recommendation
Be sure to review the automatically generated configuration for accuracy as well as any warnings produced in the console from this experimental feature of Terraform.
For example, sample output such as the following should be pruned to avoid reliance on otherwise-default values that Terraform may normally supply in the absence of an optional attribute being specified.
resource "aws_instance" "ec2" {
ami = "ami-REDACTED"
associate_public_ip_address = true
availability_zone = "us-east-1c"
disable_api_stop = false
disable_api_termination = false
ebs_optimized = false
get_password_data = false
hibernation = false
host_id = null
# generated configuration continues but is omitted for brevity
In addition, pay particular attention to the plan generated by a subsequent terraform plan
or terraform apply
operation to ensure that the generated configuration and the resulting difference or lack of difference between your current state and desired state are as wanted, prior to approving Terraform to go forward with any apply.
Additional Information
Please refer to the official documentation on auto-generated configuration via Plannable Imports here.