Compare commits

18 Commits
main ... main

Author SHA1 Message Date
105c4a17e8 ajout iam 2025-12-04 11:38:14 +01:00
dae3e1dffd ajout iam 2025-12-04 11:05:05 +01:00
fec56a2ffc ajout iam 2025-12-04 10:59:01 +01:00
afe8f23f95 ajout iam 2025-12-04 10:56:55 +01:00
267629f179 ajout iam 2025-12-04 10:37:24 +01:00
77f1cc7a93 ajout compute 2025-12-04 09:57:10 +01:00
555d5ef006 ajout compute 2025-12-04 09:54:59 +01:00
56f7e030a4 ajout compute 2025-12-04 09:50:32 +01:00
9d2e825843 ajout compute 2025-12-04 09:38:49 +01:00
bdbf44c6c3 ajout compute 2025-12-04 09:10:40 +01:00
f48c698399 ajout des configs terraform 2025-12-03 16:48:20 +01:00
f41cd5130b ajout des configs terraform 2025-12-03 16:39:42 +01:00
84d78b69e1 ajout des configs terraform 2025-12-03 16:06:54 +01:00
a587d61ebe ajout des configs terraform 2025-12-03 15:58:18 +01:00
a65ee3030a ajout des configs terraform 2025-12-03 15:57:29 +01:00
0175c91700 ajout des configs terraform 2025-12-03 15:56:20 +01:00
53b30b1010 ajout des configs terraform 2025-12-03 15:54:20 +01:00
f8a428dd6e ajout des configs terraform 2025-12-03 15:50:34 +01:00
13 changed files with 1039 additions and 0 deletions

574
TerraFormShow.txt Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
# À vous de :
# 1. Configurer le provider google
# 2. Appeler les trois modules avec les bonnes variables
# 3. Créer le fichier de configuration Ansible (template)
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 6.12.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
}
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" {
source = "../../modules/compute"
instance_type = var.instance_type
zone = var.zone
frontend_subnet_id = module.network.subnets.frontend
backend_subnet_id = module.network.subnets.backend
database_subnet_id = module.network.subnets.database
}
module "iam" {
source ="../../modules/iam"
project_id = var.project_id
ssh_public_key_path = var.ssh_public_key_path
}

View File

@@ -0,0 +1,28 @@
output "ip_internes" {
value = module.compute.ip_internes
}
output "ip_public_frontend" {
value = module.compute.ip_public_frontend
}
output "nom_instances" {
value = module.compute.nom_instances
}
output "service_account_email" {
value = module.iam.service_account_email
}
output "service_account_key" {
sensitive = true
value = module.iam.service_account_key
}
output "vpc" {
value = module.network.vpc
}
output "subnets" {
value = module.network.subnets
}

View File

@@ -0,0 +1,61 @@
# définissez toutes les variables nécessaires avec des valeurs par défaut appropriées.
variable "project_name" {
description = "Nom du projet"
type = string
default = "My First Project"
}
variable "region" {
description = "Region du projet"
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" {
description = ""
type = string
default = "0.0.0.0/0"
}
variable "project_id" {
description = "ID du projet"
type = string
default = "learned-trilogy-478713-j7"
}
variable "instance_type" {
description = "type de l'instance"
type = string
default = "e2-small"
}
variable "zone" {
description = "Nom de la zone"
type = string
default = "europe-west9-b"
}
variable "ssh_public_key_path" {
type = string
description = "Chemin vers la clé publique SSH"
default = "~/.ssh/id_ed25519.pub"
}

View File

@@ -0,0 +1,92 @@
# À vous de créer :
# 1. Instance frontend :
# - Image : debian-11
# - Disque : 10GB
# - IP publique
# - Tags : frontend, ssh
# - OS Login enabled
# 2. Instance backend :
# - Image : debian-11
# - Disque : 10GB
# - Pas d'IP publique (interne seulement)
# - Tags : backend, ssh
# - OS Login enabled
# 3. Instance database :
# - Image : debian-11
# - Disque : 20GB
# - Pas d'IP publique
# - Tags : database, ssh
# - OS Login enabled
resource "google_compute_instance" "vm_frontend" {
name = "vm-frontend"
machine_type = var.instance_type
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
size = 10
}
}
network_interface {
access_config {} # IP publique
subnetwork = var.frontend_subnet_id
}
tags = ["frontend", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}
resource "google_compute_instance" "vm_backend" {
name = "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" "vm_database" {
name = "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"
}
}

View File

@@ -0,0 +1,24 @@
# À vous d'exposer :
# 1. Les IPs internes de toutes les instances
# 2. L'IP publique du frontend
# 3. Les noms des instances
output "ip_internes" {
value = {
frontend = google_compute_instance.vm_frontend.network_interface[0].network_ip
backend = google_compute_instance.vm_backend.network_interface[0].network_ip
database = google_compute_instance.vm_database.network_interface[0].network_ip
}
}
output "ip_public_frontend" {
value = google_compute_instance.vm_frontend.network_interface[0].access_config[0].nat_ip
}
output "nom_instances" {
value = {
frontend = google_compute_instance.vm_frontend.name
backend = google_compute_instance.vm_backend.name
database = google_compute_instance.vm_database.name
}
}

