npx skills add ...
npx skills add hashicorp/terraform-agent-kit --skill terraform-stacks
Comprehensive 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 registry sources, managing multi-region or multi-environment infrastructure, or troubleshooting Terraform Stacks syntax and structure.
This repo is now called hashicorp/agent-skills. Both names install the same content, but the install count here only covers this one.
npx skills add hashicorp/terraform-agent-kit --skill terraform-stacks
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.
Stack: A complete unit of infrastructure composed of components and deployments that can be managed together.
Component: An abstraction around a Terraform module that defines infrastructure pieces. Each component specifies a source module, inputs, and providers.
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.
Stack Language: A separate HCL-based language (not regular Terraform HCL) with distinct blocks and file extensions.
Terraform Stacks use specific file extensions:
.tfcomponent.hcl.tfdeploy.hcl.terraform.lock.hcl (generated by CLI)All configuration files must be at the root level of the Stack repository. HCP Terraform processes all files in dependency order.
Note: The modules/ directory is only required when using local module sources. Components can reference modules from:
./modules/vpcterraform-aws-modules/vpc/awsapp.terraform.io/<org-name>/vpc/awsgit::https://github.com/org/repo.git//path?ref=v1.0.0HCP Terraform processes all .tfcomponent.hcl and .tfdeploy.hcl files in dependency order.
Use Terraform v1.13.x or later to access the Stacks CLI plugin and to run terraform stacks CLI commands. Begin by adding a .terraform-version file to your Stack's root directory to specify the Terraform version required for your Stack. For example, the following file specifies Terraform v1.14.5:
Declare input variables for the Stack configuration. Variables must define a type field and do not support the validation argument.
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.
Provider blocks differ from traditional Terraform:
for_each meta-argumentconfig blockSingle Provider Configuration:
Multiple Provider Configurations with for_each:
Authentication Best Practice: Use workload identity (OIDC) as the preferred authentication method for Stacks. This approach:
Configure workload identity using identity_token blocks and assume_role_with_web_identity in provider configuration. For detailed setup instructions for AWS, Azure, and GCP, see: https://developer.hashicorp.com/terraform/cloud-docs/dynamic-provider-credentials
Each Stack requires at least one component block. Add a component for each module to include in the Stack. Components reference modules from local paths, registries, or Git.
See references/component-blocks.md for examples of dependencies, for_each, public registry modules, Git sources, and more.
Key Points:
component.<name>.<output> or component.<name>[key].<output> for for_each[for x in component.s3 : x.bucket_name]for_each, reference specific instances: component.<name>[each.value].<output>provider.<type>.<alias> or provider.<type>.<alias>[each.value]Outputs require a type argument and do not support preconditions:
Locals blocks work the same in both .tfcomponent.hcl and .tfdeploy.hcl files:
Use to safely remove components from a Stack. HCP Terraform requires the component's providers to remove it.
Generate JWT tokens for OIDC authentication with cloud providers:
Reference tokens in deployments using identity_token.<name>.jwt
Access HCP Terraform variable sets within Stack deployments:
Use to centralize credentials and share variables across Stacks. See references/deployment-blocks.md for details.
Define deployment instances (minimum 1, maximum 20 per Stack):
To destroy a deployment: Set destroy = true, upload configuration, approve destroy run, then remove the deployment block. See references/deployment-blocks.md for details.
Group deployments together for shared settings (HCP Terraform Premium tier feature). Free/standard tiers use default groups named {deployment-name}_default.
Multiple deployments can reference the same group. See references/deployment-blocks.md for details.
Define rules to automatically approve deployment plans (HCP Terraform Premium tier feature):
Available context variables: context.plan.applyable, context.plan.changes.add/change/remove/total, context.success
Note: orchestrate blocks are deprecated. Use deployment_group and deployment_auto_approve instead.
See references/deployment-blocks.md for all context variables and patterns.
Link Stacks together by publishing outputs from one Stack and consuming them in another:
See references/linked-stacks.md for complete documentation and examples.
Note: Terraform Stacks is Generally Available (GA) as of Terraform CLI v1.13+. Stacks now count toward Resources Under Management (RUM) for HCP Terraform billing.
Important: No plan or apply commands. Upload configuration triggers deployment runs automatically.
For programmatic monitoring in automation, CI/CD, or non-interactive environments (like AI agents), use the HCP Terraform API instead of CLI watch commands. The API provides endpoints for:
Key points:
GET /api/v2/stack-deployment-steps/{step-id}/artifacts?name=apply-descriptionstack_deployment_step_id query parametercurl -L)For complete API workflow, authentication, polling best practices, and example scripts, see references/api-monitoring.md.
Component Dependencies: Dependencies are automatically inferred when one component references another's output (e.g., subnet_ids = component.vpc.private_subnet_ids).
Multi-Region Deployment: Use for_each on providers and components to deploy across multiple regions. Each region gets its own provider configuration and component instances.
Deferred Changes: Stacks support deferred changes to handle dependencies where values are only known after apply. This enables complex multi-component deployments where some resources depend on runtime values from other components (cluster endpoints, generated passwords, etc.).
For complete examples including multi-region deployments, component dependencies, deferred changes patterns, and linked Stacks, see references/examples.md.
.terraform.lock.hcl to version controlCircular Dependencies: Refactor to break circular references or use intermediate components.
Deployment Destruction: Cannot destroy from UI. Set destroy = true in deployment block, upload configuration, and HCP Terraform creates a destroy run.
Empty Diagnostics: Add required stack_deployment_step_id query parameter to diagnostics API requests.
Module Compatibility: Test public registry modules before production use. Some modules may have compatibility issues with Stacks.
For detailed documentation, see:
references/component-blocks.md - Complete component block reference with all arguments and syntaxreferences/deployment-blocks.md - Complete deployment block reference with all configuration optionsreferences/linked-stacks.md - Publish outputs and upstream inputs for linking Stacks togetherreferences/examples.md - Complete working examples for multi-region and component dependenciesreferences/api-monitoring.md - Full API workflow for programmatic monitoring and automationreferences/troubleshooting.md - Detailed troubleshooting guide for common issues and solutions