Tools  

Infrstructure as code using Terraforms

Terraform is an open-source IaC (Infrastructure-as-Code) tool for configuring and deploying cloud infrastructure. It codifies infrastructure in configuration files that describe the desired state for your topology. Terraform allows you to use a consistent workflow over your infrastructure lifecycle, regardless of the resource provider. The infrastructure as code workflow lets you declaratively manage a variety of services and automate your changes to them, reducing the risk of human error through manual operations.

Advantages

  1. IAC is crucial because it helps to speed the deployment and release of software
  2. Terraform supports provide multi-cloud platforms like AWS, AZURE, and Google etc..
  3. IAC assists to restrict user from accidentally deleting resources
  4. Provide a template to write code and use it many times
  5. The unified template also provides security

HCP Terraform

1. Workflows

In HCP Terraform, your resources are organized by workspaces, which contain your resource definitions, environment, input variables, and state files. A Terraform operation occurs within a workspace, and Terraform uses the configuration and state for that workspace to modify your infrastructure.

HCP Terraform supports three workflows for your Terraform runs:

  • The CLI-driven workflow, which uses Terraform's standard CLI tools to execute runs in HCP Terraform.
  • The UI/Version Control System(VCS)-driven workflow, in which changes pushed to version control repositories trigger runs in the associated workspace.
  • The API-driven workflow, which allows you to create tooling to interact with the HCP Terraform API programmatically

Terraform Basic command

  • Init : Validate working directory consisting of the Terraform config file. This is the first command to be executed after writing a new file.
  • Validate: Check for syntax and validate the config file
  • Plan: Create an execution plan
  • Apply: Apply changes and execute the command to create resources
  • Destroy: Remove Terraform-managed resources. It will ask for confirmation before removing resources

In the example below, we will deploy a resource group using Terraform modules with a real-world scenario

Terraform Module Definition

A Terraform module is a container for multiple resources that are used together.

  • A module can be as simple as a single resource or as complex as a complete infrastructure stack.
  • Modules provide a modular structure with a clear separation of concerns, making infrastructure easier to manage, reuse, and maintain.

Example: Deploying a Resource Group with Terraform Modules

In this example, we use Terraform modules to deploy a Resource Group.

Note. You can download the source code from the top of this article.

├── main.tf                    # Main Terraform configuration
├── variables.tf               # Variable definitions
├── terraform.tfvars           # variable values
├── modules/                   # Terraform modules
│   └── resource-group/        # Resource Group module
│       ├── main.tf           # Module main configuration
│       ├── variables.tf      # Module variables
│       └── outputs.tf        # Module outputs
└── README.md                 # This file

This repository demonstrates how to:

  1. Define a Terraform module for Resource Group creation.
  2. Use input variables for flexibility.
  3. Apply a modular structure for better reusability.

Summary

Terraform, as an Infrastructure as Code (IAC) tool, allows infrastructure provisioning and management through declarative configuration files. By defining resources in reusable modules, teams achieve:

  • Consistency: repeatable deployments across environments.
  • Modularity: clear separation of concerns, making code reusable and maintainable.
  • Scalability: ability to orchestrate simple to complex infrastructure stacks.
  • Collaboration: version-controlled infrastructure code shared across teams.

Using Terraform as IAC empowers organizations to manage infrastructure in the same way they manage application code. This approach reduces manual effort, minimizes errors, and accelerates delivery. With its modular design, state management, and provider ecosystem, Terraform provides a scalable, reliable, and automated way to orchestrate infrastructure, making it a cornerstone of modern DevOps practices.