View File

@@ -0,0 +1,31 @@
# À vous de définir les variables pour :
# - instance_type
# - zone
# - frontend_subnet_id
# - backend_subnet_id
# - database_subnet_id
variable "instance_type" {
description = "type de l'instance"
type = string
}
variable "zone" {
description = "Nom de la zone"
type = string
}
variable "frontend_subnet_id" {
description = "id du frontend"
type = string
}
variable "backend_subnet_id" {
description = "id du backend"
type = string
}
variable "database_subnet_id" {
description = "id du database"
type = string
}

View File

@@ -0,0 +1,27 @@
resource "google_service_account" "service_account" {
account_id = "terraform"
display_name = "terraform"
}
resource "google_service_account_key" "key" {
service_account_id = google_service_account.service_account.name
public_key_type = "TYPE_X509_PEM_FILE"
}
resource "google_project_iam_binding" "sa_viewer" {
project = var.project_id
role = "roles/viewer"
members = [
"serviceAccount:${google_service_account.service_account.email}",
]
}
# Ajout sécurité : OS Login
data "google_client_openid_userinfo" "me" {}
resource "google_os_login_ssh_public_key" "ssh_key" {
user = data.google_client_openid_userinfo.me.email
project = var.project_id
key = file(var.ssh_public_key_path)
}

View File

@@ -0,0 +1,10 @@
output "service_account_email" {
value = google_service_account.service_account.email
description = "Email du service account créé"
}
output "service_account_key" {
value = google_service_account_key.key.private_key
sensitive = true
description = "Clé privée du service account"
}

View File

@@ -0,0 +1,9 @@
variable "project_id" {
type = string
description = "ID du projet GCP"
}
variable "ssh_public_key_path" {
type = string
description = "Chemin vers la clé publique SSH"
}

View File

@@ -0,0 +1,88 @@
# À vous de créer :
# 1. Un VPC personnalisé avec auto_create_subnetworks = false
# 2. Trois sous-réseaux (frontend, backend, database)
# 3. Règles de firewall :
# - HTTP/HTTPS vers frontend
# - SSH vers toutes les instances
# - Port 8000 de frontend vers backend
# - Port 3306 de backend vers database
# VPC
resource "google_compute_network" "vpc_terraform" {
name = "vpc-terraform"
auto_create_subnetworks = false
}
# Sous-réseau
resource "google_compute_subnetwork" "subnet_frontend" {
name = "frontend"
network = google_compute_network.vpc_terraform.id
ip_cidr_range = var.frontend_cidr
region = var.region
}
resource "google_compute_subnetwork" "subnet_backend" {
name = "backend"
network = google_compute_network.vpc_terraform.id
ip_cidr_range = var.backend_cidr
region = var.region
}
resource "google_compute_subnetwork" "subnet_database" {
name = "database"
network = google_compute_network.vpc_terraform.id
ip_cidr_range = var.database_cidr
region = var.region
}
resource "google_compute_firewall" "allow_user_frontend" {
name = "allow-user-frontend"
network = google_compute_network.vpc_terraform.id
allow {
protocol = "tcp"
ports = ["80", "443"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["frontend"]
}
resource "google_compute_firewall" "allow_frontend_backend" {
name = "allow-frontend-backend"
network = google_compute_network.vpc_terraform.id
allow {
protocol = "tcp"
ports = ["8000"]
}
source_tags = ["frontend"]
target_tags = ["backend"]
}
resource "google_compute_firewall" "allow_ssh_all" {
name = "allow-ssh-all"
network = google_compute_network.vpc_terraform.id
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["ssh"]
}
resource "google_compute_firewall" "allow_backend_database" {
name = "allow-backend-database"
network = google_compute_network.vpc_terraform.id
allow {
protocol = "tcp"
ports = ["3306"]
}
source_tags = ["backend"]
target_tags = ["database"]
}

View File

@@ -0,0 +1,15 @@
# À vous d'exposer :
# 1. L'ID du VPC
# 2. Les IDs des sous-réseaux sous forme de map
output "vpc" {
value = google_compute_network.vpc_terraform.id
}
output "subnets" {
value = {
frontend = google_compute_subnetwork.subnet_frontend.id
backend = google_compute_subnetwork.subnet_backend.id
database = google_compute_subnetwork.subnet_database.id
}
}

View File

@@ -0,0 +1,37 @@
# À 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
}
variable "region" {
description = "Region du projet"
type = string
}
variable "frontend_cidr" {
description = "CIDR for frontend subnet"
type = string
}
variable "backend_cidr" {
description = "CIDR for backend subnet"
type = string
}
variable "database_cidr" {
description = "CIDR for database subnet"
type = string
}
variable "ssh_source_ranges" {
description = ""
type = string
}