Compare commits

8 Commits
main ... main

Author SHA1 Message Date
James Boutaric
94829c2252 ajout terraform show 2025-12-04 11:47:58 +01:00
James Boutaric
84294c24f5 . 2025-12-04 11:12:28 +01:00
James Boutaric
f54d9bacfb ajout outputs 2025-12-04 11:05:37 +01:00
James Boutaric
cfeee8df7f . 2025-12-04 10:15:36 +01:00
James Boutaric
540fb6c634 . 2025-12-04 10:03:07 +01:00
James Boutaric
a1aee0dd84 . 2025-12-04 09:59:27 +01:00
James Boutaric
cd01194190 ajout computes 2025-12-04 09:32:09 +01:00
James Boutaric
50b5a884f0 . 2025-12-03 17:10:48 +01:00
14 changed files with 1085 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,48 @@
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 6.12.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
}
# module "network" {
# source = "./modules/network"
# # Variables d'entrée
# project_name = var.project_name
# region = var.region
# # Autres variables spécifiques au module
# cidr_range = "10.0.0.0/16"
# }
module "network" {
source = "../../modules/network"
project_name = var.project_name
region = var.region
frontend_cidr = var.frontend_cidr
backend_cidr = var.backend_cidr
database_cidr = var.database_cidr
ssh_source_ranges = var.ssh_source_ranges
}
module "compute" {
source = "../../modules/compute"
instance_type = var.instance_type
zone = var.zone
frontend_subnet_id = module.network.subnets.frontend
backend_subnet_id = module.network.subnets.backend
database_subnet_id = module.network.subnets.database
}
module "iam" {
source = "../../modules/iam"
project_id = var.project_id
}

View File

@@ -0,0 +1,33 @@
# output "instance_ip" {
# value = google_compute_instance.main.network_interface[0].access_config[0].nat_ip
# }
output "ip_internes" {
value = module.compute.ip_internes
}
output "ip_public_frontend" {
value = module.compute.ip_public_frontend
}
output "nom_instances" {
value = module.compute.nom_instances
}
output "service_account_email" {
description = "Email du compte de service Terraform."
value = module.iam.email
}
output "service_account_key" {
description = "Clé privée du compte de service Terraform (sensitive)."
value = module.iam.key
sensitive = true
}
output "vpc" {
value = module.network.vpc
}
output "subnets" {
value = module.network.subnets
}

View File

@@ -0,0 +1,59 @@
# variable "project_id" {
# description = "ID du projet GCP"
# type = string
# default = "mon-projet"
# }
variable "project_name" {
description = "Nom du projet"
type = string
default = "My First Project"
}
variable "region" {
description = "Region du projet"
type = string
default = "europe-west9"
}
variable "frontend_cidr" {
description = "CIDR for frontend subnet"
type = string
default = "10.0.1.0/24"
}
variable "backend_cidr" {
description = "CIDR for backend subnet"
type = string
default = "10.0.2.0/24"
}
variable "database_cidr" {
description = "CIDR for database subnet"
type = string
default = "10.0.3.0/24"
}
variable "ssh_source_ranges" {
description = ""
type = string
default = "0.0.0.0/0"
}
variable "project_id" {
description = "ID du projet"
type = string
default = "plenary-plane-478713-q1"
}
variable "instance_type" {
description = "type de l'instance"
type = string
default = "e2-small"
}
variable "zone" {
description = "Nom de la zone"
type = string
default = "europe-west9-b"
}

View File

