Ravindra BagaleCourses & study guides Track your progress

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

Intermediate35 minTerraform 1.6+ · AWS CLI v2 · An AWS account

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 moved and 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-identity works). Never use the root user, and never paste access keys into .tf files.
  • Region Mumbai (ap-south-1) is used; change region if 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"

  1. 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 $BUCKET
    

    What you should see: a "Location" line and the bucket name.

Part 2: Import it

  1. 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-lab
    
  2. Plan:

    terraform init
    terraform plan
    

    What you should see: aws_s3_bucket.media: Preparing import... and Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.

  3. Apply and check state:

    terraform apply
    terraform state list
    

    What you should see: Apply complete! Resources: 1 imported, 0 added and aws_s3_bucket.media.

  4. Delete the import block from main.tf (its job is done) and run terraform plan again.

    What you should see: No changes.

Part 3: Rename with moved

  1. Rename the resource to user_media and add a moved block:

    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
    }
    
  2. Plan:

    terraform plan
    

    What you should see: # aws_s3_bucket.media has moved to aws_s3_bucket.user_media and Plan: 0 to add, 0 to change, 0 to destroy.

  3. Apply so state records the new name:

    terraform apply
    terraform state list
    

    What you should see: aws_s3_bucket.user_media.

Clean up: destroy everything

  1. Delete everything this lab created. Type yes when asked:

    terraform destroy
    

    What 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

MistakeWhat happensFix
Bucket names in import and resource differPlan wants to create a second bucketUse the same name in both
Renaming without movedPlan shows destroy and createAdd a moved block
Keeping the import block foreverConfusing for the next readerRemove it after the import is applied
Provider region differs from the bucket regionCannot import non-existent remote objectMatch 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.