commit final tp Terraform
This commit is contained in:
		@@ -1,68 +1,94 @@
 | 
			
		||||
#réation du réseau VPC
 | 
			
		||||
resource "google_compute_network" "my_vpc" {
 | 
			
		||||
  name                    = "mon-vpc"
 | 
			
		||||
# VPC
 | 
			
		||||
resource "google_compute_network" "vpc" {
 | 
			
		||||
  name                    = "myvpc"
 | 
			
		||||
  auto_create_subnetworks = false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Création des sous-réseaux
 | 
			
		||||
resource "google_compute_subnetwork" "frontend" {
 | 
			
		||||
  name          = "frontend"
 | 
			
		||||
  ip_cidr_range = "10.0.1.0/24"
 | 
			
		||||
  region        = google_compute_network.my_vpc.region
 | 
			
		||||
  network       = google_compute_network.my_vpc.id
 | 
			
		||||
# Sous-réseau
 | 
			
		||||
resource "google_compute_subnetwork" "frontend_subnet" {
 | 
			
		||||
  name          = "frontend-subnet"
 | 
			
		||||
  network       = google_compute_network.vpc.id
 | 
			
		||||
  ip_cidr_range = var.frontend_cidr
 | 
			
		||||
  region        = var.region
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "google_compute_subnetwork" "backend" {
 | 
			
		||||
  name          = "backend"
 | 
			
		||||
  ip_cidr_range = "10.0.2.0/24"
 | 
			
		||||
  region        = google_compute_network.my_vpc.region
 | 
			
		||||
  network       = google_compute_network.my_vpc.id
 | 
			
		||||
# Sous-réseau
 | 
			
		||||
resource "google_compute_subnetwork" "backend_subnet" {
 | 
			
		||||
  name          = "backend-subnet"
 | 
			
		||||
  network       = google_compute_network.vpc.id
 | 
			
		||||
  ip_cidr_range = var.backend_cidr
 | 
			
		||||
  region        = var.region
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "google_compute_subnetwork" "database" {
 | 
			
		||||
  name          = "database"
 | 
			
		||||
  ip_cidr_range = "10.0.3.0/24"
 | 
			
		||||
  region        = google_compute_network.my_vpc.region
 | 
			
		||||
  network       = google_compute_network.my_vpc.id
 | 
			
		||||
# Sous-réseau
 | 
			
		||||
resource "google_compute_subnetwork" "database_subnet" {
 | 
			
		||||
  name          = "database-subnet"
 | 
			
		||||
  network       = google_compute_network.vpc.id
 | 
			
		||||
  ip_cidr_range = var.database_cidr
 | 
			
		||||
  region        = var.region
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Création des règles de firewall
 | 
			
		||||
resource "google_compute_firewall" "allow_http_https" {
 | 
			
		||||
  name       = "allow-http-https"
 | 
			
		||||
  network    = google_compute_network.my_vpc.id
 | 
			
		||||
  allowed    = [
 | 
			
		||||
    { IPProtocol = "tcp", ports = ["80", "443"] },
 | 
			
		||||
  ]
 | 
			
		||||
resource "google_compute_firewall" "allow_http" {
 | 
			
		||||
  name    = "allow-http"
 | 
			
		||||
  network = google_compute_network.vpc.id
 | 
			
		||||
 | 
			
		||||
  allow {
 | 
			
		||||
    protocol = "tcp"
 | 
			
		||||
    ports    = ["80"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  source_ranges = ["0.0.0.0/0"]
 | 
			
		||||
  target_tags   = ["web"]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "google_compute_firewall" "allow_https" {
 | 
			
		||||
  name    = "allow-https"
 | 
			
		||||
  network = google_compute_network.vpc.id
 | 
			
		||||
 | 
			
		||||
  allow {
 | 
			
		||||
    protocol = "tcp"
 | 
			
		||||
    ports    = ["443"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  source_ranges = ["0.0.0.0/0"]
 | 
			
		||||
  target_tags   = ["web"]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "google_compute_firewall" "allow_ssh" {
 | 
			
		||||
  name       = "allow-ssh"
 | 
			
		||||
  network    = google_compute_network.my_vpc.id
 | 
			
		||||
  allowed    = [
 | 
			
		||||
    { IPProtocol = "tcp", ports = ["22"] },
 | 
			
		||||
  ]
 | 
			
		||||
  name    = "allow-ssh"
 | 
			
		||||
  network = google_compute_network.vpc.id
 | 
			
		||||
 | 
			
		||||
  allow {
 | 
			
		||||
    protocol = "tcp"
 | 
			
		||||
    ports    = ["22"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  source_ranges = [var.ssh_source_ranges]
 | 
			
		||||
  target_tags   = ["ssh"]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "google_compute_firewall" "frontend_to_backend" {
 | 
			
		||||
  name       = "frontend-to-backend"
 | 
			
		||||
  network    = google_compute_network.my_vpc.id
 | 
			
		||||
  allowed    = [
 | 
			
		||||
    { IPProtocol = "tcp", ports = ["8000"] },
 | 
			
		||||
  ]
 | 
			
		||||
  source_ranges = [
 | 
			
		||||
    google_compute_subnetwork.frontend.ip_cidr_range
 | 
			
		||||
  ]
 | 
			
		||||
  target_tags = ["backend"]
 | 
			
		||||
resource "google_compute_firewall" "allow_frontend_to_backend" {
 | 
			
		||||
  name    = "allow-frontend-to-backend"
 | 
			
		||||
  network = google_compute_network.vpc.id
 | 
			
		||||
 | 
			
		||||
  allow {
 | 
			
		||||
    protocol = "tcp"
 | 
			
		||||
    ports    = ["8000"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  source_ranges = [var.frontend_cidr]
 | 
			
		||||
  target_tags   = ["web"]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
resource "google_compute_firewall" "backend_to_database" {
 | 
			
		||||
  name       = "backend-to-database"
 | 
			
		||||
  network    = google_compute_network.my_vpc.id
 | 
			
		||||
  allowed    = [
 | 
			
		||||
    { IPProtocol = "tcp", ports = ["3306"] },
 | 
			
		||||
  ]
 | 
			
		||||
  source_ranges = [
 | 
			
		||||
    google_compute_subnetwork.backend.ip_cidr_range
 | 
			
		||||
  ]
 | 
			
		||||
  target_tags = ["database"] 
 | 
			
		||||
resource "google_compute_firewall" "allow-sql" {
 | 
			
		||||
  name    = "allow-sql"
 | 
			
		||||
  network = google_compute_network.vpc.id
 | 
			
		||||
 | 
			
		||||
  allow {
 | 
			
		||||
    protocol = "tcp"
 | 
			
		||||
    ports    = ["3306"]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  source_ranges = [var.backend_cidr]
 | 
			
		||||
  target_tags   = ["web"]
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,11 @@
 | 
			
		||||
output "vpc_id" {
 | 
			
		||||
  value = google_compute_network.my_vpc.id
 | 
			
		||||
  description = "ID du VPC créé"
 | 
			
		||||
output "id_vpc" {
 | 
			
		||||
  value = google_compute_network.vpc.id
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
output "subnet_ids" {
 | 
			
		||||
  value = {
 | 
			
		||||
    frontend = google_compute_subnetwork.frontend.id
 | 
			
		||||
    backend  = google_compute_subnetwork.backend.id
 | 
			
		||||
    database = google_compute_subnetwork.database.id
 | 
			
		||||
  }
 | 
			
		||||
  description = "Map contenant les IDs des sous-réseaux"
 | 
			
		||||
output "id_subnetwork" {
 | 
			
		||||
    value = {
 | 
			
		||||
        frontend = google_compute_subnetwork.frontend_subnet.id,
 | 
			
		||||
        backend = google_compute_subnetwork.backend_subnet.id,
 | 
			
		||||
        database = google_compute_subnetwork.database_subnet.id
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,16 +1,15 @@
 | 
			
		||||
# modules/network/variables.tf
 | 
			
		||||
 | 
			
		||||
variable "project_name" {
 | 
			
		||||
  description = "Nom du projet Google Cloud"
 | 
			
		||||
  description = "Nom du projet"
 | 
			
		||||
  type        = string
 | 
			
		||||
  default     = "automatisation-tp"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
variable "region" {
 | 
			
		||||
  description = "Région Google Cloud"
 | 
			
		||||
  description = "Région du projet"
 | 
			
		||||
  type        = string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
variable "frontend_cidr" {
 | 
			
		||||
  description = "Bloc CIDR pour le sous-réseau frontend"
 | 
			
		||||
  type        = string
 | 
			
		||||
@@ -28,3 +27,9 @@ variable "database_cidr" {
 | 
			
		||||
  type        = string
 | 
			
		||||
  default     = "10.0.3.0/24"
 | 
			
		||||
}
 | 
			
		||||
# variables (network)
 | 
			
		||||
variable "ssh_source_ranges" {
 | 
			
		||||
  description = "ssh access"
 | 
			
		||||
  type        = string
 | 
			
		||||
  default     = "0.0.0.0/0"
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user