Terraform Advanced — generate-config-out

less than 1 minute read

Let Terraform auto-generate the resource config from an import — no more guessing attributes.


The Workflow

# Step 1: Write import block without resource block
import {
  to = aws_s3_bucket.legacy
  id = "my-existing-bucket"
}
# Step 2: Generate config
$ terraform plan -generate-config-out=generated.tf

# Terraform reads the actual resource from AWS
# and writes the HCL to generated.tf
# generated.tf (auto-generated)
resource "aws_s3_bucket" "legacy" {
  bucket              = "my-existing-bucket"
  force_destroy       = false
  object_lock_enabled = false
  tags                = {}
  tags_all            = {}
}
# Step 3: Review and clean up generated.tf
# Remove computed-only attributes, add descriptions

# Step 4: Apply to import
$ terraform apply

Limitations

  • Generated code is verbose (includes all attributes, even defaults)
  • Some attributes are read-only and must be removed manually
  • Nested blocks may need restructuring
  • Not all providers support full config generation

💡 Production Tip: Treat generated config as a starting point. Always review and refactor before committing.


What’s Next?

Import hundreds of resources at scale.

← Import Block (HCL) Bulk Import Strategies →

Updated: