From 91d297cb215c59ea5c7fd349a164460a1fb3ed68 Mon Sep 17 00:00:00 2001 From: raphael Date: Wed, 3 Dec 2025 16:15:42 +0100 Subject: [PATCH] merci gpt XD --- terraform/environments/dev/main.tf | 68 ++++++++++++++++++++ terraform/environments/dev/variables.tf | 34 ++++++++++ terraform/modules/compute/main.tf | 85 +++++++++++++++++++++++++ terraform/modules/compute/outputs.tf | 19 ++++++ terraform/modules/compute/variables.tf | 24 +++++++ terraform/modules/iam/main.tf | 24 +++++++ terraform/modules/iam/outputs.tf | 8 +++ terraform/modules/iam/variables.tf | 4 ++ terraform/modules/network/variables.tf | 12 ---- 9 files changed, 266 insertions(+), 12 deletions(-) diff --git a/terraform/environments/dev/main.tf b/terraform/environments/dev/main.tf index e69de29..21324b9 100644 --- a/terraform/environments/dev/main.tf +++ b/terraform/environments/dev/main.tf @@ -0,0 +1,68 @@ +terraform { + required_providers { + google = { + source = "hashicorp/google" + version = "~> 6.12.0" + } + } +} + +provider "google" { + project = var.project_id + region = var.region +} + +# ------------------------ +# Module NETWORK +# ------------------------ + +module "network" { + source = "../../modules/network" + project_name = var.project_name + region = var.region + frontend_cidr = var.frontend_cidr + backend_cidr = var.backend_cidr + database_cidr = var.database_cidr + ssh_source_ranges = var.ssh_source_ranges +} + +# ------------------------ +# Module COMPUTE +# ------------------------ + +module "compute" { + source = "../../modules/compute" + instance_type = var.instance_type + zone = var.zone + frontend_subnet_id = module.network.subnet_ids["frontend"] + backend_subnet_id = module.network.subnet_ids["backend"] + database_subnet_id = module.network.subnet_ids["database"] +} + +# ------------------------ +# Module IAM +# ------------------------ + +module "iam" { + source = "../../modules/iam" + project_id = var.project_id +} + +# ------------------------ +# Fichier template Ansible +# ------------------------ + +data "templatefile" "ansible_cfg" { + template = file("${path.module}/ansible.tpl") + + vars = { + frontend_ip = module.compute.frontend_public_ip + backend_ip = module.compute.internal_ips["backend"] + db_ip = module.compute.internal_ips["database"] + } +} + +resource "local_file" "ansible_file" { + filename = "${path.module}/ansible_inventory.ini" + content = data.templatefile.ansible_cfg.rendered +} diff --git a/terraform/environments/dev/variables.tf b/terraform/environments/dev/variables.tf index e69de29..3e16a00 100644 --- a/terraform/environments/dev/variables.tf +++ b/terraform/environments/dev/variables.tf @@ -0,0 +1,34 @@ +variable "project_name" { + default = "test" +} +variable "project_id" { + default = "my-gcp-project" +} + +variable "region" { + default = "europe-west1" +} + +variable "zone" { + default = "europe-west1-b" +} + +variable "instance_type" { + default = "e2-medium" +} + +variable "frontend_cidr" { + default = "10.0.1.0/24" +} + +variable "backend_cidr" { + default = "10.0.2.0/24" +} + +variable "database_cidr" { + default = "10.0.3.0/24" +} + +variable "ssh_source_ranges" { + default = ["0.0.0.0/0"] +} diff --git a/terraform/modules/compute/main.tf b/terraform/modules/compute/main.tf index e69de29..8764d27 100644 --- a/terraform/modules/compute/main.tf +++ b/terraform/modules/compute/main.tf @@ -0,0 +1,85 @@ +locals { + image = "projects/debian-cloud/global/images/family/debian-11" +} + +# ------------------------ +# FRONTEND instance +# ------------------------ + +resource "google_compute_instance" "frontend" { + name = "frontend" + machine_type = var.instance_type + zone = var.zone + + tags = ["frontend", "ssh"] + + boot_disk { + initialize_params { + size = 10 + image = local.image + } + } + + network_interface { + subnetwork = var.frontend_subnet_id + access_config {} + } + + metadata = { + enable-oslogin = "TRUE" + } +} + +# ------------------------ +# BACKEND instance +# ------------------------ + +resource "google_compute_instance" "backend" { + name = "backend" + machine_type = var.instance_type + zone = var.zone + + tags = ["backend", "ssh"] + + boot_disk { + initialize_params { + size = 10 + image = local.image + } + } + + network_interface { + subnetwork = var.backend_subnet_id + } + + metadata = { + enable-oslogin = "TRUE" + } +} + +# ------------------------ +# DATABASE instance +# ------------------------ + +resource "google_compute_instance" "database" { + name = "database" + machine_type = var.instance_type + zone = var.zone + + tags = ["database", "ssh"] + + boot_disk { + initialize_params { + size = 20 + image = local.image + } + } + + network_interface { + subnetwork = var.database_subnet_id + } + + metadata = { + enable-oslogin = "TRUE" + } +} \ No newline at end of file diff --git a/terraform/modules/compute/outputs.tf b/terraform/modules/compute/outputs.tf index e69de29..0406b85 100644 --- a/terraform/modules/compute/outputs.tf +++ b/terraform/modules/compute/outputs.tf @@ -0,0 +1,19 @@ +output "internal_ips" { + value = { + frontend = google_compute_instance.frontend.network_interface[0].network_ip + backend = google_compute_instance.backend.network_interface[0].network_ip + database = google_compute_instance.database.network_interface[0].network_ip + } +} + +output "frontend_public_ip" { + value = google_compute_instance.frontend.network_interface[0].access_config[0].nat_ip +} + +output "instance_names" { + value = [ + google_compute_instance.frontend.name, + google_compute_instance.backend.name, + google_compute_instance.database.name + ] +} diff --git a/terraform/modules/compute/variables.tf b/terraform/modules/compute/variables.tf index e69de29..330d7e5 100644 --- a/terraform/modules/compute/variables.tf +++ b/terraform/modules/compute/variables.tf @@ -0,0 +1,24 @@ +variable "instance_type" { + description = "instance_type" + type = string +} + +variable "zone" { + description = "zone" + type = string +} + +variable "frontend_subnet_id" { + description = "frontend_subnet_id" + type = string +} + +variable "backend_subnet_id" { + description = "backend_subnet_id" + type = string +} + +variable "database_subnet_id" { + description = "database_subnet_id" + type = string +} \ No newline at end of file diff --git a/terraform/modules/iam/main.tf b/terraform/modules/iam/main.tf index e69de29..0191b43 100644 --- a/terraform/modules/iam/main.tf +++ b/terraform/modules/iam/main.tf @@ -0,0 +1,24 @@ +# Service Account Terraform +resource "google_service_account" "terraform_sa" { + account_id = "terraform-admin" + display_name = "Terraform Admin" +} + +# Key +resource "google_service_account_key" "terraform_sa_key" { + service_account_id = google_service_account.terraform_sa.name +} + +# IAM roles nécessaires +resource "google_project_iam_member" "terraform_roles" { + project = var.project_id + role = "roles/owner" + member = "serviceAccount:${google_service_account.terraform_sa.email}" +} + +# Activation OS Login pour SSH +resource "google_os_login_ssh_public_key" "ssh_key" { + user = "raphael.hochlaf@gmail.com" + key = file("~/.ssh/id_rsa.pub") + project = var.project_id +} diff --git a/terraform/modules/iam/outputs.tf b/terraform/modules/iam/outputs.tf index e69de29..3dc4f5e 100644 --- a/terraform/modules/iam/outputs.tf +++ b/terraform/modules/iam/outputs.tf @@ -0,0 +1,8 @@ +output "service_account_email" { + value = google_service_account.terraform_sa.email +} + +output "service_account_private_key" { + value = google_service_account_key.terraform_sa_key.private_key + sensitive = true +} diff --git a/terraform/modules/iam/variables.tf b/terraform/modules/iam/variables.tf index e69de29..daa3e47 100644 --- a/terraform/modules/iam/variables.tf +++ b/terraform/modules/iam/variables.tf @@ -0,0 +1,4 @@ +variable "project_id" { + description = "project_id" + type = string +} diff --git a/terraform/modules/network/variables.tf b/terraform/modules/network/variables.tf index 28e7b30..38d64e6 100644 --- a/terraform/modules/network/variables.tf +++ b/terraform/modules/network/variables.tf @@ -1,39 +1,27 @@ -# À vous de définir les variables pour : -# - project_name (string) -# - region (string) -# - frontend_cidr (string) -# - backend_cidr (string) -# - database_cidr (string) -# - ssh_source_ranges (string) variable "project_name" { description = "Nom du projet" type = string - default = "Test IAC" } variable "region" { description = "Nom de la région" type = string - default = "europe-west9" } variable "frontend_cidr" { description = "CIDR for frontend subnet" type = string - default = "10.0.1.0/24" } variable "backend_cidr" { description = "CIDR for backend subnet" type = string - default = "10.0.2.0/24" } variable "database_cidr" { description = "CIDR for database subnet" type = string - default = "10.0.3.0/24" } variable "ssh_source_ranges" {