forked from pierront/but3-iac
116 lines
3.0 KiB
HCL
116 lines
3.0 KiB
HCL
// 1. VPC personnalisé
|
|
resource "google_compute_network" "vpc" {
|
|
name = "${var.project_name}-vpc"
|
|
project = var.project_name
|
|
auto_create_subnetworks = false
|
|
}
|
|
|
|
// 2. Sous-réseaux
|
|
|
|
resource "google_compute_subnetwork" "frontend" {
|
|
name = "${var.project_name}-frontend-subnet"
|
|
project = var.project_name
|
|
region = var.region
|
|
network = google_compute_network.vpc.id
|
|
ip_cidr_range = var.frontend_cidr
|
|
}
|
|
|
|
resource "google_compute_subnetwork" "backend" {
|
|
name = "${var.project_name}-backend-subnet"
|
|
project = var.project_name
|
|
region = var.region
|
|
network = google_compute_network.vpc.id
|
|
ip_cidr_range = var.backend_cidr
|
|
}
|
|
|
|
resource "google_compute_subnetwork" "database" {
|
|
name = "${var.project_name}-database-subnet"
|
|
project = var.project_name
|
|
region = var.region
|
|
network = google_compute_network.vpc.id
|
|
ip_cidr_range = var.database_cidr
|
|
}
|
|
|
|
// 3. Règles de firewall
|
|
|
|
// 3.1 HTTP/HTTPS vers frontend (depuis Internet)
|
|
resource "google_compute_firewall" "frontend_http_https" {
|
|
name = "${var.project_name}-fw-frontend-http-https"
|
|
project = var.project_name
|
|
network = google_compute_network.vpc.name
|
|
|
|
description = "Autorise HTTP/HTTPS vers les instances frontend"
|
|
|
|
direction = "INGRESS"
|
|
|
|
allow {
|
|
protocol = "tcp"
|
|
ports = ["80", "443"]
|
|
}
|
|
|
|
source_ranges = ["0.0.0.0/0"]
|
|
|
|
// Les instances frontend devront avoir ce tag
|
|
target_tags = ["frontend"]
|
|
}
|
|
|
|
// 3.2 SSH vers toutes les instances (depuis la plage fournie)
|
|
resource "google_compute_firewall" "ssh_all" {
|
|
name = "${var.project_name}-fw-ssh-all"
|
|
project = var.project_name
|
|
network = google_compute_network.vpc.name
|
|
|
|
description = "Autorise SSH vers toutes les instances du VPC"
|
|
|
|
direction = "INGRESS"
|
|
|
|
allow {
|
|
protocol = "tcp"
|
|
ports = ["22"]
|
|
}
|
|
|
|
// Une seule CIDR passée en string, on l'enferme dans une liste
|
|
source_ranges = var.ssh_source_ranges
|
|
}
|
|
|
|
// 3.3 Port 8000 de frontend vers backend
|
|
resource "google_compute_firewall" "frontend_to_backend_8000" {
|
|
name = "${var.project_name}-fw-frontend-backend-8000"
|
|
project = var.project_name
|
|
network = google_compute_network.vpc.name
|
|
|
|
description = "Autorise le trafic TCP 8000 des instances frontend vers backend"
|
|
|
|
direction = "INGRESS"
|
|
|
|
allow {
|
|
protocol = "tcp"
|
|
ports = ["8000"]
|
|
}
|
|
|
|
// Le trafic vient des instances taguées 'frontend'
|
|
source_tags = ["frontend"]
|
|
|
|
// Et cible les instances taguées 'backend'
|
|
target_tags = ["backend"]
|
|
}
|
|
|
|
// 3.4 Port 3306 de backend vers database
|
|
resource "google_compute_firewall" "backend_to_database_3306" {
|
|
name = "${var.project_name}-fw-backend-database-3306"
|
|
project = var.project_name
|
|
network = google_compute_network.vpc.name
|
|
|
|
description = "Autorise le trafic TCP 3306 des instances backend vers database"
|
|
|
|
direction = "INGRESS"
|
|
|
|
allow {
|
|
protocol = "tcp"
|
|
ports = ["3306"]
|
|
}
|
|
|
|
source_tags = ["backend"]
|
|
target_tags = ["database"]
|
|
}
|