Introduction
HCP Terraform Stacks allows you to orchestrate multiple Terraform modules as a single unit. When using private modules from the HCP Registry, the CLI workflow ensures that your local configuration is validated and synchronized with the HCP Terraform cloud environment.
This article explains the correct syntax to reference private registry modules in Terraform Stacks, along with a validated end-to-end CLI workflow.
Prerequisites
HCP Terraform CLI: Version
1.13.xor higher (recommended your.terraform-versionmatches your local install).Authenticated Session: Ensure you have logged in via
terraform login.-
Permissions: You must have
Manage Stackspermissions in your HCP Terraform Organization. Private Module: A module published and available in the HCP Terraform private registry.
Procedures
1. Define and Organize All Required Stack Configuration Files
Before defining a component that consumes a private registry module, ensure that all required Stacks configuration files are properly defined and placed in the correct file types. Terraform Stacks enforces strict separation of configuration blocks by file extension.
a. Define Providers (providers.tfcomponent.hcl)
All required providers must be declared explicitly using required_providers, along with any provider configurations referenced by components.
required_providers {
random = {
source = "hashicorp/random"
version = "3.7.1"
}
}
provider "random" "test" {}b. Define Input Variables (variables.tfcomponent.hcl)
Define input variables that will be passed into the component. This includes string and boolean variables used by the module inputs.
variable "string_length" {
description = "Length of the string"
type = string
}
variable "special" {
description = "Whether to include special characters"
type = bool
}c. Define the Component Using a Private Registry Module
When referencing a private registry module in a Stack component, the source attribute must follow this format: app.terraform.io/<organization>/<module-name>/<provider>
component "random-test" {
source = "app.terraform.io/<org-name>/<module-name>/<provider>"
version = "1.0.4"
# inputs are options if you already have the values and the variables configured at module level.
inputs = {
length = var.string_length
special = var.special
}
providers = {
random = provider.random.test
}
}
d. Define the Deployment (deployment.tfdeploy.hcl)
Deployments must be defined in a *.tfdeploy.hcl file. This file declares how components are grouped and deployed within the stack. At a minimum, a deployment block must exist for the stack to create deployment runs.
deployment "application" {
}Additional Information:
Terraform Stacks CLI Reference : https://developer.hashicorp.com/terraform/cli/commands/stacks
Terraform Stacks Concepts: Components, Deployments : https://developer.hashicorp.com/terraform/language/stacks
HCP Terraform Stacks Tutorial : https://developer.hashicorp.com/terraform/tutorials/cloud/stacks-deploy