TERRAFORM STATE: come sfruttare AWS S3 BUCKET per salvarlo in CLOUD!

TERRAFORM STATE: come sfruttare AWS S3 BUCKET per salvarlo in CLOUD!

Se stai lavorando con AWS ed utilizzi Terraform oggi ti voglio spiegare come salvare il Terraform State in un BUCKET su AWS S3 con una semplice modifca al tuo script tf. ⬇️

ATTENZIONE: Questo contenuto è fatto a scopo dimostrativo e viene fornito senza garanzie. Il rischio di utilizzo è a carico vostro.

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

Situazione di Partenza

Prima di spiegarvi come procedere per sfruttare la gestione del Terraform State in S3 desidero proporvi il caso base.

Nel seguente script Terraform andremo a creare un bucket di esempio.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.18.1"
    }
  }
}


# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "s3_bucket" {
  bucket = "bucket-test-s3-example"
}

Una volta lanciato lo script troveremo in locale sul nostro disco il file terraform.tfstate.

tfstate - Migrazione su S3

Per spostare la gestione del terraform.tfstate su S3 dobbiamo fare una modifica veramente semplice e minimale al nostro script.

Essendo veramente corto, ve lo riporto per integrale:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.18.1"
    }
  }

  backend "s3" {
    bucket = "bucket-test-tfstate"
    key    = "bucket-test-tfstate-file-name.tfstate"
    region = "us-east-1"
  }

}


# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "s3_bucket" {
  bucket = "bucket-test-s3-example"
}

Come noterete -a differenza di prima- possiamo trovare backend “s3” nella parte iniziale dello script.

In questo modo andremo a dire a Terraform di utilizzare il bucket indificato (deve estistere) e di salvare il file nel path speficiato in key.

Tips:

Prima di concludere l’articolo vorrei lasciarvi due suggerimenti importanti per semplificarvi il lavoro:

terraform init

Se avete una copia del terraform.tfstate in locale ed applicate la modifica indicata al primo terraform init vi troverete davanti a questo messaggio:

PS Z:\terraform-template-aws\aws-s3-terraform-state> terraform init

Initializing the backend… Do you want to copy existing state to the new backend? Pre-existing state was found while migrating the previous “local” backend to the newly configured “s3” backend. No existing state was found in the newly configured “s3” backend. Do you want to copy this state to the new “s3” backend? Enter “yes” to copy and “no” to start with an empty state.

Enter a value: yes

Successfully configured the backend “s3”! Terraform will automatically use this backend unless the backend configuration changes.

Initializing provider plugins…

  • Reusing previous version of hashicorp/aws from the dependency lock file
  • Using previously-installed hashicorp/aws v4.67.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running “terraform plan” to see any changes that are required for your infrastructure. All Terraform commands should now work.

If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.

ExecutionPolicy

Se lanciando i comandi di Terraform dovreste imbattervi in un messaggio d’errore legato ai permessi/ policy ecco il comando da lanciare:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

YouTube Video

Se vuoi vedere come gestire il tutto in maniera pratica eccoti il video associato all’articolo appena letto: