HCP Terraform Capabilities
HashiCorp Cloud Platform (HCP) Terraform, formerly known as Terraform Cloud, provides enhanced capabilities for managing Terraform infrastructure at scale. Understanding these capabilities is important for the certification exam.
Overview of HCP Terraform
HCP Terraform is a cloud-based service from HashiCorp that offers:
- Remote state management
- Collaborative workflows
- Policy enforcement
- Private module registry
- CI/CD integrations
- Governance features
- API and automation capabilities
HCP Terraform comes in three tiers:
- Free Tier (limited features for individuals/small teams)
- Team & Governance Tier (team collaboration and basic governance)
- Business Tier (advanced governance, SSO, audit, etc.)
Remote State Management in HCP Terraform
Configuring HCP Terraform as a Backend
terraform {
cloud {
organization = "my-organization"
workspaces {
name = "my-app-prod"
}
}
}
Benefits of HCP Terraform State Management
- Security
- Encryption at rest and in transit
- Access controls
- No local state files
- Collaboration
- Centralized state
- Automatic state locking
- State history and versioning
- Reliability
- Hosted and managed by HashiCorp
- Highly available
- Regular backups
Workspace Types
HCP Terraform supports two types of workspaces:
- CLI-driven workflows
- Terraform commands executed locally
- State stored in HCP Terraform
- Integration with version control optional
- Version Control System (VCS) driven workflows
- Connected to a Git repository
- Automatically triggered on commits
- Terraform commands executed remotely
- “Infrastructure as Code” approach
Execution Models
Local Execution
In local execution:
- Terraform commands run on your local machine
- State is pushed to HCP Terraform
- Resources are created from your machine
Configuration:
terraform {
cloud {
organization = "my-organization"
workspaces {
name = "my-app-prod"
}
execution_mode = "local"
}
}
Remote Execution
In remote execution:
- Terraform commands run on HCP Terraform servers
- No local access or credentials needed
- Consistent execution environment
Configuration:
terraform {
cloud {
organization = "my-organization"
workspaces {
name = "my-app-prod"
}
execution_mode = "remote"
}
}
Benefits of remote execution:
- Consistent environment
- Secure credential management
- Parallel operations
- Detailed logging
Team Collaboration Features
Workspace Management
HCP Terraform workspaces offer:
- Environment-specific settings
- Variable management (including sensitive values)
- Run triggers between workspaces
- Access controls
User Management and Permissions
HCP Terraform provides:
- Organization-level roles
- Workspace-level permissions
- Team management
- SSO integration (Business tier)
Common roles and permissions:
- Organization Owner
- Organization Member
- Workspace Admin
- Workspace Write
- Workspace Read
Collaborative Workflow
The typical HCP Terraform workflow:
- Write code and commit to VCS
- Plan automatically triggered in HCP Terraform
- Review plan with team members
- Apply changes after approval
- Monitor the infrastructure state
Sentinel Policies for Governance
What is Sentinel?
Sentinel is HashiCorp’s policy as code framework that enables:
- Fine-grained, logic-based policy decisions
- Governance across all HashiCorp products
- Enforcement of compliance and security standards
How Sentinel Works with HCP Terraform
- Policies are written in Sentinel language
- Policies are applied to workspaces
- Policies are checked during plan phase
- Policies can allow, deny, or require additional approval
Example Sentinel Policy
# Only allow specific instance types
import "tfplan"
allowed_types = [
"t3.micro",
"t3.small",
"t3.medium",
]
ec2_instances = filter tfplan.resource_changes as _, rc {
rc.type is "aws_instance" and
(rc.change.actions contains "create" or rc.change.actions contains "update")
}
instance_type_allowed = rule {
all ec2_instances as _, instance {
instance.change.after.instance_type in allowed_types
}
}
main = rule {
instance_type_allowed
}
Sentinel Policy Types
HCP Terraform supports three policy enforcement levels:
- Advisory: Issues warnings but allows operations to proceed
- Soft Mandatory: Requires an override from authorized users
- Hard Mandatory: Cannot be overridden
Private Module Registry
Purpose and Benefits
The private module registry provides:
- Centralized repository for reusable modules
- Version control of modules
- Documentation generation
- Access controls
Publishing a Module
To publish a module:
- Create a Git repository following the naming convention:
terraform-<PROVIDER>-<MODULE_NAME>
- Add required files:
main.tf,variables.tf,outputs.tf, etc.README.md(for documentation)
-
Release a version with semantic versioning
- Connect the repository to HCP Terraform
Using Private Registry Modules
module "vpc" {
source = "app.terraform.io/my-organization/vpc/aws"
version = "1.0.0"
name = "production-vpc"
cidr = "10.0.0.0/16"
}
Cost Estimation and Capacity Planning
Cost Estimation Features
HCP Terraform provides cost estimation for:
- AWS
- Azure
- Google Cloud
- Oracle Cloud Infrastructure
Capabilities include:
- Pre-apply cost estimates
- Cost comparisons between runs
- Monthly projections
Integrating Cost Estimation
Cost estimation is automatically enabled for supported providers and can be:
- Viewed in the UI during planning
- Used in approval workflows
- Shared with stakeholders
HCP Terraform API and Automation
API Capabilities
HCP Terraform offers a comprehensive API for:
- Workspace management
- Run management
- Variable management
- State management
- User/team management
Common API Use Cases
- CI/CD Integration:
- Trigger runs from external systems
- Extract outputs for downstream processes
- Workspace Automation:
- Create workspaces programmatically
- Manage variables across workspaces
- Reporting:
- Collect state data for compliance
- Generate infrastructure reports
Authentication
API authentication uses token-based authentication:
- Organization tokens
- Team tokens
- User tokens
CI/CD Integration
VCS Integration
HCP Terraform connects with:
- GitHub
- GitLab
- Bitbucket
- Azure DevOps
Features:
- Automatic run triggering on commit
- Plan display in pull requests
- Apply on merge
Other CI/CD Tools
Integration with other CI/CD systems:
- Jenkins
- CircleCI
- GitHub Actions
- Others via API
Example GitHub Actions workflow:
name: "Terraform"
on:
push:
branches: [ main ]
pull_request:
jobs:
terraform:
name: "Terraform"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
cli_config_credentials_token: $
- name: Terraform Init
run: terraform init
- name: Terraform Format
run: terraform fmt -check
- name: Terraform Plan
run: terraform plan
- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply -auto-approve
Key Differences: HCP Terraform vs. Terraform Community Edition
| Feature | HCP Terraform | Terraform Community Edition |
|---|---|---|
| State Management | Remote, secure, versioned | Local by default, manual remote backend config required |
| Execution | Local or remote | Local only |
| Collaboration | Built-in | Manual, requires external tools |
| Private Registry | Included | Not available |
| Policy Enforcement | Sentinel available | Not available |
| Cost Estimation | Included | Not available |
| Approval Workflow | Built-in | Not available |
| API | Full platform API | Limited state operations |
| User Management | Organizations, teams, roles | No built-in user management |
| VCS Integration | Automatic | Manual |
Key Points for the Exam
- HCP Terraform offers remote state management with enhanced security and collaboration
- Workspaces in HCP Terraform organize and separate environments and teams
- HCP Terraform supports both local and remote execution models
- Sentinel enables policy as code for governance and compliance
- Private module registry provides a secure, centralized place for reusable modules
- Cost estimation helps predict infrastructure spending before applying changes
- HCP Terraform offers extensive APIs for automation and integration
- VCS integration enables GitOps workflows with automatic planning and applying
- HCP Terraform comes in Free, Team & Governance, and Business tiers with increasing capabilities