Looks like you're stuck. Need a hand?

Share This Tutorial

Views 36

Terraform 2.0 (Stacks public beta)

Date  |  Category Programming
...
...
Back Back

Getting Started with Terraform 2.0 (Stacks Public Beta)

Terraform 2.0 introduces significant improvements, with Stacks being the most notable feature. This tutorial guides you through the key features and how to get started with Terraform 2.0.

Prerequisites

  1. Install Terraform 2.0Ensure you have Terraform 2.0 installed. You can verify the version using: terraform --version
  2. Sign Up for HashiCorp Cloud Platform (HCP)
  3. Enable Stacks Beta
    • Log in to your HCP account.
    • Navigate to the Terraform Stacks page and enable the beta feature.

Step 1: Initialize Terraform Stacks

  1. Authenticate with HCPRun the following commands to authenticate with HCP: hcp login
  2. Initialize a Terraform StackCreate a new directory for your Terraform project and initialize a stack: mkdir my-terraform-project && cd my-terraform-project terraform stack init

Step 2: Understanding Stacks

What is a Stack?

A Stack is an opinionated abstraction over Workspaces. It groups modules, variables, and state into a single unit, making it easier to manage infrastructure as code.

Key Benefits of Stacks

Step 3: Managing Infrastructure with Stacks

Creating Your First Stack

  1. Define InfrastructureCreate a main.tf file with your infrastructure definition:

    ```terraform provider "aws" { region = "us-west-2" }

    resource "aws_instance" "example" { ami = "ami-abc123" instance_type = "t2.micro" } `` 2. **Initialize and Apply**Run the following commands to initialize and apply your configuration:terraform stack apply`

Using Variables in Stacks

  1. Define VariablesCreate a variables.tf file to define input variables:

    terraform variable "instance_type" { type = string default = "t2.micro" description = "The type of instance to start" } 2. Pass Variables at Apply TimeUse variables when applying your stack: terraform stack apply -var instance_type="t3.micro"

Step 4: Handling Dependencies with Orchestration Rules

Cross-Stack Dependencies

  1. Define DependenciesUse depends_on to specify dependencies between stacks:

    ```terraform resource "aws_instance" "example" { ami = "ami-abc123" instance_type = "t2.micro"

    depends_on = [ aws_security_group.example ] } `` 2. **Orchestration Rules**Terraform 2.0 introduces orchestration rules to manage the order of operations. Define rules in aorchestration.tf` file:

    terraform orchestration { stack = "network" }

Step 5: Deferred Changes

What Are Deferred Changes?

Deferred changes allow you to queue a plan to run only when inputs stabilize, reducing churn in PR-driven workflows.

Using Deferred Changes

  1. Queue a Planterraform stack apply --defer
  2. Apply Deferred ChangesWhen inputs stabilize, apply the changes: terraform stack apply --no-defer

Step 6: Free Tier and Migration

Free Tier

Migrating to Stacks

  1. Existing Workspaces
    • Existing cloud or CLI workspaces will continue to work.
    • Use the terraform migrate tool to move state into a Stack.
  2. Refactor Modules
    • Refactor modules to use outputs and variables instead of remote_state data sources.

Step 7: Waypoint Integration

Golden Workflows

Waypoint is now Generally Available (GA) and integrates seamlessly with Terraform Stacks. It provides golden workflow templates that tie build, test, and deploy phases to the stack lifecycle.

Step 8: Licensing and Open Source

  1. Licensing
    • Terraform 2.0 is still under the Business Source License (BSL).
    • Open-source forks (e.g., OpenTofu) are unaffected but cannot consume the Stacks API.

Step 9: Getting Help

  1. HashiCorp Community
  2. HCP Documentation

By following this tutorial, you've successfully started working with Terraform 2.0 and Stacks. Explore more advanced features and best practices to fully leverage the power of Terraform 2.0!