commit final tp Terraform

This commit is contained in:
2024-12-06 15:15:47 +00:00
parent 6dc7ad109d
commit 7b1ff431c0
19 changed files with 2404 additions and 69 deletions

View File

@@ -0,0 +1,69 @@
resource "google_compute_instance" "frontend" {
name = "ma-vm-frontend"
machine_type = var.instance_type
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
size = 10
}
}
network_interface {
subnetwork = var.frontend_subnet_id
access_config {} # IP publique
}
tags = ["frontend", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}
resource "google_compute_instance" "backend" {
name = "ma-vm-backend"
machine_type = var.instance_type
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
size = 10
}
}
network_interface {
subnetwork = var.backend_subnet_id
}
tags = ["backend", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}
resource "google_compute_instance" "database" {
name = "ma-vm-database"
machine_type = var.instance_type
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
size = 20
}
}
network_interface {
subnetwork = var.database_subnet_id
}
tags = ["database", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}

View File

@@ -0,0 +1,26 @@
variable "instance_type" {
description = "les types d'instances"
type = string
default = "e2-micro"
}
variable "zone" {
description = "zone des instances"
type = string
default = "europe-west4"
}
variable "frontend_subnet_id" {
description = "L'identifiant du subnet utilisé pour le frontend"
type = string
}
variable "backend_subnet_id" {
description = "L'identifiant du subnet utilisé pour le backend"
type = string
}
variable "database_subnet_id" {
description = "L'identifiant du subnet utilisé pour la database"
type = string
}

View File

@@ -0,0 +1,24 @@
resource "google_service_account" "service_account" {
account_id = "terraform"
display_name = "terraform"
}
resource "google_service_account_key" "service_account" {
service_account_id = google_service_account.service_account.name
public_key_type = "TYPE_X509_PEM_FILE"
}
resource "google_project_iam_binding" "service_account_roles" {
project = var.project_id
role = "roles/viewer"
members = ["serviceAccount:${google_service_account.service_account.email}"]
}
data "google_client_openid_userinfo" "me" {
}
resource "google_os_login_ssh_public_key" "add_my_key" {
project = var.project_id
user = data.google_client_openid_userinfo.me.email
key = file("~/.ssh/id_ed25519.pub")
}

View File

@@ -0,0 +1,10 @@
output "service_account_email" {
description = "Email du compte de service"
value = google_service_account.service_account.email
}
output "service_account_key" {
description = "Clé du compte de service"
value = google_service_account_key.service_account.private_key
sensitive = true
}

View File

@@ -0,0 +1,5 @@
# modules/iam/variables.tf
variable "project_id" {
description = "ID du projet GCP"
type = string
}

View File

@@ -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"]
}

View File

@@ -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
}
}

View File

@@ -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"
}