Synopsis
Manage HCP Terraform stack operationsUsage
Description
Theterraform stacks command interacts with HCP Terraform (formerly Terraform Cloud) for stack operations. This command delegates all execution to an internal plugin that manages the stack lifecycle.
Stacks provide a way to manage collections of Terraform configurations as a single unit in HCP Terraform, enabling complex multi-environment and multi-configuration deployments.
How It Works
The stacks command operates through a plugin architecture:- Plugin Discovery - Discovers HCP Terraform service URLs via service discovery
- Plugin Download - Downloads the appropriate stacks plugin binary for your platform (or uses a cached version)
- Plugin Execution - Delegates all subcommand execution to the plugin via gRPC
Options
Specify a custom directory for caching the stacks plugin binary. By default, the plugin is cached in the Terraform CLI configuration directory under
stacksplugin/.Default: ~/.terraform.d/stacksplugin (or platform equivalent)Configuration
The stacks command is configured via environment variables:Required Configuration
While some commands may work without authentication, most operations require a token:Authentication token for HCP Terraform. If not provided, the command will attempt to read from the credentials file. Some commands like
stacks init and stacks validate may work without a token.Optional Configuration
The hostname of the HCP Terraform instance to connect to.Default:
app.terraform.ioThe HCP Terraform organization name to use for operations.
The HCP Terraform project name to use for operations.
The stack name to use for operations.
Development Configuration
Path to a local stacks plugin binary for development. When set, this overrides the normal plugin download mechanism. A warning will be displayed when using this override.Use only for plugin development.
Plugin Management
The stacks command automatically manages the plugin lifecycle:- First Run - Downloads the plugin from the HCP Terraform service
- Cached Runs - Uses the cached plugin binary from subsequent runs
- Plugin Storage - Plugins are stored in
~/.terraform.d/stacksplugin/or the configured cache directory - Custom Cache - Use
-plugin-cache-dirto override the default cache location - Dev Override - Use
TF_STACKS_PLUGIN_DEV_OVERRIDEfor local development
Service Discovery
The command uses service discovery to locate:- TFE API Service (
tfe.v2) - The main HCP Terraform API - Stacks Plugin Service (
stacksplugin.v1) - The plugin download service
Exit Codes
- 0 - Success
- 1 - General error (configuration errors, flag parsing errors)
- 98 - Plugin download or initialization error
- 99 - RPC communication error with the plugin
- Subcommand-specific codes - Exit codes from plugin subcommands
Examples
Display stacks help
Run a stacks subcommand
plan subcommand via the plugin.
Use custom plugin cache directory
/custom/cache/stacksplugin/.
Configure via environment variables
Use development plugin override
Prerequisites
- Cloud Backend - The stacks command requires HCP Terraform connectivity
- Service Discovery - Must be able to reach the HCP Terraform hostname for service discovery
- Authentication - Most operations require a valid HCP Terraform token
Subcommands
The available subcommands are provided by the stacks plugin. Runterraform stacks without arguments to see the full list of available subcommands and their documentation.
Common subcommands typically include:
init- Initialize a stackplan- Preview stack changesapply- Apply stack changesvalidate- Validate stack configurationdestroy- Destroy stack resources
Notes
- The plugin handshake uses protocol version 1 by default
- Plugin binaries are platform-specific (OS and architecture)
- The command respects the global
-chdirflag for working directory - Plugin logs are available via Terraform’s logging mechanisms
- Network connectivity to HCP Terraform is required for most operations
- The credentials file follows standard HCP Terraform authentication patterns