Labs · DevOps
Lab: Bring a Hand-Made S3 Bucket Under Terraform with an import Block, Then Rename the Resource with a moved Block Without Re-creating It
Course: DevOps · Chapter 37: Terraform Import Moved and Removed Blocks · Lesson: 37.1 import block: adopt a resource built by hand
Chapter 37 explains import, moved and removed blocks; this lab uses the first two on one bucket.
Chala mitrano! Console madhe haatane banavlela resource Terraform madhe aanaycha aahe? Delete nako, import kara.
Suppose we are…
Suppose we are on an Instagram-style team, and last year someone created the media bucket by hand in the console. Now the team wants everything in Terraform, but deleting and re-creating the bucket would lose data.
In this lab we create a bucket with the CLI (playing the "by hand" part), adopt it with an import block, then rename the Terraform resource with a moved block. The real bucket is never touched.
Goal of this lab
By the end you will have:
- A plan that says
1 to import, 0 to add. - The bucket in state without being re-created.
- A rename with
movedand a plan with no destroy.
What you need
- Terraform 1.6 or newer (
terraform version). - AWS CLI v2 signed in to your own AWS account with an IAM user or SSO role that may create these resources (
aws sts get-caller-identityworks). Never use the root user, and never paste access keys into .tf files. - Region Mumbai (
ap-south-1) is used; changeregionif you prefer another. - 35 minutes.
Cost and cleanup
One empty S3 bucket costs almost nothing. Small charges are possible even on the free tier. Finish the lab in one sitting and always run the Clean up part at the end.
Part 1: Make a bucket "by hand"
Create the bucket with the CLI (your account id makes the name unique):
ACC=$(aws sts get-caller-identity --query Account --output text) BUCKET=badge-handmade-$ACC aws s3api create-bucket --bucket $BUCKET --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1 echo $BUCKETWhat you should see: a
"Location"line and the bucket name.
Part 2: Import it
Create a folder and
main.tf(put your bucket name in both places):main.tf
terraform { required_version = ">= 1.6" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = "ap-south-1" } import { to = aws_s3_bucket.media id = "badge-handmade-<your-account-id>" } resource "aws_s3_bucket" "media" { bucket = "badge-handmade-<your-account-id>" }mkdir tf-import-lab && cd tf-import-labPlan:
terraform init terraform planWhat you should see:
aws_s3_bucket.media: Preparing import...andPlan: 1 to import, 0 to add, 0 to change, 0 to destroy.Apply and check state:
terraform apply terraform state listWhat you should see:
Apply complete! Resources: 1 imported, 0 addedandaws_s3_bucket.media.Delete the
importblock frommain.tf(its job is done) and runterraform planagain.What you should see:
No changes.
Part 3: Rename with moved
Rename the resource to
user_mediaand add amovedblock:main.tf (resource part)
resource "aws_s3_bucket" "user_media" { bucket = "badge-handmade-<your-account-id>" } moved { from = aws_s3_bucket.media to = aws_s3_bucket.user_media }Plan:
terraform planWhat you should see:
# aws_s3_bucket.media has moved to aws_s3_bucket.user_mediaandPlan: 0 to add, 0 to change, 0 to destroy.Apply so state records the new name:
terraform apply terraform state listWhat you should see:
aws_s3_bucket.user_media.
Clean up: destroy everything
Delete everything this lab created. Type
yeswhen asked:terraform destroyWhat you should see:
Destroy complete! Resources: 1 destroyed.The bucket is empty, so it deletes cleanly.
Ravindra Bagale's Tip
Try the rename once without the moved block and read the plan: 1 to add, 1 to destroy. That one line in the plan is the difference between a rename and data loss.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Bucket names in import and resource differ | Plan wants to create a second bucket | Use the same name in both |
Renaming without moved | Plan shows destroy and create | Add a moved block |
| Keeping the import block forever | Confusing for the next reader | Remove it after the import is applied |
| Provider region differs from the bucket region | Cannot import non-existent remote object | Match the provider region to the bucket |
Self-check checklist
0 of 4 done
Try-at-home challenge
Create a second bucket with the CLI and write only an import block (no resource block). Run terraform plan -generate-config-out=generated.tf and read the code Terraform writes for you.
Samajla ka? import to adopt, moved to rename, never delete to fix a name. Aata pudhe jaauya drift.