Skip to main content

Introduction to Terraform


Chapter objectives

  • Understand Infrastructure as Code
  • Discover Terraform and its concepts
  • Compare with the alternatives
  • Identify use cases

1 - What is Infrastructure as Code?

The traditional problem

The IaC solution

Advantages of IaC

Manual approachInfrastructure as Code
Clicks in the consoleVersioned code
Outdated documentationCode = documentation
Hard to reproduceIdentical deployment
Complex auditGit history
Slow and error-proneFast and reliable

2 - What is Terraform?

Definition

Terraform is an open-source HashiCorp tool for provisioning and managing infrastructure declaratively.

Key characteristics

CharacteristicDescription
DeclarativeYou describe the desired final state
AgnosticWorks with 3000+ providers
IdempotentSame result on every run
StateTracking of the infrastructure state
PlanPreview of the changes

3 - Terraform workflow

The 4 essential commands

Command details

CommandActionWhen to use it
initInitializes the projectAt the start, after adding a provider
planPreviews the changesBefore each apply
applyApplies the changesTo create/modify
destroyRemoves the infrastructureTo clean up

4 - Fundamental concepts

Architecture

Main components

ComponentDescriptionExample
ProviderPlugin to communicate with an APIaws, azurerm, google
ResourceInfrastructure element to createaws_instance, azurerm_virtual_machine
Data SourceReading of existing dataaws_ami, azurerm_subscription
VariableInput parameterinstance_type, region
OutputExported valuepublic_ip, dns_name
ModuleReusable group of resourcesvpc-module, ecs-cluster
StateFile tracking the infrastructureterraform.tfstate

5 - Declarative vs Imperative

Imperative approach (scripts)

# Script bash
aws ec2 run-instances \
--image-id ami-12345678 \
--instance-type t2.micro \
--count 1

# Si on relance, on crée une 2ème instance !

Declarative approach (Terraform)

# Configuration Terraform
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t2.micro"
}

# Relancer = pas de changement si l'instance existe déjà

6 - Comparison with the alternatives

IaC tools

Comparison table

CriterionTerraformCloudFormationAnsiblePulumi
TypeProvisioningProvisioningConfigProvisioning
Multi-cloud✅ Yes❌ AWS only✅ Yes✅ Yes
LanguageHCLJSON/YAMLYAMLPython/TS/Go
StateExternalManaged by AWSNoneExternal
CurveMediumMediumEasyHard
LicenseBSLProprietaryGPLApache

When to use Terraform?

Use caseTerraformAlternative
Multi-cloud✅ Excellent-
AWS only✅ GoodCloudFormation
Server configuration⚠️ PossibleAnsible
Kubernetes✅ GoodHelm, ArgoCD
Developers⚠️ HCLPulumi (languages)

7 - HashiCorp ecosystem

HashiCorp products

Terraform Cloud

FeatureOSSCloud FreeCloud Team
Local execution
Remote state
Collaboration
Sentinel policies
SSO

8 - First example

Configuration file

# main.tf

# Configuration du provider AWS
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

provider "aws" {
region = "eu-west-1"
}

# Créer une instance EC2
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"

tags = {
Name = "HelloTerraform"
}
}

# Afficher l'IP publique
output "public_ip" {
value = aws_instance.web.public_ip
}

Execution

# Initialiser le projet
terraform init

# Prévisualiser
terraform plan

# Appliquer
terraform apply

# Résultat
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:
public_ip = "54.123.45.67"

Summary

Key points
  • Terraform turns infrastructure into code
  • Declarative approach: describe the final state
  • Multi-cloud: AWS, Azure, GCP and more
  • The state tracks the actual state of the infrastructure
  • Plan before apply: always preview

Practical exercises

  1. Explain the difference between IaC and traditional scripts
  2. Name 3 advantages of Terraform
  3. What is the role of the state file?
  4. Compare Terraform and Ansible

← Table of contents | Installation →