Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Providers and Resources

Resources and References

Learn the resource block in Terraform: arguments versus computed attributes, referencing other resources, implicit dependencies, timeouts, naming and the resource life cycle.

The resource block is the heart of Terraform. Every server, bucket, database, DNS record or firewall rule you manage is one resource (or many, with count and for_each). A resource declaration says what you want to exist; Terraform's job is to make that true and to keep it true. This lesson covers the anatomy of a resource, the difference between what you set and what the provider returns, and how references between resources create the web that Terraform turns into an ordered plan.

All HCL is read-only here; the stand-ins imitate attribute resolution and schema checks.

Anatomy of a resource

HCL
resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
  subnet_id     = aws_subnet.main.id

  tags = {
    Name = "web"
  }
}
  • aws_instance is the resource type. The prefix (aws) selects the provider; the rest says which kind of object.
  • web is the local name, unique per type within the module.
  • Inside the braces are arguments (what you configure), possibly nested blocks, and meta-arguments that Terraform itself understands: count, for_each, depends_on, provider, lifecycle.

Documentation for every resource lives on the provider's registry page and is split into Argument Reference (what you may set) and Attribute Reference (what you can read).

Arguments versus attributes

Arguments are inputs you write. Attributes are values you can read, including every argument and also values the platform decides, such as id, arn, public_ip or private_ip. The latter are computed attributes: they only exist after creation.

HCL
output "web_ip" {
  value = aws_instance.web.public_ip   # computed: known after apply
}

A few attributes are optional and computed: if you omit them the provider picks a value (a default VPC, a generated name) and records it in state. If you later omit them in config, Terraform does not change what exists.

Python Arguments, computed attributes and a schema check (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The messages mimic the real ones: Terraform tells you when a required argument is missing, when an argument does not exist, and when you try to set a value that only the platform may decide.

References and implicit dependencies

Whenever one resource uses another's attribute, Terraform records a dependency:

HCL
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "app" {
  vpc_id     = aws_vpc.main.id        # reference: subnet depends on vpc
  cidr_block = "10.0.1.0/24"
}

resource "aws_instance" "web" {
  subnet_id = aws_subnet.app.id       # instance depends on subnet
}

You never write the order. Terraform sees aws_vpc.main.id inside aws_subnet.app, so the VPC is created first, and its id, unknown at plan time, flows into the subnet once the VPC exists. This works for any expression that contains a reference: arguments, interpolations, function calls, even count. Chains like the one above form the graph you saw in How Terraform Works.

Python Resolving references during an apply (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The IDs (vpc-001 and so on) do not exist until each resource is created; the subnet receives the VPC's ID only then. That is the "known after apply" flow in miniature.

Reading attributes safely

You may read any attribute with resource_type.name.attribute. For nested attributes use dots and indexes: aws_instance.web.root_block_device[0].volume_id. The full set of attributes is visible in the state: terraform state show aws_instance.web.

Do not reference a resource's own attributes inside itself (self is only available in provisioners). Do not create cycles: if A uses B and B uses A, Terraform reports Error: Cycle. Break the cycle by moving one of the values into a third resource (for example a separate aws_security_group_rule).

What a resource goes through

Terraform manages each resource through the same life cycle:

  1. Create when the block is new.
  2. Read (refresh) on every plan.
  3. Update in place when only mutable arguments changed.
  4. Replace (destroy and create) when an immutable argument changed.
  5. Delete when you remove the block or run destroy.

Resources can have timeouts for slow operations:

HCL
resource "aws_db_instance" "main" {
  # ...
  timeouts {
    create = "60m"
    delete = "30m"
  }
}

Naming resources

Choose local names with care, because renaming a block changes its address, and to Terraform a new address looks like a new resource: it plans to destroy the old object and create a new one. If you want to rename without recreating, tell Terraform with a moved block:

HCL
moved {
  from = aws_instance.web
  to   = aws_instance.frontend
}

Terraform then updates the state address instead of replacing the real server. Keep moved blocks until every environment has applied them. The State chapter covers refactoring more.

Importing existing objects

If an object already exists (created by hand, or by another team), you can adopt it into state with an import block:

HCL
import {
  to = aws_s3_bucket.legacy
  id = "company-legacy-bucket"
}

resource "aws_s3_bucket" "legacy" {
  bucket = "company-legacy-bucket"
}

terraform plan shows the import and any differences between your configuration and the real object. Apply, and the bucket is now managed.

Resource behavior you should know

  • Idempotent reads. Terraform refreshes from the real world, so manual changes show up as drift in the plan.
  • Sensitive output. Resource attributes like generated passwords land in state unencrypted; see sensitive values.
  • Provider differences. The same idea (a virtual machine) has different arguments on AWS, Azure and GCP. Terraform unifies the workflow, not the resource models.
  • Eventual consistency. Clouds are sometimes slow; some resources need depends_on or a time_sleep to wait.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is public_ip on aws_instance.web?

    Attributes like ids and addresses are decided by the platform, so they are only known after apply.
  2. 2. How does Terraform learn that a subnet must be created after its VPC?

    A reference to another resource's attribute creates an implicit dependency edge in the graph.
  3. 3. You rename resource "aws_instance" "web" to "frontend" with no other change. What does the plan show without a moved block?

    A new address looks like a new resource. A moved block tells Terraform it is the same object.
  4. 4. Where do you read the list of attributes a resource exposes?

    Provider docs list both arguments and attributes. After apply, terraform state show lists every recorded attribute.

Summary

  • A resource block has a type, a local name, arguments, nested blocks and meta-arguments.
  • Arguments are inputs; attributes include computed values such as id and arn.
  • References between resources create implicit dependencies, and so the creation order.
  • Renaming a block changes its address; use moved to avoid replacement and import to adopt existing objects.
  • Terraform creates, reads, updates in place, replaces or deletes depending on the diff.

Next: Dependencies and the Resource Graph