📌 How Import Works

Import command

terraform import <terraform_resource_address> <aws_resource_identifier>

Important Rules

  • Resource MUST exist in AWS
  • Resource MUST exist as a block in Terraform

After import

  • Terraform updates terraform.tfstate
  • But does NOT write code for you in .tf
  • You must add matching resource config/attributes in .tf file

📌 Import IAM Role — Steps

  1. Write the resource block in .tf

    • Ensure name matches AWS resource ✔

      resource "aws_iam_role" "lambda_role" {
           name = "OrderUpLambdaExecutionRole"
      }
  2. Run terraform import

    • Maps existing AWS resource to state ✔

      terraform import aws_iam_role.lambda_role OrderUpLambdaExecutionRole
  3. Run terraform plan

    • Terraform compares real infra ⇄ state + config ✔

      terraform plan
  4. Fix the Drift

    • Fix any differences
    • Update .tf until no unexpected drift ✔
  5. Run terraform apply

    • Sync final desired config → infra + state ✔

      terraform apply