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.
terraform --version
hcp login
mkdir my-terraform-project && cd my-terraform-project
terraform stack init
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.
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`
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"
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 a
orchestration.tf` file:
terraform
orchestration {
stack = "network"
}
Deferred changes allow you to queue a plan to run only when inputs stabilize, reducing churn in PR-driven workflows.
terraform stack apply --defer
terraform stack apply --no-defer
terraform migrate
tool to move state into a Stack.remote_state
data sources.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.
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!