Pré-requis : Il faut avoir initialiser google cloud SDK (ici)
Dans un premier temps, connectez-vous sur votre compte GCP, puis :
terraform-sa
par exemple)~/.config/gcloud
:❯ mv terraform-gcp-374614-ecdd4af1dcf5.json ~/.config/gcloud/terraform-gcp.json
Une fois effectué. Nous allons créer l'architecture suivante :
❯ tree
.
├── main.tf
├── outputs.tf
└── variables.tf
1 directory, 3 files
Comme vu précédemment, le main.tf
va contenir les instructions principales :
terraform {
required_providers {
google = ">= 4.48.0"
}
}
provider "google" {
project = var.project
credentials = file(var.credentials_path)
region = var.region
zone = var.zone
}
# [START compute_network]
resource "google_compute_network" "vpc_network" {
name = var.network_name
auto_create_subnetworks = var.auto_create_subnetworks
mtu = var.network_mtu
}
resource "google_compute_subnetwork" "default" {
name = var.subnetwork_name
ip_cidr_range = var.subnetwork_cidr
region = var.region
network = google_compute_network.vpc_network.id
}
# [END compute_network]
# [START compute_instance]
# Create a single Compute Engine instance
resource "google_compute_instance" "default" {
name = var.machine_name
machine_type = var.machine_type
zone = var.zone
tags = var.machine_tags
boot_disk {
initialize_params {
image = var.boot_disk_image
}
}
network_interface {
subnetwork = google_compute_subnetwork.default.id
access_config {
# Include this section to give the VM an external IP address
}
}
}
# [END compute_instance]
Ces instructions ont été faites à partir de la documentation du provider GCP ici.
Maintenant on va définir les variables, voici quelques variables :
Fichier source ici
variable "project" {}
variable "credentials_path" {
type = string
default = "~/.config/gcloud/terraform-gcp.json"
}
variable "network_name" {
type = string
default = "my-custom-mode-network"
}
variable "network_mtu" {
type = number
default = 1460
}
variable "auto_create_subnetworks" {
type = bool
default = false
}
variable "machine_tags" {
type = list
default = []
}
...
Une fois le fichier variables.tf
crée on va définir l'output :
output "google_compute_instance" {
value = google_compute_instance.default.network_interface.0.access_config.0.nat_ip
}
Cette value a pu être crée en testant la création sans output dans un premier temps, puis en faisant la commande tf show
:
❯ tf show
google_compute_instance.default:
resource "google_compute_instance" "default" {
network_interface {
access_config {
nat_ip = "X.X.X.X"
}
}
}
Pour exécuter le terraform :
❯ tf init
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/google from the dependency lock file
- Installing hashicorp/google v4.48.0...
- Installed hashicorp/google v4.48.0 (signed by HashiCorp)
Terraform has been successfully initialized!
Puis tf plan
:
❯ tf plan
var.machine_name
Enter a value: test-wiki
var.project
Enter a value: terraform-gcp-374614
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# google_compute_instance.default will be created
+ resource "google_compute_instance" "default" {
+ can_ip_forward = false
+ cpu_platform = (known after apply)
...
Et enfin, si le plan vous convient tf apply -auto-approve
:
❯ tf apply -auto-approve
var.machine_name
Enter a value: test-wiki
var.project
Enter a value: terraform-gcp-374614
...
Plan: 3 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ google_compute_instance = (known after apply)
google_compute_network.vpc_network: Creating...
google_compute_network.vpc_network: Still creating... [10s elapsed]
google_compute_network.vpc_network: Still creating... [20s elapsed]
google_compute_network.vpc_network: Creation complete after 22s [id=projects/terraform-gcp-374614/global/networks/my-custom-mode-network]
google_compute_subnetwork.default: Creating...
...
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Outputs:
google_compute_instance = "X.X.X.X"
Si tout s'est bien passé, tout devrait être crée sur l'interface GCP
Pour tout supprimer :
❯ tf destroy
...
Destroy complete! Resources: 3 destroyed.