$npx -y skills add hashicorp/agent-skills --skill terraform-stacksComprehensive guide for working with HashiCorp Terraform Stacks. Use when creating, modifying, or validating Terraform Stack configurations (.tfcomponent.hcl, .tfdeploy.hcl files), working with stack components and deployments from local modules, public registry, or private regis
| 1 | # Terraform Stacks |
| 2 | |
| 3 | Terraform Stacks simplify infrastructure provisioning and management at scale by providing a configuration layer above traditional Terraform modules. Stacks enable declarative orchestration of multiple components across environments, regions, and cloud accounts. |
| 4 | |
| 5 | ## Core Concepts |
| 6 | |
| 7 | **Stack**: A complete unit of infrastructure composed of components and deployments that can be managed together. |
| 8 | |
| 9 | **Component**: An abstraction around a Terraform module that defines infrastructure pieces. Each component specifies a source module, inputs, and providers. |
| 10 | |
| 11 | **Deployment**: An instance of all components in a stack with specific input values. Use deployments for different environments (dev/staging/prod), regions, or cloud accounts. |
| 12 | |
| 13 | **Stack Language**: A separate HCL-based language (not regular Terraform HCL) with distinct blocks and file extensions. |
| 14 | |
| 15 | ## File Structure |
| 16 | |
| 17 | Terraform Stacks use specific file extensions: |
| 18 | |
| 19 | - **Component configuration**: `.tfcomponent.hcl` |
| 20 | - **Deployment configuration**: `.tfdeploy.hcl` |
| 21 | - **Provider lock file**: `.terraform.lock.hcl` (generated by CLI) |
| 22 | |
| 23 | All configuration files must be at the root level of the Stack repository. HCP Terraform processes all files in dependency order. |
| 24 | |
| 25 | ### Recommended File Organization |
| 26 | |
| 27 | ``` |
| 28 | my-stack/ |
| 29 | ├── .terraform-version # The required Terraform version for this Stack |
| 30 | ├── variables.tfcomponent.hcl # Variable declarations |
| 31 | ├── providers.tfcomponent.hcl # Provider configurations |
| 32 | ├── components.tfcomponent.hcl # Component definitions |
| 33 | ├── outputs.tfcomponent.hcl # Stack outputs |
| 34 | ├── deployments.tfdeploy.hcl # Deployment definitions |
| 35 | ├── .terraform.lock.hcl # Provider lock file (generated) |
| 36 | └── modules/ # Local modules (optional - only if using local modules) |
| 37 | ├── s3/ |
| 38 | └── compute/ |
| 39 | ``` |
| 40 | |
| 41 | **Note**: The `modules/` directory is only required when using local module sources. Components can reference modules from: |
| 42 | - Local file paths: `./modules/vpc` |
| 43 | - Public registry: `terraform-aws-modules/vpc/aws` |
| 44 | - Private registry: `app.terraform.io/<org-name>/vpc/aws` |
| 45 | - Git: `git::https://github.com/org/repo.git//path?ref=v1.0.0` |
| 46 | |
| 47 | HCP Terraform processes all `.tfcomponent.hcl` and `.tfdeploy.hcl` files in dependency order. |
| 48 | |
| 49 | ## Required Terraform version (.terraform-version) |
| 50 | |
| 51 | Use Terraform v1.13.x or later to access the Stacks CLI plugin and to run |
| 52 | terraform stacks CLI commands. Begin by adding a .terraform-version file to |
| 53 | your Stack's root directory to specify the Terraform version required for your |
| 54 | Stack. For example, the following file specifies Terraform v1.14.5: |
| 55 | |
| 56 | ``` |
| 57 | 1.14.5 |
| 58 | ``` |
| 59 | |
| 60 | ## Component Configuration (.tfcomponent.hcl) |
| 61 | |
| 62 | ### Variable Block |
| 63 | |
| 64 | Declare input variables for the Stack configuration. Variables must define a `type` field and do not support the `validation` argument. |
| 65 | |
| 66 | ```hcl |
| 67 | variable "aws_region" { |
| 68 | type = string |
| 69 | description = "AWS region for deployments" |
| 70 | default = "us-west-1" |
| 71 | } |
| 72 | |
| 73 | variable "identity_token" { |
| 74 | type = string |
| 75 | description = "OIDC identity token" |
| 76 | ephemeral = true # Does not persist to state file |
| 77 | } |
| 78 | |
| 79 | variable "instance_count" { |
| 80 | type = number |
| 81 | nullable = false |
| 82 | } |
| 83 | ``` |
| 84 | |
| 85 | **Important**: Use `ephemeral = true` for credentials and tokens (identity tokens, API keys, passwords) to prevent them from persisting in state files. Use `stable` for longer-lived values like license keys that need to persist across runs. |
| 86 | |
| 87 | ### Required Providers Block |
| 88 | |
| 89 | ```hcl |
| 90 | required_providers { |
| 91 | aws = { |
| 92 | source = "hashicorp/aws" |
| 93 | version = "~> 6.0" |
| 94 | } |
| 95 | random = { |
| 96 | source = "hashicorp/random" |
| 97 | version = "~> 3.5.0" |
| 98 | } |
| 99 | } |
| 100 | ``` |
| 101 | |
| 102 | ### Provider Block |
| 103 | |
| 104 | Provider blocks differ from traditional Terraform: |
| 105 | |
| 106 | 1. Support `for_each` meta-argument |
| 107 | 2. Define aliases in the block header (not as an argument) |
| 108 | 3. Accept configuration through a `config` block |
| 109 | |
| 110 | **Single Provider Configuration:** |
| 111 | |
| 112 | ```hcl |
| 113 | provider "aws" "this" { |
| 114 | config { |
| 115 | region = var.aws_region |
| 116 | assume_role_with_web_identity { |
| 117 | role_arn = var.role_arn |
| 118 | web_identity_token = var.identity_token |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | ``` |
| 123 | |
| 124 | **Multiple Provider Configurations with for_each:** |
| 125 | |
| 126 | ```hcl |
| 127 | provider "aws" "configurations" { |
| 128 | for_each = var.regions |
| 129 | |
| 130 | config { |
| 131 | region = each.value |
| 132 | assume_role_with_web_identity { |
| 133 | role_arn = var.role_arn |
| 134 | web_identity_token = var.identity_token |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | ``` |
| 139 | |
| 140 | **Authentication Best Practice**: Use **workload identi |