Lesson 6 of 14 · 16m
Renaming Without Deleting: moved, import, removed
Terraform tracks resources by address, not name — `moved`, `import`, and `removed` blocks refactor state through code review instead of destroying infrastructure.
Terraform doesn't track your resources by name — it tracks them by address in state. Rename aws_instance.web to aws_instance.app in your HCL and run plan, and Terraform reports that aws_instance.web will be destroyed and aws_instance.app created. From its view, one object vanished and an unrelated new one appeared. The machine doesn't know it's the same EC2 instance; the data on it doesn't matter to the diff.
That's the wrong mental model — "the name is just a label" — and it deletes databases. Since Terraform 1.1 there are three code-side blocks for telling Terraform that an address changed or that reality changed, plus the older imperative state commands for the same surgery. Which one you reach for is about auditability.
moved: refactoring that survives code review
moved {
from = aws_instance.web
to = aws_instance.app
}A moved block in your config tells the next apply "the resource at the old address is the resource at the new address" — Terraform rewrites the address in state instead of destroying and recreating. It works for moves into modules too: to = module.app.aws_instance.web is how you extract a pile of loose resources into a module without downtime.
The block is part of the code, which is the point — it shows up in the PR, it's reviewable, and after the apply lands you can delete it or leave it as history. The equivalent terraform state mv aws_instance.web aws_instance.app does the same surgery imperatively: faster for a one-off fix on your own state, invisible to anyone reviewing what you did.
import: adopting infrastructure that already exists
import {
to = aws_s3_bucket.legacy_logs
id = "legacy-logs-bucket-name"
}import blocks (Terraform 1.5+) adopt resources Terraform doesn't manage — the clickops bucket from 2019, the security group an engineer made during an incident. Run plan and Terraform generates the diff to bring the resource under management; pair it with terraform plan -generate-config-out=generated.tf and it writes candidate HCL for the imported resources that you refine into real code.
The older terraform import aws_s3_bucket.legacy_logs legacy-logs-bucket-name works identically but leaves no code trail — the config has to already exist and match, and the adoption itself happens outside review. For a migration project with dozens of resources, blocks plus -generate-config-out is the auditable path; for one resource at 2 a.m., the CLI is fine.
removed: handing a resource back
removed {
from = aws_instance.legacy
lifecycle {
destroy = false
}
}removed blocks (Terraform 1.7+) are the inverse of import: drop the resource from state without touching the real object. The EC2 instance keeps running; Terraform just forgets it exists. This is the clean answer to "another team is taking this over" or "we're managing this manually now" — the handoff is declared in code, applied once, and the resource walks away alive.
terraform state rm aws_instance.legacy does the same thing imperatively. Same tradeoff: instant, but unreviewed.
The decision rule
Both paths change the same state file. The code blocks are for changes your team needs to see and reproduce — refactors, migrations, ownership transfers. The state subcommands are surgical tools for incidents — a broken apply, a corrupted address, an emergency detach. The failure mode to avoid is mixing them carelessly: a state mv nobody recorded followed by a moved block for the same resource produces a plan nobody predicted. Pick one mechanism per change and let it be the only thing mutating addresses that day.
Field notes
- After
moved, verify withplan— the correct result is "no changes," not a small diff. A diff means the addresses didn't match what you assumed. - Imported resources need real config — Terraform won't invent the arguments;
-generate-config-outgets you a draft, and the draft usually contains attributes you should turn into variables. removed { destroy = false }plusterraform importin the same project is the full bidirectional handshake for resources crossing ownership boundaries.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.
What is Infrastructure as a Service? The Layer That Makes Everything Else Possible
Infrastructure as a Service gives you virtualized servers, storage, and networking on demand without owning any hardware. You rent compute and own everything that runs on it. Here is what IaaS actually is, what it demands from you.
What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"
Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.
The World now is Serverless and Container based
A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,