Course contentsShow
← Cloud and Infrastructure
Lesson 729 of 2,98914. Terraform FundamentalsFree lesson

Resource Blocks and Meta-Arguments

Defining resources with type and name, and understanding common meta-arguments like depends_on and count.

Resource Blocks and Meta-Arguments

What you'll learn: How to define infrastructure resources in Terraform and use meta-arguments to control their behavior.

Understanding Resource Blocks

A resource block is where you actually create infrastructure in Terraform. Every resource has two identifiers: a type and a name. The type tells Terraform what kind of infrastructure to create (like an AWS EC2 instance or Azure storage account), and the name is your local label for referring to it within your configuration.

The basic syntax looks like this:

resource "resource_type" "resource_name" {
  # Configuration arguments go here
}

Think of the resource type as the "what" (what kind of thing) and the name as the "who" (who you're talking about in your code). For example:

resource "aws_instance" "web_server" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

Here, aws_instance is the type and web_server is the name you chose.

Common Meta-Arguments

Meta-arguments are special Terraform keywords that work across all resource types to control behavior:

  • depends_on: Forces Terraform to create one resource before another, even if there's no automatic dependency. Useful when relationships aren't obvious from the code.

  • count: Creates multiple copies of a resource. Instead of writing the same block three times, set count = 3 and Terraform handles it.

resource "aws_instance" "workers" {
  count         = 3
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

Key Takeaway: Resource blocks define infrastructure using a type and name, while meta-arguments like depends_on and count give you fine-grained control over how Terraform creates and manages those resources.