๐Ÿ“Œ 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