8/5/2024 • VirtueCloud

Terraform HCP (HashiCorp Cloud Platform) is an application that helps teams use Terraform together. It manages Terraform runs in a consistent and reliable environment, and includes easy access to shared state and secret data, access controls for approving changes to infrastructure, a private registry for sharing Terraform modules, detailed policy controls for governing the contents of Terraform configurations, and more. This blog will introduce you to Terraform HCP, explore its features, explain the different workflows it supports, and guide you through setting up and configuring the platform.
HCP Terraform offers several advantages that make it an appealing choice for managing your Terraform infrastructure:
HCP Terraform organizes resources by workspaces, each containing resource definitions, environment variables, and state files. It supports three primary workflows:
1. CLI-Driven Workflow
In the CLI-driven workflow, you execute Terraform operations using Terraform’s standard CLI tools. HCP Terraform provides ephemeral remote execution environments to run these operations. Here’s what this workflow entails:
2. VCS-Driven Workflow
The VCS-driven workflow integrates with version control systems to automate Terraform operations based on repository changes. This workflow includes:
3. API-Driven Workflow
The API-driven workflow allows you to interact with HCP Terraform programmatically. This workflow is ideal for creating custom tooling and automating Terraform operations through HCP’s API. For detailed information, refer to HCP Terraform’s API Documentation.
Setting Up an Account


Create a Credentials Variable Set
Your set will be listed and applied as per the chosen scope.
In this section, we will walk through the steps to create and provision an EC2 instance using HCP Terraform’s CLI-driven workflow.
Step 1: Create Terraform Configuration Files
Initialize Your Project Directory:
mkdir ec2-instance && cd ec2-instance Create a main.tf File:
provider "aws" {
region = var.region
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "ubuntu" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
tags = {
Name = var.instance_name
}
} Create a variables.tf File:
variable "region" {
description = "AWS region"
default = "us-east-1"
}
variable "instance_type" {
description = "Type of EC2 instance to provision"
default = "t2.micro"
}
variable "instance_name" {
description = "EC2 instance name"
default = "My-EC2"
} Create a terraform.tf File:
terraform {
cloud {
organization = "XYZ"
workspaces {
name = "HCP-EC2"
}
}
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.31.0"
}
}
required_version = "~> 1.2"
} Create a outputs.tf File:
output "instance_ami" {
value = aws_instance.ubuntu.ami
}
output "instance_arn" {
value = aws_instance.ubuntu.arn
} Step 2: Initialize , Plan and Apply Configuration
Initialize Terraform:
terraform initPlan Configuration:
terraform planWhen you plan configuration, the complete terraform plan can be viewed on Hashicorp Terraform UI like this :

Apply Configuration:
terraform applyAfter you run terraform apply, For the CLI-driven workflow, you can approve the run either in the UI, or in your Terminal.

Step 5: Verify Your EC2 Instance
Check AWS Console:
Log in to your AWS Management Console.
Navigate to the EC2 Dashboard and verify that your new instance is running.
HCP Terraform offers a robust, managed platform for infrastructure management, enhancing Terraform’s capabilities with features like secure state management, collaboration, and scalability. By supporting various workflows—CLI-driven, VCS-driven, and API-driven—HCP caters to diverse needs and preferences. Whether you opt for the free tier or a paid plan, It provides a comprehensive solution for efficient and secure infrastructure management.
Related articles you might find interesting

Why platform teams are re-routing north-south traffic through the Kubernetes Gateway API, what HTTPRoute changes on the ground, and how to migrate without a big-bang rewrite.


Hassle-Free ECS: Terraform Automation + CI/CD Pipeline