État - partie 2

Backend

Le backend détermine l’endroit où l’état est stocké. Par exemple le backend local (par défaut) stocke l’état sur le disque dans un fichier JSON. D’autres backend permettent de stocker l’état à d’autres emplacement sous d’autres formats, par exemple dans un bucket AWS S3, HashiCorp Consul, Azure Blob Storage, Google Cloud Storage, Alibaba Cloud OSS, etc.

Terraform distingue deux types de backends:

Local

Remote

Bloc cloud

s3

-backend-config

TF_WORKSPACE

Lock

lock.hcl

state push/pull

data.terraform_remote_state

Un bloc data de type “terraform_remote_state” permet de récupérer les variables en sortie d’un objet d’infrastructure décrit dans un fichier tfstate distant. Il n’est pas nécessaire ici d’acquérir un lock puisqu’on est en lecture uniquement

data "terraform_remote_state" "vpc" {
  backend = "remote"

  config = {
    organization = "hashicorp"
    workspaces = {
      name = "vpc-prod"
    }
  }
}

# Terraform >= 0.12
resource "aws_instance" "foo" {
  # ...
  subnet_id = data.terraform_remote_state.vpc.outputs.subnet_id
}
# cross-referencing stacks
data "terraform_remote_state" "networking" {
  backend = "local"

  config = {
    path = "${path.module}/networking/terraform.tfstate"
  }
}