Terraform permet de faire du management d'infrastructure :
Terraform :
.tf
Lors du déploiement d'une infrastructure terraform génère un fichier state
. Ce fichier state permet :
terraform.tfstate
tfstate
ne doit pas être push sur un Git, car il pourrait y avoir des conflits --> C'est le drameterraform plan
va analyser en fonction du tfstate
quels sont les changements à faire.Commandes de bases :
Commande | Description |
---|---|
init | Prepare your working directory for other commands |
validate | Check whether the configuration is valid |
plan | Show changes required by the current configuration |
apply | Create or update infrastructure |
destroy | Destroy previously-created infrastructure |
import | Associate existing infrastructure with a Terraform resource |
refresh | Update the state to match remote systems |
show | Show the current state or a saved plan |
Pour l'installer se référer à l'installation. Dans mon cas, je ferai l'installation pour macOS :
❯ brew tap hashicorp/tap
❯ brew install hashicorp/tap/terraform
❯ terraform
Usage: terraform [global options] <subcommand> [args]
The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
Main commands:
init Prepare your working directory for other commands
validate Check whether the configuration is valid
plan Show changes required by the current configuration
...
On va maintenant créer notre premier projet :
❯ cd hello/
❯ terraform init
Terraform initialized in an empty directory!
The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.
Comme indiqué, nous allons créer notre premier fichier .tf
:
❯ vim main.tf
❯ cat main.tf
output "ma-variable" {
value = "hello Wiki !"
}
On va maintenant appliquer la configuration :
❯ terraform plan
Changes to Outputs:
+ ma-variable = "hello Wiki !"
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
❯ terraform apply
Changes to Outputs:
+ ma-variable = "hello Wiki !"
You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
ma-variable = "hello Wiki !"
❯ ls
main.tf terraform.tfstate
On voit que cela nous a sorti l'output et ainsi crée un terraform.tfstate
. Voici les informations qu'il y a dans un .tfstate
:
❯ cat terraform.tfstate
{
"version": 4,
"terraform_version": "1.3.7",
"serial": 1,
"lineage": "388055d3-37ae-963a-07cc-867c5268364a",
"outputs": {
"ma-variable": {
"value": "hello Wiki !",
"type": "string"
}
},
"resources": [],
"check_results": null
}
Maintenant que nous avons installé Terraform et crée un petit fichier .tf
nous allons rentrer un peu plus dans les détails.