forked from pierront/but3-iac
ajout des configs terraform
This commit is contained in:
@@ -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"]
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user