HCP Terraform & Terraform Enterprise Integration
Terraform’s cloud backend provides seamless integration with HCP Terraform (formerly Terraform Cloud) and Terraform Enterprise for remote state storage, execution, and team collaboration.Overview
The cloud integration enables:- Remote state management: Store state securely in HCP Terraform
- Remote execution: Run Terraform operations in HCP Terraform’s infrastructure
- Team collaboration: Share workspaces and coordinate changes
- Policy enforcement: Apply Sentinel policies and OPA checks
- VCS integration: Trigger runs from version control systems
- Cost estimation: Preview infrastructure costs before applying
Configuration
Basic Cloud Block
Configuration Options
Frominternal/cloud/backend.go:135-180:
Workspace Strategies
The cloud backend supports three workspace selection strategies:1. Single Workspace (Name Strategy)
internal/cloud/backend.go:630-632
2. Tag-Based Selection (Tags Strategy)
internal/cloud/backend.go:638-639
3. Key-Value Tags (KV Tags Strategy)
internal/cloud/backend.go:640-650
Authentication
Environment Variables
Configuration values can be set via environment variables:internal/cloud/backend.go:453-595:
CLI Credentials
Authenticate usingterraform login:
internal/cloud/backend.go:296-309
Service Discovery
The cloud backend uses service discovery to locate the TFE API:internal/cloud/backend.go:263-279
Remote Operations
The cloud backend determines when to run operations remotely vs. locally:internal/cloud/backend.go:881-934
Local vs Remote Execution
Execution mode is determined by:- Force local flag:
TF_FORCE_LOCAL_BACKENDenvironment variable - Workspace settings: Workspace execution mode configuration
- Operations entitlement: Organization’s operations capability
internal/cloud/backend.go:428-431
Remote Plan Execution
When runningterraform plan with cloud backend:
Plan Options
internal/cloud/backend.go:55-84
Configuration Upload
internal/cloud/backend_plan.go:132-141
Run Creation
internal/cloud/backend_plan.go:143-149
Version Compatibility
The cloud backend verifies Terraform version compatibility:internal/cloud/backend.go:396-425
Workspace Terraform Version
internal/cloud/backend.go:1065-1162
Workspace Management
Auto-Creation
Workspaces are created automatically if they don’t exist:internal/cloud/backend.go:772-810
Tag Synchronization
The backend keeps workspace tags in sync:internal/cloud/backend.go:837-861
Integration Patterns
Policy Enforcement
The cloud backend handles policy evaluation during runs: Location:internal/cloud/backend_taskStage_policyEvaluation.go
Cost Estimation
Cost estimates are displayed during planning: Location:internal/cloud/backend_plan.go
State Locking
Remote state is automatically locked during operations: Location:internal/cloud/state.go
Testing Integration
Run tests remotely on HCP Terraform:internal/command/test.go:140-157
Best Practices
1. Use Environment Variables for Configuration
2. Organize Workspaces with Tags
3. Use TF_WORKSPACE for Multi-Workspace Setups
4. Leverage Local Execution When Needed
5. Implement Proper Error Handling
The cloud integration includes retry logic:internal/cloud/backend.go:611-619
Troubleshooting
Connection Issues
Enable retry logging:internal/cloud/backend.go:434
Version Conflicts
Ignore version conflicts if needed:internal/cloud/backend.go:1054-1056
Debugging Remote Runs
The cloud backend provides detailed output during remote operations through theIntegrationOutputWriter interface:
Location: internal/cloud/cloud_integration.go:18-117
Related Resources
- Testing Framework - Run tests on HCP Terraform
- Debugging - Debug cloud integration issues
- Stacks - Use Stacks with cloud backend