Terraform

Infrastructure as Code (IaC) has completely changed the way teams manage and provision infrastructure. In the past, infrastructure management was manual, slow, and prone to human error, which caused inconsistencies and scaling issues. As businesses grow, their infrastructure becomes more complex, creating the need for a faster and more reliable solution. Terraform helps solve this challenge by automating infrastructure provisioning across multiple platforms.

What is Infrastructure as code?

Infrastructure as Code (IaC) is a practice in IT where infrastructure is provisioned and managed using code and automated tools, rather than through manual processes. This approach allows for managing infrastructure in a way that is similar to how software development and deployment are handled. Using IAC tools like Terraform offers advantages for your infrastructure, including consistency, repeatability, and scalability

What is Terraform?

Terraform is an open-source tool that is used for infrastructure provisioning. It enables users to create, manage, and update resources across various cloud providers and services in a consistent and automated manner. It works in a declarative format meaning you define the desired state of your application rather than specifying the steps to achieve that state.

Why use terraform?

Terraform helps to carry out 3 major function / use cases

Prepare Infrastructure:
Terraform helps in setting up the initial environment, provisioning resources, and ensuring that all necessary components are in place for your applications and services to run. This includes tasks like creating and configuring servers (EC2 instances), networks, storage systems and other essential components.

Manage Existing resources:
Terraform automates continuous changes to your infrastructure. They are used to manage existing infrastructure which involves updating and modifying the resources that have already been provisioned.

Replicating Infrastructure:
One of the significant benefits of using Infrastructure as Code (IAC) tools such as Terraform is the ability to duplicate infrastructure across various environments.
This enables the creation of a uniform environment for development, testing, staging, and production. With Terraform, you can effortlessly employ the same configuration to duplicate your development environment in the production environment, promoting consistency and minimizing the chance of errors.

Terraform Architecture

Terraform has two major components in its architecture which are

  • Terraform Core
  • Terraform Providers


Terraform Core

At its heart, Terraform Core is the engine that powers Terraform. Instead of working directly with resources, it processes configuration files and manages the state of your infrastructure. In simple terms, you can think of it as the “brain” of Terraform because it decides what should change, and in which order, so your setup matches your desired outcome.

To achieve this, Terraform Core relies on two important inputs:

  1. Terraform Configuration
    • These files act as the blueprint of your infrastructure.
    • You can write them in HashiCorp Configuration Language (HCL) or JSON.
    • Within the files, you describe the resources you need, such as an AWS EC2 instance, an Azure storage account, or a Kubernetes cluster.
    • For example, if you declare three EC2 instances with a specific security group, Terraform reads those instructions and prepares a plan to create them.
  2. Terraform State
    • Acting like Terraform’s memory, the state file stores details of your existing infrastructure.
    • With this record, Terraform compares the current state against your configuration.
    • As a result, it knows when to create, update, or remove resources.
    • Without the state file, Terraform would lose track of deployed resources, which could lead to duplication or conflicts.

In short, Terraform Core merges the configuration (what you want) with the state (what you already have) to produce an execution plan that aligns both.


Terraform Providers

While Terraform Core makes decisions, it does not interact with cloud platforms or APIs directly. Terraform Providers fill this gap by acting as plugins that connect Terraform to external services.

  • A provider is essentially a bridge between Terraform and a service.
  • Each provider knows how to manage resources on a specific platform such as AWS, Azure, GCP, Kubernetes, GitHub, or MySQL.
  • Furthermore, providers expose resources (things you can create, like aws_instance or kubernetes_pod) and data sources (things you can read, like aws_ami).

For example:

  • The AWS Provider lets you build EC2 instances, VPCs, and S3 buckets.
  • The Kubernetes Provider enables you to deploy pods, services, and config maps.
  • The GitHub Provider automates repository creation, branch policies, and permissions.

In addition, you can combine several providers in a single project. Therefore, Terraform becomes powerful enough to manage resources across multiple cloud platforms, on-premises systems, and SaaS tools all at once.

How terraform works?

Terraform works by using configuration files to define the desired state of infrastructure resources. It follows these steps:

  • Write: In this phase, you define the infrastructure resources in config files.
  • Initialize: Run terraform init command to setup the working directory and download the necessary provider plugins
  • Plan: Run the terraform plan command to create an execution plan, showing what changes will be made to reach the desired state.
  • Apply: After running terraform plan the next stage is terraform applyterraform apply is used to apply changes and provision the infrastructure specified in the plan. It also updates the state files, ensuring they are up to date.
  • Destroy: Run terraform destroy to tear down the infrastructure and clean up resources if needed.

Terraform vs ansible

Terraform is primarily used for provisioning infrastructure. It prepares infrastructure for deployment, while Ansible is mainly a configuration tool. After Terraform has provisioned the infrastructure, Ansible configures the infrastructure, deploys applications, and installs/updates software.

DevOps engineers commonly use both tools. They use Terraform to handle the infrastructure setup, while Ansible manages the configuration and deployment. This combination helps automate the entire process, making it more efficient and reliable.

FeatureTerraformAnsible
TypeInfrastructure as Code (IaC) toolConfiguration Management & Automation tool
Primary UseProvisioning and managing infrastructureConfiguring and managing software on servers
Execution ModelDeclarative – you define the desired state, Terraform ensures itProcedural (also declarative in some cases) – tasks run in the order defined
LanguageHashiCorp Configuration Language (HCL) or JSONYAML (Ansible Playbooks)
State ManagementMaintains state in a file to track infrastructureStateless – no state file, executes tasks each time
IdempotencyBuilt-in, ensures the same result every runAlso idempotent, but requires careful module usage
ProvisioningExcellent at creating and managing cloud resources (AWS, Azure, GCP, etc.)Limited; mainly focuses on configuration after provisioning
ConfigurationWeak in configuration managementStrong in configuration management (packages, users, services)
OrchestrationOrchestrates infrastructure lifecycle (create, update, destroy)Orchestrates application deployment and server setup
Agent RequirementAgentlessAgentless
Learning CurveModerate, requires understanding of IaC conceptsEasy to start, simple YAML syntax
Best Use CaseMulti-cloud infrastructure provisioningApplication deployment and configuration management
IntegrationWorks with providers (AWS, Azure, GCP, Kubernetes, etc.)Works with modules for apps, services, OS packages, etc.

Conclusion

In this tutorial, we covered the basics of Terraform and Infrastructure as Code (IaC). Terraform automates the provisioning and management of infrastructure through declarative configuration files, ensuring consistency and scalability across environments. We explored its core components — Terraform Core and Providers — as well as its operational workflow, including writing, initializing, planning, applying, and destroying infrastructure. Additionally, we compared Terraform with Ansible.
Terraform, as a leading IaC tool, simplifies the provisioning and management of infrastructure across different cloud providers and services.