How it works
The explanation, with examples you can run.
moved: rename without rebuilding
resource "random_pet" "service" { # was "name" prefix = var.env} moved { from = random_pet.name to = random_pet.service}The plan (Terraform 1.5.7) shows only the move:
Terraform will perform the following actions: # random_pet.name has moved to random_pet.service resource "random_pet" "service" { id = "dev-known-woodcock" # (3 unchanged attributes hidden) } Plan: 0 to add, 0 to change, 0 to destroy.moved works for resources, modules (module.old to module.new) and instances (aws_instance.web to aws_instance.web[0]). Keep moved blocks for a while after applying, especially in shared modules, so every caller's state gets moved.
import: adopt what already exists
import { to = random_integer.port id = "8443,8000,8999"} resource "random_integer" "port" { min = 8000 max = 8999}random_integer.port: Preparing import... [id=8443,8000,8999]random_integer.port: Refreshing state... [id=8443] # random_integer.port will be imported resource "random_integer" "port" { id = "8443" max = 8999 min = 8000 result = 8443 } Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.The import ID format is defined by each resource type (a bucket name, an instance ID, or for random_integer, result,min,max). If you do not want to write the resource block yourself, run terraform plan -generate-config-out=generated.tf with only the import block, then tidy the generated code. Aim for an import plan with no changes besides the import itself: any ~ line means your configuration does not yet match reality.
removed: let go without destroying
removed { from = random_password.db lifecycle { destroy = false }}Terraform 1.16.6 plans:
# random_password.db will no longer be managed by Terraform, but will not be destroyed # (destroy = false is set in the configuration)Use it to hand a resource to another configuration (which then imports it), or to stop managing something that should outlive this code.
When to use the CLI instead
terraform state mv, terraform state rm and terraform import do the same jobs immediately, without a plan or review. They are fine for one-off repairs on a laptop with a state backup, but blocks are better in teams and CI: the change is in Git, visible in the plan, and applied with everything else.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.