Delve into defining resources, managing lifecycles, and handling dependencies between resources. This is the core of writing Terraform configurations.

What Will You Learn? #

Resources are the most important configuration blocks in Terraform — each resource represents one infrastructure object. Understanding how to define resources, manage dependencies between resources, control lifecycles, and use locking are fundamental skills.

Articles in This Section #

ArticleMain Topic
What is a Resource?The concept of resources as the basic unit of managed infrastructure
DependencyManaging creation order and dependencies between resources
LifecycleControlling resource behavior during create, update, and destroy
OperationCreate, Read, Update, Delete (CRUD) — resource lifecycle operations
LockingPreventing concurrent modification of the state file

Dependency Visualization #

flowchart TD
    subgraph Network Layer
        VPC["AWS VPC"]
        Subnet["Subnet"]
        SG["Security Group"]
    end

    subgraph Compute Layer
        EC2["EC2 Instance"]
        ASG["Auto Scaling Group"]
        ALB["Application LB"]
    end

    subgraph Data Layer
        RDS["RDS Database"]
        S3["S3 Bucket"]
    end

    VPC --> Subnet
    Subnet --> EC2
    Subnet --> ALB
    Subnet --> RDS
    SG --> EC2
    SG --> ALB
    SG --> RDS
    ALB --> ASG
    ASG --> EC2

    style Network Layer fill:#e3f2fd
    style Compute Layer fill:#e8f5e9
    style Data Layer fill:#fff3e0

Resource Lifecycle #

flowchart LR
    Create["Create<br/>Creation"] --> Read["Read<br/>Reading"]
    Read --> Update["Update<br/>Change"]
    Update --> Destroy["Destroy<br/>Deletion"]
    Destroy -.->|Recreate| Create

    style Create fill:#e8f5e9
    style Read fill:#e3f2fd
    style Update fill:#fff3e0
    style Destroy fill:#ffebee

Meta-Argument Lifecycle #

resource "aws_instance" "web" {
  ami           = "ami-12345"
  instance_type = "t3.micro"

  lifecycle {
    create_before_destroy = true   # Create new before deleting old
    prevent_destroy       = false  # Protect from destroy
    ignore_changes        = [tags] # Ignore tag changes
    replace_triggered_by  = [var.ami_id]
  }
}

After understanding resources, continue to State to learn how Terraform tracks resources, or to Variable to learn about parameterization.

About | Author | Content Scope | Editorial Policy | Privacy Policy | Disclaimer | Contact