From 6ee0abf34126d590442f334269f7eeca42b458c0 Mon Sep 17 00:00:00 2001 From: nelet Date: Fri, 6 Dec 2024 14:42:39 +0100 Subject: [PATCH] inshBouddah --- tp-cloud/terraform/environments/dev/main.tf | 10 +++ .../terraform/environments/dev/variables.tf | 6 ++ tp-cloud/terraform/modules/compute/main.tf | 69 +++++++++++++++++++ .../terraform/modules/compute/variables.tf | 26 +++++++ tp-cloud/terraform/modules/network/main.tf | 8 +-- tp-cloud/terraform/modules/network/outputs.tf | 11 +++ 6 files changed, 126 insertions(+), 4 deletions(-) diff --git a/tp-cloud/terraform/environments/dev/main.tf b/tp-cloud/terraform/environments/dev/main.tf index 5387a8a..be99e91 100644 --- a/tp-cloud/terraform/environments/dev/main.tf +++ b/tp-cloud/terraform/environments/dev/main.tf @@ -25,4 +25,14 @@ module "network" { backend_cidr = var.backend_cidr database_cidr = var.database_cidr ssh_source_ranges = var.ssh_source_ranges +} + +module "compute" { + source = "../../modules/compute" + + instance_type = "e2-micro" + zone ="europe-west4" + frontend_subnet_id = module.network.id_subnetwork["frontend"] + backend_subnet_id = module.network.id_subnetwork["backend"] + database_subnet_id = module.network.id_subnetwork["database"] } \ No newline at end of file diff --git a/tp-cloud/terraform/environments/dev/variables.tf b/tp-cloud/terraform/environments/dev/variables.tf index e6e7f9b..9278c7e 100644 --- a/tp-cloud/terraform/environments/dev/variables.tf +++ b/tp-cloud/terraform/environments/dev/variables.tf @@ -4,36 +4,42 @@ variable "project_name" { type = string default = "tp-1-docker" } + #- project_id (string) variable "project_id" { description = "ID du projet" type = string default = "tp-1-docker" } + # - region (string) variable "region" { description = "Région du projet" type = string default = "europe-west4" } + # - frontend_cidr (string) variable "frontend_cidr" { description = "cidr du frontend" type = string default = "10.0.1.0/24" } + # - backend_cidr (string) variable "backend_cidr" { description = "cidr du backend" type = string default = "10.0.2.0/24" } + # - database_cidr (string) variable "database_cidr" { description = "cidr du database" type = string default = "10.0.3.0/24" } + # - ssh_source_ranges (string) variable "ssh_source_ranges" { description = "Accès à internet" diff --git a/tp-cloud/terraform/modules/compute/main.tf b/tp-cloud/terraform/modules/compute/main.tf index e69de29..c92a231 100644 --- a/tp-cloud/terraform/modules/compute/main.tf +++ b/tp-cloud/terraform/modules/compute/main.tf @@ -0,0 +1,69 @@ +resource "google_compute_instance" "frontend" { + name = "ma-vm frontend" + machine_type = var.instance_type + zone = var.zone + + boot_disk { + initialize_params { + image = "debian-cloud/debian-11" + size = 10 + } + } + + network_interface { + subnetwork = var.frontend_subnet_id + access_config {} # IP publique + } + + tags = ["frontend", "ssh"] + + metadata = { + enable-oslogin = "TRUE" + } +} + +resource "google_compute_instance" "backend" { + name = "ma-vm backend" + machine_type = var.instance_type + zone = var.zone + + boot_disk { + initialize_params { + image = "debian-cloud/debian-11" + size = 10 + } + } + + network_interface { + subnetwork = var.backend_subnet_id + } + + tags = ["backend", "ssh"] + + metadata = { + enable-oslogin = "TRUE" + } +} + +resource "google_compute_instance" "database" { + name = "ma-vm database" + machine_type = var.instance_type + zone = var.zone + + boot_disk { + initialize_params { + image = "debian-cloud/debian-11" + size = 20 + } + } + + network_interface { + subnetwork = var.database_subnet_id + } + + tags = ["database", "ssh"] + + metadata = { + enable-oslogin = "TRUE" + } +} \ No newline at end of file diff --git a/tp-cloud/terraform/modules/compute/variables.tf b/tp-cloud/terraform/modules/compute/variables.tf index e69de29..6840a39 100644 --- a/tp-cloud/terraform/modules/compute/variables.tf +++ b/tp-cloud/terraform/modules/compute/variables.tf @@ -0,0 +1,26 @@ +variable "instance_type" { + description = "les types d'instances" + type = string + default = "e2-micro" +} + +variable "zone" { + description = "zone des instances" + type = string + default = "europe-west4" +} + +variable "frontend_subnet_id" { + description = "L'identifiant du subnet utilisé pour le frontend" + type = string +} + +variable "backend_subnet_id" { + description = "L'identifiant du subnet utilisé pour le backend" + type = string +} + +variable "database_subnet_id" { + description = "L'identifiant du subnet utilisé pour la database" + type = string +} \ No newline at end of file diff --git a/tp-cloud/terraform/modules/network/main.tf b/tp-cloud/terraform/modules/network/main.tf index cb2b5f9..9b1ee92 100644 --- a/tp-cloud/terraform/modules/network/main.tf +++ b/tp-cloud/terraform/modules/network/main.tf @@ -38,7 +38,7 @@ resource "google_compute_firewall" "allow_http" { } source_ranges = ["0.0.0.0/0"] - target_tags = ["web"] + target_tags = ["frontend"] } resource "google_compute_firewall" "allow_https" { @@ -51,7 +51,7 @@ resource "google_compute_firewall" "allow_https" { } source_ranges = ["0.0.0.0/0"] - target_tags = ["web"] + target_tags = ["frontend"] } resource "google_compute_firewall" "allow_ssh" { @@ -77,7 +77,7 @@ resource "google_compute_firewall" "allow_frontend_to_backend" { } source_ranges = [var.frontend_cidr] - target_tags = ["web"] + target_tags = ["backend"] } resource "google_compute_firewall" "allow-sql" { @@ -90,5 +90,5 @@ resource "google_compute_firewall" "allow-sql" { } source_ranges = [var.backend_cidr] - target_tags = ["web"] + target_tags = ["database"] } \ No newline at end of file diff --git a/tp-cloud/terraform/modules/network/outputs.tf b/tp-cloud/terraform/modules/network/outputs.tf index e69de29..f0793a5 100644 --- a/tp-cloud/terraform/modules/network/outputs.tf +++ b/tp-cloud/terraform/modules/network/outputs.tf @@ -0,0 +1,11 @@ +output "id_vpc" { + value = google_compute_network.vpc.id +} + +output "id_subnetwork" { + value = { + frontend=google_compute_network.frontend.id, + backend=google_compute_network.backend.id, + database=google_compute_network.database.id + } +} \ No newline at end of file