Terraform - Infrastructure-as-a-Code

De Marijan Stajic | Wiki
Aller à la navigation Aller à la recherche
Les informations sont posés en vrac pour le moment.

Introduction

Terraform Banner.png

Terraform is an infrastructure as code (IaC) tool that allows users to define, provision, and manage cloud and on-premises resources using a declarative configuration language (HCL). It enables consistent and repeatableinfrastructure deployment across multiple providers like AWS, Azure, and Google Cloud.

Files

There are different basic files (.tf) needed for Terraform to work properly.

project/
├── variables.tfvars
└── environments/
    └── dev/
        ├── main.tf
        ├── variables.tf
        └── provider.tf

Provider

This file is used to define:

terraform {
  required_version = ">=1.2"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=4.1.0"
    }
  }
    backend "azurerm" {
      resource_group_name  = "rg-rd-tfstate-chn"
      storage_account_name = "rdsttfstate"
      container_name       = "tfstate"
      key                  = "terraform.tfstate"
  }
}

provider "azurerm" {
  features {}
}
  • Define the required Terraform version and provider
  • Set where the tfstate should be stored (Azure Storage Account, S3 on Exo, etc.)
  • Choose the Cloud Provider (Azure, AWS, Exoscale, etc.) to use libraries (available at https://registry.terraform.io/)

Main

The main.tf file is the main one. It’s where actions on the infrastructure happen (create, delete, or update).

resource "azurerm_Resource_group" "example" {
  name     = "example-rg"
  location = "France Central"
}
  • resource: tells Terraform this is a resource
  • azurerm_Resource_group: a resource group on Azure (see registry)
  • example: the name used to refer to the resource later

Variables

With Terraform best practices:

variables.tf

variable "resource_group_name" {
  description = "Nom du groupe de ressources"
  type        = string
}
variables.tfvars

resource_group_name = "rg-rd-testmachine-chn"
  • Declare variables in a variable.tf file
  • Define their values in a variable.tfvars file

Procedure

To deploy Terraform code, the process has three steps: init, plan & apply.

Init

First step is init (initialization):

terraform init
  • Installs plugins for the provider
  • Sets up backend for .tfstate
  • Prepares folder for running other Terraform commands

Plan

Second step is plan (planning):

terraform plan
  • Checks config files to see what to create, change or delete
  • Shows a preview of what will happen
  • Lets you review changes before applying

Apply

Final step is apply (applying changes):

terraform apply
  • Confirms changes before applying
  • Applies changes from your config files
  • Updates the .tfstate file

.tfstate

This file keeps your config aligned with real infrastructure. It must be updated after every change.

You can show the managed resources with:

terraform show

Destroy

You can delete all the infrastructure made by Terraform:

terraform destroy
  • Shows what will be deleted
  • Asks for confirmation
  • Deletes after confirmation

Removing a resource

To delete a specific resource, just remove it from main.tf. Terraform will update the .tfstate accordingly.

Modules

Terraform uses inputs for infrastructure. Sometimes, you need to reuse them across modules in the same project.

project/
├── provider.tf
├── main.tf
└── modules/
    └── azure_rg_module/
        ├── main.tf
        ├── output.tf
        └── variables.tf
    └── azure_resource_module/
        ├── main.tf
        ├── output.tf
        └── variables.tf
  • In main.tf you declare a module with source, location, resource_group_name, and variables

Output

If a project needs the ID of a resource group created by another, you can get it using an output file.