@@ -0,0 +1,93 @@
# resource "google_compute_instance" "vm" {
# name = "ma-vm"
# machine_type = "e2-medium"
# zone = "europe-west1-b"
# boot_disk {
# initialize_params {
# image = "debian-cloud/debian-11"
# size = 10
# }
# }
# network_interface {
# access_config {} # IP publique
# subnetwork = google_compute_subnetwork.subnet.id
# }
# tags = ["web", "app"]
# metadata = {
# enable-oslogin = "TRUE"
# }
# }
resource "google_compute_instance" "vm_frontend" {
name = "vm-frontend"
machine_type = var.instance_type
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
size = 10
}
}
network_interface {
access_config {} # IP publique
subnetwork = var.frontend_subnet_id
}
tags = ["frontend", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}
resource "google_compute_instance" "vm_backend" {
name = "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" "vm_database" {
name = "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,23 @@
# output "instance_ip" {
# value = google_compute_instance.main.network_interface[0].access_config[0].nat_ip
# }
output "ip_internes" {
value = {
frontend = google_compute_instance.vm_frontend.network_interface[0].network_ip
backend = google_compute_instance.vm_backend.network_interface[0].network_ip
database = google_compute_instance.vm_database.network_interface[0].network_ip
}
}
output "ip_public_frontend" {
value = google_compute_instance.vm_frontend.network_interface[0].access_config[0].nat_ip
}
output "nom_instances" {
value = {
frontend = google_compute_instance.vm_frontend.name
backend = google_compute_instance.vm_backend.name
database = google_compute_instance.vm_database.name
}
}

View File

@@ -0,0 +1,30 @@
# variable "project_id" {
# description = "ID du projet GCP"
# type = string
# default = "mon-projet"
# }
variable "instance_type" {
description = "type de l'instance"
type = string
}
variable "zone" {
description = "Nom de la zone"
type = string
}
variable "frontend_subnet_id" {
description = "id du frontend"
type = string
}
variable "backend_subnet_id" {
description = "id du backend"
type = string
}
variable "database_subnet_id" {
description = "id du database"
type = string
}

View File

@@ -0,0 +1,27 @@
resource "google_service_account" "service_account" {
account_id = "terraform"
display_name = "terraform"
}
resource "google_service_account_key" "mykey" {
service_account_id = google_service_account.service_account.name
public_key_type = "TYPE_X509_PEM_FILE"
}
resource "google_project_iam_binding" "custom_service_account" {
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" "cache" {
user = data.google_client_openid_userinfo.me.email
key = file("~/.ssh/id_ed25519.pub")
project = var.project_id
}

View File

@@ -0,0 +1,19 @@
# output "instance_ip" {
# value = google_compute_instance.main.network_interface[0].access_config[0].nat_ip
# }
output "email" {
description = "Service account email."
value = google_service_account.service_account.email
}
# output "vpc_id" {
# description = "ID of project VPC"
# value = module.vpc.vpc_id
# }
output "key" {
description = "Service account private key."
sensitive = true
value = google_service_account_key.mykey.private_key
}

View File

@@ -0,0 +1,10 @@
# variable "project_id" {
# description = "ID du projet GCP"
# type = string
# default = "mon-projet"
# }
variable "project_id" {
description = "ID du projet"
type = string
}

View File

@@ -0,0 +1,105 @@
# VPC
# resource "google_compute_network" "vpc" {
# name = "mon-vpc"
# auto_create_subnetworks = false
# }
resource "google_compute_network" "vpc_terraform" {
name = "vpc-terraform-2"
auto_create_subnetworks = false
}
# Sous-réseau
# resource "google_compute_subnetwork" "subnet" {
# name = "mon-subnet"
# network = google_compute_network.vpc.id
# ip_cidr_range = "10.0.1.0/24"
# region = "europe-west1"
# }
resource "google_compute_subnetwork" "subnet_frontend" {
name = "frontend-2"
network = google_compute_network.vpc_terraform.id
ip_cidr_range = var.frontend_cidr
region = var.region
}
resource "google_compute_subnetwork" "subnet_backend" {
name = "backend-2"
network = google_compute_network.vpc_terraform.id
ip_cidr_range = var.backend_cidr
region = var.region
}
resource "google_compute_subnetwork" "subnet_database" {
name = "database-2"
network = google_compute_network.vpc_terraform.id
ip_cidr_range = var.database_cidr
region = var.region
}
# Règles de Pare-feu
# resource "google_compute_firewall" "allow_http" {
# name = "allow-http"
# network = google_compute_network.vpc.id
# allow {
# protocol = "tcp"
# ports = ["80", "443"]
# }
# source_ranges = ["0.0.0.0/0"]
# target_tags = ["web"]
# }
resource "google_compute_firewall" "allow_user_frontend" {
name = "allow-user-frontend-2"
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_frontend_backend" {
name = "allow-frontend-backend-2"
network = google_compute_network.vpc_terraform.id
allow {
protocol = "tcp"
ports = ["8000"]
}
source_tags = ["frontend"]
target_tags = ["backend"]
}
resource "google_compute_firewall" "allow_ssh_all" {
name = "allow-ssh-all-2"
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-2"
network = google_compute_network.vpc_terraform.id
allow {
protocol = "tcp"
ports = ["3306"]
}
source_tags = ["backend"]
target_tags = ["database"]
}

View File

@@ -0,0 +1,20 @@
# output "instance_ip" {
# value = google_compute_instance.main.network_interface[0].access_config[0].nat_ip
# }
output "vpc" {
value = google_compute_network.vpc_terraform.id
}
# output "vpc_id" {
# description = "ID of project VPC"
# value = module.vpc.vpc_id
# }
output "subnets" {
value = {
frontend = google_compute_subnetwork.subnet_frontend.id
backend = google_compute_subnetwork.subnet_backend.id
database = google_compute_subnetwork.subnet_database.id
}
}

View File

@@ -0,0 +1,43 @@
# À 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_id" {
# description = "ID du projet GCP"
# type = string
# default = "mon-projet"
# }
variable "project_name" {
description = "Nom du projet"
type = string
}
variable "region" {
description = "Region du projet"
type = string
}
variable "frontend_cidr" {
description = "CIDR for frontend subnet"
type = string
}
variable "backend_cidr" {
description = "CIDR for backend subnet"
type = string
}
variable "database_cidr" {
description = "CIDR for database subnet"
type = string
}
variable "ssh_source_ranges" {
description = ""
type = string
}

575
terraform_show.txt Normal file

File diff suppressed because one or more lines are too long