Terraform: gestione dipendenze tra risorse (implicit / explicit)

TerraForm Dependencies

Sapevi che uno dei concetti fondamentali in Terraform è la gestione delle dipendenze tra le risorse?

Ti stai chiedendo a cosa servono le dipendenze? La risposta e’ molto semplice. Servono per creare una sequenza di creazione (o cancellazione) per dare un ordine corretto all’esecuzione dello script.

Comprendere queste dipendenze è essenziale per scrivere codice Terraform efficiente e affidabile. ⬇️

Terraform Dependencies

Implicit

Le dipendenze implicite sono le più semplici da gestire in Terraform in quanto non devi fare nulla. In che senso scusa?

Quando scrivi del codice Terraform puoi dire in maniera implicita ad una risorsa di utilizzare una proprietà della precedente. Facendo questo andrai a creare una dependency di tipo implicit.

Come vedrai negli esempi sottostanti non ho dovuto fare nulla di particolare ad eccezione di usare le mie risorse

Implicit Azure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
resource "azurerm_resource_group" "resource_group" {
  name     = <...>
  location = <...>
}

resource "azurerm_static_site" "static_site" {
  name                = <...>
  resource_group_name = azurerm_resource_group.resource_group.name
  location            = <...>
  sku_tier            = <...>
}

Implicit AWS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
resource "aws_s3_bucket" "s3_bucket" {
  bucket = <...>
}

resource "aws_s3_bucket_ownership_controls" "s3_bucket_ownership_controls" {
  bucket = aws_s3_bucket.s3_bucket.id
  rule {
    object_ownership = "BucketOwnerPreferred"
  }
}

Explicit

Scrivere del codice Terraform che utilizza le dipendenze esplicite richiede uno sforzo maggiore in quanto dovrai aggiungere del codice sorgente al tuo script.

Per farlo ti basterà inserire all’interno della tua risorsa l’attributo depends_on ed inserire al suo interno una lista di risorse dalle quali dipende quella che stai creando. In questo modo non sarà Terraform a decidere l’ordine di esecuzione per quella determinata risorsa, ma sarai te.

Nei due seguenti esempi ho riportato un caso limite e forse nemmeno troppo realistico. Per creare la risorsa finale ho inserito una doppia dipendenza.

  • Implicit: faccio riferimento come nell’esempio precedente alla risorsa che mi serve
  • Explicit: faccio riferimento ad una risorsa presente nello script, ma non necessaria al fine ultimi di creare la mia risorsa.

In questo modo dico a Terraform di non crearmi la risorsa finale sino a quando tutte quelle precedenti non sono state generate correttamente.

Explicit Azure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
resource "azurerm_resource_group" "resource_group" {
  name     = <...>
  location = <...>
}

resource "azurerm_resource_group" "resource_group_extra" {
  name     = <...>
  location = <...>
}


resource "azurerm_static_site" "static_site" {
  name                = <...>
  resource_group_name = azurerm_resource_group.resource_group.name
  location            = <...>
  sku_tier            = <...>
  depends_on = [
    azurerm_resource_group.resource_group_extra
  ]
}

Explicit AWS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
resource "aws_s3_bucket" "s3_bucket" {
  bucket = <...>
}

resource "aws_s3_bucket" "s3_bucket_extra" {
  bucket = <...>
}

resource "aws_s3_bucket_ownership_controls" "s3_bucket_ownership_controls" {
  bucket = aws_s3_bucket.s3_bucket.id
  rule {
    object_ownership = "BucketOwnerPreferred"
  }
    depends_on = [
      aws_s3_bucket.s3_bucket_extra
  ]
}

Terraform CookBook

HashiCorp Configuration Language (HCL) has changed how we define and provision data center infrastructure with the launch of Terraform - a top-tier product for building Infrastructure as Code (IaC). Terraform Cookbook shows you how to leverage Terraform to manage complex cloud infrastructure with ease.

This new edition has been updated to include real-world examples for provisioning Cloud infrastructure(mainly for Azure, but also a dedicated chapter for AWS and GCP) with Terraform. You’ll delve into manual and automated testing with Terraform configurations, creating and managing a balanced, efficient, reusable infrastructure with Terraform modules. You’ll learn how to automate the deployment of Terraform configuration with continuous integration and continuous delivery (CI/CD). Besides, several new chapters have been added that describe the use of Terraform for Docker and Kubernetes, and explain how to test Terraform configurations using different tools to check code and security compliance. A complete chapter is dedicated to mastering Terraform Cloud and the latest chapter covers troubleshooting common Terraform issues and provides solutions for frequently encountered errors.

By the end of this book, you’ll have developed the skills needed to get the most value out of Terraform and to effectively manage your infrastructure.

Terraform Cookbook - Second Edition: Provision, run, and scale cloud architecture with real-world examples using Terraform