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 approach | Infrastructure as Code |
|---|---|
| Clicks in the console | Versioned code |
| Outdated documentation | Code = documentation |
| Hard to reproduce | Identical deployment |
| Complex audit | Git history |
| Slow and error-prone | Fast and reliable |
2 - What is Terraform?
Definition
Terraform is an open-source HashiCorp tool for provisioning and managing infrastructure declaratively.
Key characteristics
| Characteristic | Description |
|---|---|
| Declarative | You describe the desired final state |
| Agnostic | Works with 3000+ providers |
| Idempotent | Same result on every run |
| State | Tracking of the infrastructure state |
| Plan | Preview of the changes |
3 - Terraform workflow
The 4 essential commands
Command details
| Command | Action | When to use it |
|---|---|---|
init | Initializes the project | At the start, after adding a provider |
plan | Previews the changes | Before each apply |
apply | Applies the changes | To create/modify |
destroy | Removes the infrastructure | To clean up |
4 - Fundamental concepts
Architecture
Main components
| Component | Description | Example |
|---|---|---|
| Provider | Plugin to communicate with an API | aws, azurerm, google |
| Resource | Infrastructure element to create | aws_instance, azurerm_virtual_machine |
| Data Source | Reading of existing data | aws_ami, azurerm_subscription |
| Variable | Input parameter | instance_type, region |
| Output | Exported value | public_ip, dns_name |
| Module | Reusable group of resources | vpc-module, ecs-cluster |
| State | File tracking the infrastructure | terraform.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
| Criterion | Terraform | CloudFormation | Ansible | Pulumi |
|---|---|---|---|---|
| Type | Provisioning | Provisioning | Config | Provisioning |
| Multi-cloud | ✅ Yes | ❌ AWS only | ✅ Yes | ✅ Yes |
| Language | HCL | JSON/YAML | YAML | Python/TS/Go |
| State | External | Managed by AWS | None | External |
| Curve | Medium | Medium | Easy | Hard |
| License | BSL | Proprietary | GPL | Apache |
When to use Terraform?
| Use case | Terraform | Alternative |
|---|---|---|
| Multi-cloud | ✅ Excellent | - |
| AWS only | ✅ Good | CloudFormation |
| Server configuration | ⚠️ Possible | Ansible |
| Kubernetes | ✅ Good | Helm, ArgoCD |
| Developers | ⚠️ HCL | Pulumi (languages) |
7 - HashiCorp ecosystem
HashiCorp products
Terraform Cloud
| Feature | OSS | Cloud Free | Cloud 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
- Explain the difference between IaC and traditional scripts
- Name 3 advantages of Terraform
- What is the role of the state file?
- Compare Terraform and Ansible