Learn how to manage multiple environments (dev, staging, production) with Terraform efficiently and consistently.

What Will You Learn? #

Managing multiple environments is a real challenge in Terraform. This section covers various strategies — from the directory-based approach to workspaces — along with the advantages and disadvantages of each.

Articles in This Section #

ArticleMain Topic
What is an Environment?The concept of environments in the IaC context
Environment TypesDev, staging, production — the characteristics and needs of each
Directory BasedUsing separate folder structures for each environment
WorkspaceUsing Terraform Workspaces for multi-environment

Environment Strategy #

flowchart TD
    subgraph Directory["Directory-Based"]
        D_ROOT["root/<br/>├── dev/<br/>├── staging/<br/>└── production/"]
        D_PRO["Explicit separation"]
        D_CON["More files to maintain"]
    end

    subgraph Workspace["Workspace-Based"]
        W_ROOT["Single config<br/>+ workspace select"]
        W_PRO["Less duplication"]
        W_CON["Hidden differences"]
    end

    subgraph Terragrunt["Terragrunt"]
        T_ROOT["DRY config<br/>+ per-env overrides"]
        T_PRO["Best of both"]
        T_CON["Extra tool"]
    end

    D_ROOT -->|"Alternative"| W_ROOT
    W_ROOT -->|"Alternative"| T_ROOT

    style Directory fill:#e3f2fd
    style Workspace fill:#e8f5e9
    style Terragrunt fill:#fff3e0

Directory-Based Structure #

flowchart TD
    subgraph Shared["Shared Modules"]
        MOD_VPC["modules/vpc/"]
        MOD_EC2["modules/ec2/"]
    end

    subgraph Dev["environments/dev/"]
        DEV_MAIN["main.tf<br/>instance_type = t3.micro"]
    end

    subgraph Prod["environments/production/"]
        PROD_MAIN["main.tf<br/>instance_type = t3.xlarge"]
    end

    MOD_VPC --> Dev
    MOD_VPC --> Prod
    MOD_EC2 --> Dev
    MOD_EC2 --> Prod

    style Shared fill:#f3e5f5
    style Dev fill:#e8f5e9
    style Prod fill:#fff3e0

Environment Comparison #

flowchart LR
    subgraph Dev["Development"]
        D_INST["t3.micro"]
        D_HA["Single AZ"]
        D_BACKUP["No backup"]
        D_MONITOR["Basic monitoring"]
    end

    subgraph Prod["Production"]
        P_INST["t3.xlarge"]
        P_HA["Multi-AZ"]
        P_BACKUP["30-day backup"]
        P_MONITOR["Full monitoring"]
    end

    Dev -->|"Promote"| Prod

    style Dev fill:#e8f5e9
    style Prod fill:#fff3e0

After understanding environments, continue to Multi Provider to learn how to use several cloud providers at once.

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