Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Resources  ›  Lesson

How Resources Behave

Resource Behavior 12 minFor each resource Terraform compares configuration with state and chooses one action: create it if it is only in the configuration, destroy it if it is only in state, update it in place if a changeable argument differs, or replace it if a changed argument cannot be updated.

How Resources Behave

A resource block is a promise: "this thing should exist, like this." Every time you plan, Terraform checks that promise against what it built before and picks an action for each resource. There are only four, and you can predict all of them.

The rule

Only in config: create. Only in state: destroy. Changed: update, or replace if it cannot bend.

Crumb's two resources:

resource "aws_s3_bucket" "assets" {
  bucket = "crumb-prod-assets"
}

resource "aws_instance" "web" {
  ami           = "ami-0123456789abcdef0"
  instance_type = "t3.micro"
}

Each resource has an address, aws_instance.web, made of its type and local name. The address is how state remembers which real object belongs to which block. The type's prefix, aws_, tells Terraform which provider manages it.

For each address, Terraform compares configuration with state (refreshed from the real cloud):

Situation Action
In configuration, not in state create
In state, not in configuration destroy
In both, an updatable argument differs update in place
In both, an argument differs that cannot change on a live object replace: destroy, then create
In both, no difference nothing

Whether an argument can be updated is decided by the provider. Changing instance_type can be done on an existing server; changing its ami cannot, so the plan marks that argument with # forces replacement.

Resources also expose attributes you did not set, such as id or public_ip, which the provider fills in after creation.

One surprise: renaming the local name, from web to server, changes the address. Terraform sees one address vanish and a new one appear, so it plans destroy and create. A moved block tells Terraform it is the same object under a new name.

The shortcut

Ask two questions per resource: "Where does it exist: config, state, or both?" and, if both, "Can the changed argument bend?"

Predicting Crumb's next planStep 1 of 4
  1. New bucket block for logs

    Config only. Create.

The trap: assuming every edit is an in-place update. Always look for forces replacement before you apply, because replacing a server loses anything stored only on it.

Drills

Answer fast — trust the rule.

quick drill

A resource is in state but its block was deleted from the configuration. What does the plan do?

quick drill

What decides whether a changed argument is updated in place or forces replacement?

quick drill

You rename resource "aws_instance" "web" to "server" and change nothing else. What does the plan show, without a moved block?

quick drill

Which of these is an attribute the provider fills in rather than an argument you set?

Remember

  • Config only: create. State only: destroy. Both and changed: update, or replace if it cannot bend.
  • Shortcut: where does it exist, and can the change bend?
  • Trap: renaming a block changes its address; look out for forces replacement.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPredict the action

Describe what Terraform will do to Crumb's resources in each case, and why: a brand-new aws_s3_bucket block is added; the web server's instance_type changes; the web server's ami changes; the whole aws_instance.web block is deleted.