Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a2cf7fc07d | |||
| 98d9b5736a | |||
| 1f39fd0938 | |||
| a656228549 | |||
| 4e7b8ec1ca | |||
| 58e9500c59 | |||
| 72eb00af18 | |||
| e45f8e6c93 | |||
| 66e5a60d85 | |||
| ce8052be22 | |||
| be4001f6c0 | |||
| 3227265c7a | |||
| da5310f034 | |||
| eebd1ba3d3 | |||
| 370e3e3aa2 |
42
terraform/environments/dev/main.tf
Normal file
42
terraform/environments/dev/main.tf
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
# À vous de :
|
||||||
|
# 1. Configurer le provider google
|
||||||
|
# 2. Appeler les trois modules avec les bonnes variables
|
||||||
|
# 3. Créer le fichier de configuration Ansible (template)
|
||||||
|
|
||||||
|
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"
|
||||||
|
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
|
||||||
|
}
|
||||||
29
terraform/environments/dev/outputs.tf
Normal file
29
terraform/environments/dev/outputs.tf
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
55
terraform/environments/dev/variables.tf
Normal file
55
terraform/environments/dev/variables.tf
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# définissez toutes les variables nécessaires avec des valeurs par défaut appropriées.
|
||||||
|
|
||||||
|
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 = "fourth-palisade-478713-i3"
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
92
terraform/modules/compute/main.tf
Normal file
92
terraform/modules/compute/main.tf
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
# À vous de créer :
|
||||||
|
|
||||||
|
# 1. Instance frontend :
|
||||||
|
# - Image : debian-11
|
||||||
|
# - Disque : 10GB
|
||||||
|
# - IP publique
|
||||||
|
# - Tags : frontend, ssh
|
||||||
|
# - OS Login enabled
|
||||||
|
|
||||||
|
# 2. Instance backend :
|
||||||
|
# - Image : debian-11
|
||||||
|
# - Disque : 10GB
|
||||||
|
# - Pas d'IP publique (interne seulement)
|
||||||
|
# - Tags : backend, ssh
|
||||||
|
# - OS Login enabled
|
||||||
|
|
||||||
|
# 3. Instance database :
|
||||||
|
# - Image : debian-11
|
||||||
|
# - Disque : 20GB
|
||||||
|
# - Pas d'IP publique
|
||||||
|
# - Tags : database, ssh
|
||||||
|
# - OS Login enabled
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
24
terraform/modules/compute/outputs.tf
Normal file
24
terraform/modules/compute/outputs.tf
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# À vous d'exposer :
|
||||||
|
# 1. Les IPs internes de toutes les instances
|
||||||
|
# 2. L'IP publique du frontend
|
||||||
|
# 3. Les noms des instances
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
31
terraform/modules/compute/variables.tf
Normal file
31
terraform/modules/compute/variables.tf
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# À vous de définir les variables pour :
|
||||||
|
# - instance_type
|
||||||
|
# - zone
|
||||||
|
# - frontend_subnet_id
|
||||||
|
# - backend_subnet_id
|
||||||
|
# - database_subnet_id
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
33
terraform/modules/iam/main.tf
Normal file
33
terraform/modules/iam/main.tf
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# À vous de créer :
|
||||||
|
# 1. Un compte de service pour Terraform
|
||||||
|
# 2. Une clé pour ce compte de service
|
||||||
|
# 3. Les rôles IAM nécessaires
|
||||||
|
# 4. La configuration OS Login avec votre clé SSH
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
14
terraform/modules/iam/outputs.tf
Normal file
14
terraform/modules/iam/outputs.tf
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# À vous d'exposer :
|
||||||
|
# 1. L'email du compte de service
|
||||||
|
# 2. La clé du compte de service (sensitive = true)
|
||||||
|
|
||||||
|
output "email" {
|
||||||
|
description = "Service account email."
|
||||||
|
value = google_service_account.service_account.email
|
||||||
|
}
|
||||||
|
|
||||||
|
output "key" {
|
||||||
|
description = "Service account private key."
|
||||||
|
sensitive = true
|
||||||
|
value = google_service_account_key.mykey.private_key
|
||||||
|
}
|
||||||
7
terraform/modules/iam/variables.tf
Normal file
7
terraform/modules/iam/variables.tf
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# À vous de définir :
|
||||||
|
# - project_id (string)
|
||||||
|
|
||||||
|
variable "project_id" {
|
||||||
|
description = "ID du projet"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
88
terraform/modules/network/main.tf
Normal file
88
terraform/modules/network/main.tf
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
# À 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"
|
||||||
|
auto_create_subnetworks = false
|
||||||
|
}
|
||||||
|
|
||||||
|
# Sous-réseau
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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"]
|
||||||
|
}
|
||||||
15
terraform/modules/network/outputs.tf
Normal file
15
terraform/modules/network/outputs.tf
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# À vous d'exposer :
|
||||||
|
# 1. L'ID du VPC
|
||||||
|
# 2. Les IDs des sous-réseaux sous forme de map
|
||||||
|
|
||||||
|
output "vpc" {
|
||||||
|
value = google_compute_network.vpc_terraform.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
|
||||||
|
}
|
||||||
|
}
|
||||||
37
terraform/modules/network/variables.tf
Normal file
37
terraform/modules/network/variables.tf
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
# À 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_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
|
||||||
|
}
|
||||||
572
terraform_show.txt
Normal file
572
terraform_show.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user