Files
but3-iac/terraform/modules/network/main.tf

88 lines
2.1 KiB
Terraform
Raw Normal View History

2025-12-04 10:15:36 +01:00
# À 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-2"
2025-12-03 17:10:48 +01:00
auto_create_subnetworks = false
}
2025-12-04 10:03:07 +01:00
# Sous-réseau
2025-12-04 10:15:36 +01:00
resource "google_compute_subnetwork" "subnet_frontend" {
name = "frontend-2"
network = google_compute_network.vpc_terraform.id
2025-12-03 17:10:48 +01:00
ip_cidr_range = var.frontend_cidr
region = var.region
}
2025-12-04 10:15:36 +01:00
resource "google_compute_subnetwork" "subnet_backend" {
name = "backend-2"
network = google_compute_network.vpc_terraform.id
2025-12-03 17:10:48 +01:00
ip_cidr_range = var.backend_cidr
region = var.region
}
2025-12-04 10:15:36 +01:00
resource "google_compute_subnetwork" "subnet_database" {
name = "database-2"
network = google_compute_network.vpc_terraform.id
2025-12-03 17:10:48 +01:00
ip_cidr_range = var.database_cidr
region = var.region
}
2025-12-04 10:15:36 +01:00
resource "google_compute_firewall" "allow_user_frontend" {
name = "allow-user-frontend-2"
network = google_compute_network.vpc_terraform.id
2025-12-04 09:32:09 +01:00
2025-12-04 10:15:36 +01:00
allow {
2025-12-04 10:03:07 +01:00
protocol = "tcp"
2025-12-04 10:15:36 +01:00
ports = ["80", "443"]
2025-12-04 10:03:07 +01:00
}
source_ranges = ["0.0.0.0/0"]
2025-12-04 10:15:36 +01:00
target_tags = ["frontend"]
2025-12-04 10:03:07 +01:00
}
2025-12-04 09:32:09 +01:00
2025-12-04 10:15:36 +01:00
resource "google_compute_firewall" "allow_frontend_backend" {
name = "allow-frontend-backend-2"
network = google_compute_network.vpc_terraform.id
2025-12-04 09:32:09 +01:00
2025-12-04 10:15:36 +01:00
allow {
2025-12-03 17:10:48 +01:00
protocol = "tcp"
2025-12-04 10:15:36 +01:00
ports = ["8000"]
2025-12-03 17:10:48 +01:00
}
2025-12-04 10:15:36 +01:00
source_tags = ["frontend"]
target_tags = ["backend"]
2025-12-03 17:10:48 +01:00
}
2025-12-04 10:15:36 +01:00
resource "google_compute_firewall" "allow_ssh_all" {
name = "allow-ssh-all-2"
network = google_compute_network.vpc_terraform.id
2025-12-03 17:10:48 +01:00
2025-12-04 10:15:36 +01:00
allow {
2025-12-03 17:10:48 +01:00
protocol = "tcp"
2025-12-04 10:15:36 +01:00
ports = ["22"]
2025-12-03 17:10:48 +01:00
}
2025-12-04 10:15:36 +01:00
source_ranges = ["0.0.0.0/0"]
target_tags = ["ssh"]
2025-12-03 17:10:48 +01:00
}
2025-12-04 10:15:36 +01:00
resource "google_compute_firewall" "allow_backend_database" {
name = "allow-backend-database-2"
network = google_compute_network.vpc_terraform.id
2025-12-03 17:10:48 +01:00
2025-12-04 10:15:36 +01:00
allow {
2025-12-03 17:10:48 +01:00
protocol = "tcp"
2025-12-04 09:59:27 +01:00
ports = ["3306"]
2025-12-03 17:10:48 +01:00
}
2025-12-04 09:59:27 +01:00
source_tags = ["backend"]
2025-12-04 10:15:36 +01:00
target_tags = ["database"]
}