Deploy an EC2 instance using Terraform Cloud (HCP Terraform)
Introduction
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.
Why HCP Terraform?
HCP Terraform offers several advantages that make it an appealing choice for managing your Terraform infrastructure:
Types of Workflows HCP Supports
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.
How to Get Started with HCP Terraform
Setting Up an Account
Configuring Terraform Through CLI Login
Managing AWS Credentials on HCP?
Create a Credentials Variable Set
1. Navigate to Settings > Variable Sets.
2. Click Create variable set.
1. Name it “AWS Credentials.”
2. Choose scope (global or specific).
1. Click +Add Variable.
领英推荐
2. Add AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as Environment variables. Mark as Sensitive.
Your set will be listed and applied as per the chosen scope.
Provision an EC2 Instance using CLI-Driven Workflow
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:
Plan Configuration:
When you plan configuration, the complete terraform plan can be viewed on Hashicorp Terraform UI like this :
Apply Configuration:
After 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:
Pricing
Conclusion
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.