ajout des configs terraform

This commit is contained in:
2025-12-03 16:39:42 +01:00
parent 84d78b69e1
commit f41cd5130b
5 changed files with 115 additions and 154 deletions

View File

@@ -1,8 +1,13 @@
# À 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 = "~> 5.0"
version = "~> 6.12.0"
}
}
}
@@ -12,50 +17,12 @@ provider "google" {
region = var.region
}
module "iam" {
source = "../../modules/iam"
project_id = var.project_id
gcp_user_email = var.gcp_user_email
ssh_public_key = var.ssh_public_key
}
module "network" {
source = "../../modules/network"
project_id = var.project_id
region = var.region
frontend_cidr = var.frontend_cidr
backend_cidr = var.backend_cidr
database_cidr = var.database_cidr
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"
frontend_subnet_id = module.network.subnets_map.frontend
backend_subnet_id = module.network.subnets_map.backend
database_subnet_id = module.network.subnets_map.database
project_id = var.project_id
instance_type = var.instance_type
zone = var.zone
depends_on = [module.iam]
}
data "template_file" "ansible_cfg_tpl" {
template = file("../../templates/ansible.cfg.tpl")
vars = {
# On utilise les IPs internes pour les deux tiers privés
frontend_public_ip = module.compute.frontend_public_ip
backend_ip = module.compute.internal_ips.backend
database_ip = module.compute.internal_ips.database
}
}
resource "local_file" "ansible_inventory" {
content = data.template_file.ansible_cfg_tpl.rendered
filename = "ansible-inventory.cfg"
}

View File

@@ -1,56 +1,43 @@
variable "project_id" {
description = "L'ID du projet GCP."
# 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 = "Région de déploiement."
description = "Region du projet"
type = string
default = "europe-west1"
}
variable "zone" {
description = "Zone de déploiement."
type = string
default = "europe-west1-b"
}
variable "instance_type" {
description = "Type de machine pour les instances."
type = string
default = "e2-small"
default = "europe-west9"
}
variable "frontend_cidr" {
description = "Plage CIDR du frontend."
description = "CIDR for frontend subnet"
type = string
default = "10.10.1.0/24"
default = "10.0.1.0/24"
}
variable "backend_cidr" {
description = "Plage CIDR du backend."
description = "CIDR for backend subnet"
type = string
default = "10.10.2.0/24"
default = "10.0.2.0/24"
}
variable "database_cidr" {
description = "Plage CIDR de la base de données."
description = "CIDR for database subnet"
type = string
default = "10.10.3.0/24"
default = "10.0.3.0/24"
}
variable "ssh_source_ranges" {
description = "Plages CIDR autorisées pour l'accès SSH."
type = list(string)
default = ["0.0.0.0/0"]
description = ""
type = string
default = "0.0.0.0/0"
}
variable "gcp_user_email" {
description = "Votre adresse e-mail GCP (pour OS Login)."
type = string
}
variable "ssh_public_key" {
description = "Votre clé publique SSH (contenu du fichier ~/.ssh/id_ed25519.pub)."
variable "project_id" {
description = "ID du projet"
type = string
default = "learned-trilogy-478713-j7 "
}

View File

@@ -1,86 +1,88 @@
resource "google_compute_network" "vpc_network" {
project = var.project_id
name = "three-tier-vpc"
# À 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
routing_mode = "REGIONAL"
}
resource "google_compute_subnetwork" "frontend_subnet" {
project = var.project_id
name = "frontend-subnet"
# 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
network = google_compute_network.vpc_network.id
region = var.region
}
resource "google_compute_subnetwork" "backend_subnet" {
project = var.project_id
name = "backend-subnet"
resource "google_compute_subnetwork" "subnet_backend" {
name = "backend"
network = google_compute_network.vpc_terraform.id
ip_cidr_range = var.backend_cidr
region = var.region
network = google_compute_network.vpc_network.id
region = var.region
}
resource "google_compute_subnetwork" "database_subnet" {
project = var.project_id
name = "database-subnet"
resource "google_compute_subnetwork" "subnet_database" {
name = "database"
network = google_compute_network.vpc_terraform.id
ip_cidr_range = var.database_cidr
region = var.region
network = google_compute_network.vpc_network.id
region = var.region
}
resource "google_compute_firewall" "allow_web" {
project = var.project_id
name = "allow-http-https-frontend"
network = google_compute_network.vpc_network.name
direction = "INGRESS"
source_ranges = ["0.0.0.0/0"]
target_tags = ["frontend"]
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_ssh" {
project = var.project_id
name = "allow-ssh-all"
network = google_compute_network.vpc_network.name
direction = "INGRESS"
source_ranges = var.ssh_source_ranges
target_tags = ["ssh"]
allow {
protocol = "tcp"
ports = ["22"]
}
}
resource "google_compute_firewall" "allow_frontend_to_backend" {
project = var.project_id
name = "allow-frontend-to-backend-8000"
network = google_compute_network.vpc_network.name
direction = "INGRESS"
source_tags = ["frontend"]
target_tags = ["backend"]
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_backend_to_database" {
project = var.project_id
name = "allow-backend-to-database-3306"
network = google_compute_network.vpc_network.name
direction = "INGRESS"
source_tags = ["backend"]
target_tags = ["database"]
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

@@ -1,18 +1,15 @@
output "vpc_id" {
description = "L'ID du VPC créé."
value = google_compute_network.vpc_network.id
# À vous d'exposer :
# 1. L'ID du VPC
# 2. Les IDs des sous-réseaux sous forme de map
output "vpc_terraform_output" {
value = google_compute_network.vpc_terraform.id
}
output "subnets_map" {
description = "Map des IDs des sous-réseaux."
value = {
frontend = google_compute_subnetwork.frontend_subnet.id
backend = google_compute_subnetwork.backend_subnet.id
database = google_compute_subnetwork.database_subnet.id
}
}
output "network_name" {
description = "Le nom du VPC créé."
value = google_compute_network.vpc_network.name
output "list_id" {
value = {
frontend = google_compute_subnetwork.subnet_frontend.id
backend = google_compute_subnetwork.subnet_backend.id
database = google_compute_subnetwork.subnet_database.id
}
}

View File

@@ -1,29 +1,37 @@
variable "project_id" {
description = "L'ID du projet GCP."
# À 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 = "La région GCP pour les sous-réseaux."
description = "Region du projet"
type = string
}
variable "frontend_cidr" {
description = "Plage CIDR pour le sous-réseau frontend (public)."
description = "CIDR for frontend subnet"
type = string
}
variable "backend_cidr" {
description = "Plage CIDR pour le sous-réseau backend (privé)."
description = "CIDR for backend subnet"
type = string
}
variable "database_cidr" {
description = "Plage CIDR pour le sous-réseau base de données (privé)."
description = "CIDR for database subnet"
type = string
}
variable "ssh_source_ranges" {
description = "Plages CIDR autorisées pour l'accès SSH (par exemple, ['0.0.0.0/0'])."
type = list(string)
description = ""
type = string
}