πŸ“Œ How Import Works

Import command:

terraform import <terraform_resource_address> <aws_resource_identifier>

After import:

  • Terraform updates terraform.tfstate
  • But *does NOT write code for you
  • You must have matching resource config in .tf files

πŸ“Œ Example β€” Import IAM Role

Terraform code:

resource "aws_iam_role" "lambda_role" {
  name = "OrderUpLambdaExecutionRole"
}

Import:

terraform import aws_iam_role.lambda_role OrderUpLambdaExecutionRole

Now Terraform knows:
β€œThis existing IAM role belongs to this resource block.”


πŸ“Œ Important Rules

βœ” Resource MUST exist in AWS
βœ” Resource MUST exist as a block in Terraform
βœ” Import ONLY updates state, not your .tf code
βœ” After import: run terraform plan


πŸ“Œ Workflow Summary

  1. Write the resource block in .tf
  2. Run terraform import
  3. Run terraform plan
  4. Fix any differences
  5. Run terraform apply