Aller au contenu

Lab 03 : Utilisation Backend S3

Dans cet atelier vous allez configurer un backend S3 Outscale (Outscale Object Storage) pour y stocker les états de configurations Terraform (fichier terraform.tfstate).

Vous allez à la fin vérifier que Terraform stocke bien les fichiers dans le Bucket pré-défini.

  • Terraform doit être installé et fonctionnel
  • Vous devez disposez de vos identifiants API (Acces Key et Secret Access Key) pour authentifier les appels d’API effectués par Terraform.

En reprenant le lab2 :

  • Créer et configurer le Backend S3
  • Vérifier que les fichiers terraform.state ont bien été stockés dans le Bucket pré-défini

📌 NOTE
awscli doit être déjà installé et configuré avec vos identifiants d’accès à l’API Outscale. Si ce n’est pas le cas, vous pouvez suivre la documentation suivante pour l’installer. https://docs.outscale.com/en/userguide/Installing-and-Configuring-AWS-CLI.html

Commencez par créer le Bucket cible à l’aide de la commande S3API suivante :

Terminal window
aws s3api create-bucket --profile default --bucket lab03-terraform-state --endpoint https://oos.eu-west-2.outscale.com

Dans le fichier providers.tf adaptez les lignes existantes pour ajouter le backend S3 :

terraform {
required_providers {
outscale = {
source = "outscale/outscale"
version = ">= 0.12.0"
}
}
backend "s3" {
profile = "default"
bucket = "lab03-terraform-state"
key = "terraform.state"
region = "eu-west-2"
endpoints = {
s3 = "https://oos.eu-west-2.outscale.com"
iam = "https://eim.eu-west-2.outscale.com"
}
skip_region_validation = true
skip_credentials_validation = true
skip_metadata_api_check = true
skip_s3_checksum = true
}
}
provider "outscale" {
access_key_id = var.access_key_id
secret_key_id = var.secret_key_id
region = var.region
}

Maintenant, lancez la commande terraform init :

Terminal window
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/local from the dependency lock file
- Reusing previous version of outscale/outscale from the dependency lock file
- Reusing previous version of hashicorp/tls from the dependency lock file
- Using previously-installed outscale/outscale v0.12.0
- Using previously-installed hashicorp/tls v4.0.5
- Using previously-installed hashicorp/local v2.5.1
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.

Vérification des fichiers state coté Backend S3 (Bucket OOS)

Section intitulée « Vérification des fichiers state coté Backend S3 (Bucket OOS) »

Utilisez le call S3API suivant pour lister le contenu du Bucket pré-défini pour le stockage des fichiers state.

Terminal window
aws s3api list-objects-v2 --profile demo --bucket lab03-terraform-state
--endpoint https://oos.eu-west-2.outscale.com
{
"Contents": [
{
"Key": "terraform.state",
"LastModified": "2024-08-27T07:32:18.643000+00:00",
"ETag": "\"cd8ab3890f0e931362406f53d7472749\"",
"Size": 23384,
"StorageClass": "STANDARD"
}
],
"RequestCharged": null
}