📌 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
.tffile
📌 Import IAM Role — Steps
-
Write the resource block in
.tf-
Ensure
namematches AWS resource ✔resource "aws_iam_role" "lambda_role" { name = "OrderUpLambdaExecutionRole" }
-
-
Run
terraform import-
Maps existing AWS resource to state ✔
terraform import aws_iam_role.lambda_role OrderUpLambdaExecutionRole
-
-
Run
terraform plan-
Terraform compares real infra ⇄ state + config ✔
terraform plan
-
-
Fix the Drift
- Fix any differences
- Update
.tfuntil no unexpected drift ✔
-
Run
terraform apply-
Sync final desired config → infra + state ✔
terraform apply
-