forked from pierront/but3-iac
on espere ca va marcher
This commit is contained in:
42
terraform/environements/dev/main.tf
Normal file
42
terraform/environements/dev/main.tf
Normal file
@@ -0,0 +1,42 @@
|
||||
# Provider Google
|
||||
provider "google" {
|
||||
project = var.project_id
|
||||
region = var.region
|
||||
zone = var.zone
|
||||
}
|
||||
|
||||
# Module IAM
|
||||
module "iam" {
|
||||
source = "../../modules/iam"
|
||||
project_id = var.project_id
|
||||
}
|
||||
|
||||
# Module Compute
|
||||
module "compute" {
|
||||
source = "../../modules/compute"
|
||||
instance_type = var.instance_type
|
||||
zone = var.zone
|
||||
frontend_subnet_id = var.frontend_subnet_id
|
||||
backend_subnet_id = var.backend_subnet_id
|
||||
database_subnet_id = var.database_subnet_id
|
||||
}
|
||||
|
||||
# Exemple de création d'un template pour Ansible
|
||||
data "template_file" "ansible_inventory" {
|
||||
template = <<EOT
|
||||
[frontend]
|
||||
${module.compute.frontend_internal_ip} ansible_user=YOUR_SSH_USER
|
||||
|
||||
[backend]
|
||||
${module.compute.backend_internal_ip} ansible_user=YOUR_SSH_USER
|
||||
|
||||
[database]
|
||||
${module.compute.database_internal_ip} ansible_user=YOUR_SSH_USER
|
||||
EOT
|
||||
}
|
||||
|
||||
# Optionnel : écrire le fichier sur le disque
|
||||
resource "local_file" "ansible_inventory" {
|
||||
content = data.template_file.ansible_inventory.rendered
|
||||
filename = "${path.module}/inventory.ini"
|
||||
}
|
||||
35
terraform/environements/dev/outputs.tf
Normal file
35
terraform/environements/dev/outputs.tf
Normal file
@@ -0,0 +1,35 @@
|
||||
# Outputs du module Compute
|
||||
output "frontend_internal_ip" {
|
||||
value = module.compute.frontend_internal_ip
|
||||
}
|
||||
|
||||
output "backend_internal_ip" {
|
||||
value = module.compute.backend_internal_ip
|
||||
}
|
||||
|
||||
output "database_internal_ip" {
|
||||
value = module.compute.database_internal_ip
|
||||
}
|
||||
|
||||
output "frontend_public_ip" {
|
||||
value = module.compute.frontend_public_ip
|
||||
}
|
||||
|
||||
output "instance_names" {
|
||||
value = module.compute.instance_names
|
||||
}
|
||||
|
||||
# Outputs du module IAM
|
||||
output "service_account_email" {
|
||||
value = module.iam.service_account_email
|
||||
}
|
||||
|
||||
output "service_account_key" {
|
||||
value = module.iam.service_account_key
|
||||
sensitive = true
|
||||
}
|
||||
|
||||
# Output du fichier Ansible généré
|
||||
output "ansible_inventory_file" {
|
||||
value = local_file.ansible_inventory.filename
|
||||
}
|
||||
41
terraform/environements/dev/variables.tf
Normal file
41
terraform/environements/dev/variables.tf
Normal file
@@ -0,0 +1,41 @@
|
||||
# Variables pour le projet GCP
|
||||
variable "project_id" {
|
||||
description = "ID du projet GCP"
|
||||
type = string
|
||||
default = "mon-projet-gcp-dev"
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
description = "Région GCP"
|
||||
type = string
|
||||
default = "europe-west1"
|
||||
}
|
||||
|
||||
variable "zone" {
|
||||
description = "Zone GCP"
|
||||
type = string
|
||||
default = "europe-west1-b"
|
||||
}
|
||||
|
||||
# Variables pour les subnets
|
||||
variable "frontend_subnet_id" {
|
||||
description = "ID du subnet frontend"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "backend_subnet_id" {
|
||||
description = "ID du subnet backend"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "database_subnet_id" {
|
||||
description = "ID du subnet database"
|
||||
type = string
|
||||
}
|
||||
|
||||
# Type d'instance par défaut
|
||||
variable "instance_type" {
|
||||
description = "Type d'instance pour Compute"
|
||||
type = string
|
||||
default = "e2-medium"
|
||||
}
|
||||
71
terraform/modules/compute/main.tf
Normal file
71
terraform/modules/compute/main.tf
Normal file
@@ -0,0 +1,71 @@
|
||||
resource "google_compute_instance" "frontend" {
|
||||
name = "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
|
||||
}
|
||||
|
||||
metadata = {
|
||||
enable-oslogin = "TRUE"
|
||||
}
|
||||
|
||||
tags = ["frontend", "ssh"]
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "backend" {
|
||||
name = "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
|
||||
# Pas d'IP publique
|
||||
}
|
||||
|
||||
metadata = {
|
||||
enable-oslogin = "TRUE"
|
||||
}
|
||||
|
||||
tags = ["backend", "ssh"]
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "database" {
|
||||
name = "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
|
||||
# Pas d'IP publique
|
||||
}
|
||||
|
||||
metadata = {
|
||||
enable-oslogin = "TRUE"
|
||||
}
|
||||
|
||||
tags = ["database", "ssh"]
|
||||
}
|
||||
23
terraform/modules/compute/outputs.tf
Normal file
23
terraform/modules/compute/outputs.tf
Normal file
@@ -0,0 +1,23 @@
|
||||
output "frontend_internal_ip" {
|
||||
value = google_compute_instance.frontend.network_interface[0].network_ip
|
||||
}
|
||||
|
||||
output "backend_internal_ip" {
|
||||
value = google_compute_instance.backend.network_interface[0].network_ip
|
||||
}
|
||||
|
||||
output "database_internal_ip" {
|
||||
value = google_compute_instance.database.network_interface[0].network_ip
|
||||
}
|
||||
|
||||
output "frontend_public_ip" {
|
||||
value = google_compute_instance.frontend.network_interface[0].access_config[0].nat_ip
|
||||
}
|
||||
|
||||
output "instance_names" {
|
||||
value = [
|
||||
google_compute_instance.frontend.name,
|
||||
google_compute_instance.backend.name,
|
||||
google_compute_instance.database.name
|
||||
]
|
||||
}
|
||||
26
terraform/modules/compute/variables.tf
Normal file
26
terraform/modules/compute/variables.tf
Normal file
@@ -0,0 +1,26 @@
|
||||
variable "instance_type" {
|
||||
description = "Type de machine pour les instances"
|
||||
type = string
|
||||
default = "e2-medium"
|
||||
}
|
||||
|
||||
variable "zone" {
|
||||
description = "Zone où seront déployées les instances"
|
||||
type = string
|
||||
default = "europe-west1-b"
|
||||
}
|
||||
|
||||
variable "frontend_subnet_id" {
|
||||
description = "ID du subnet pour le frontend"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "backend_subnet_id" {
|
||||
description = "ID du subnet pour le backend"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "database_subnet_id" {
|
||||
description = "ID du subnet pour la base de données"
|
||||
type = string
|
||||
}
|
||||
42
terraform/modules/iam/main.tf
Normal file
42
terraform/modules/iam/main.tf
Normal file
@@ -0,0 +1,42 @@
|
||||
# Création du compte de service Terraform
|
||||
resource "google_service_account" "terraform" {
|
||||
account_id = "terraform-sa"
|
||||
display_name = "Terraform Service Account"
|
||||
project = var.project_id
|
||||
}
|
||||
|
||||
# Création de la clé pour le compte de service
|
||||
resource "google_service_account_key" "terraform_key" {
|
||||
service_account_id = google_service_account.terraform.name
|
||||
keepers = {
|
||||
# Permet de régénérer la clé si besoin
|
||||
project = var.project_id
|
||||
}
|
||||
}
|
||||
|
||||
# Attribution de rôles IAM
|
||||
# Exemples de rôles pour pouvoir gérer Compute, IAM et OS Login
|
||||
resource "google_project_iam_member" "compute_admin" {
|
||||
project = var.project_id
|
||||
role = "roles/compute.admin"
|
||||
member = "serviceAccount:${google_service_account.terraform.email}"
|
||||
}
|
||||
|
||||
resource "google_project_iam_member" "iam_admin" {
|
||||
project = var.project_id
|
||||
role = "roles/iam.serviceAccountAdmin"
|
||||
member = "serviceAccount:${google_service_account.terraform.email}"
|
||||
}
|
||||
|
||||
resource "google_project_iam_member" "oslogin" {
|
||||
project = var.project_id
|
||||
role = "roles/compute.osLogin"
|
||||
member = "serviceAccount:${google_service_account.terraform.email}"
|
||||
}
|
||||
|
||||
# Configuration d'OS Login avec clé SSH
|
||||
resource "google_compute_project_metadata_item" "enable_oslogin" {
|
||||
project = var.project_id
|
||||
key = "enable-oslogin"
|
||||
value = "TRUE"
|
||||
}
|
||||
8
terraform/modules/iam/outputs.tf
Normal file
8
terraform/modules/iam/outputs.tf
Normal file
@@ -0,0 +1,8 @@
|
||||
output "service_account_email" {
|
||||
value = google_service_account.terraform.email
|
||||
}
|
||||
|
||||
output "service_account_key" {
|
||||
value = google_service_account_key.terraform_key.private_key
|
||||
sensitive = true
|
||||
}
|
||||
4
terraform/modules/iam/variables.tf
Normal file
4
terraform/modules/iam/variables.tf
Normal file
@@ -0,0 +1,4 @@
|
||||
variable "project_id" {
|
||||
description = "ID du projet GCP"
|
||||
type = string
|
||||
}
|
||||
81
terraform/modules/network/main.tf
Normal file
81
terraform/modules/network/main.tf
Normal file
@@ -0,0 +1,81 @@
|
||||
provider "google" {
|
||||
project = var.project_name
|
||||
region = var.region
|
||||
}
|
||||
|
||||
resource "google_compute_network" "custom_vpc" {
|
||||
name = "${var.project_name}-vpc"
|
||||
auto_create_subnetworks = false
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "frontend" {
|
||||
name = "${var.project_name}-frontend-subnet"
|
||||
ip_cidr_range = var.frontend_cidr
|
||||
network = google_compute_network.custom_vpc.id
|
||||
region = var.region
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "backend" {
|
||||
name = "${var.project_name}-backend-subnet"
|
||||
ip_cidr_range = var.backend_cidr
|
||||
network = google_compute_network.custom_vpc.id
|
||||
region = var.region
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "database" {
|
||||
name = "${var.project_name}-database-subnet"
|
||||
ip_cidr_range = var.database_cidr
|
||||
network = google_compute_network.custom_vpc.id
|
||||
region = var.region
|
||||
}
|
||||
|
||||
resource "google_compute_firewall" "frontend_http_https" {
|
||||
name = "${var.project_name}-frontend-http-https"
|
||||
network = google_compute_network.custom_vpc.id
|
||||
|
||||
allow {
|
||||
protocol = "tcp"
|
||||
ports = ["80", "443"]
|
||||
}
|
||||
|
||||
source_ranges = ["0.0.0.0/0"]
|
||||
target_tags = ["frontend"]
|
||||
}
|
||||
|
||||
resource "google_compute_firewall" "ssh" {
|
||||
name = "${var.project_name}-ssh"
|
||||
network = google_compute_network.custom_vpc.id
|
||||
|
||||
allow {
|
||||
protocol = "tcp"
|
||||
ports = ["22"]
|
||||
}
|
||||
|
||||
source_ranges = [var.ssh_source_ranges]
|
||||
}
|
||||
|
||||
resource "google_compute_firewall" "frontend_to_backend" {
|
||||
name = "${var.project_name}-frontend-to-backend"
|
||||
network = google_compute_network.custom_vpc.id
|
||||
|
||||
allow {
|
||||
protocol = "tcp"
|
||||
ports = ["8000"]
|
||||
}
|
||||
|
||||
source_tags = ["frontend"]
|
||||
target_tags = ["backend"]
|
||||
}
|
||||
|
||||
resource "google_compute_firewall" "backend_to_database" {
|
||||
name = "${var.project_name}-backend-to-database"
|
||||
network = google_compute_network.custom_vpc.id
|
||||
|
||||
allow {
|
||||
protocol = "tcp"
|
||||
ports = ["3306"]
|
||||
}
|
||||
|
||||
source_tags = ["backend"]
|
||||
target_tags = ["database"]
|
||||
}
|
||||
13
terraform/modules/network/outputs.tf
Normal file
13
terraform/modules/network/outputs.tf
Normal file
@@ -0,0 +1,13 @@
|
||||
output "vpc_id" {
|
||||
description = "ID du VPC créé"
|
||||
value = google_compute_network.custom_vpc.id
|
||||
}
|
||||
|
||||
output "subnet_ids" {
|
||||
description = "IDs des sous-réseaux"
|
||||
value = {
|
||||
frontend = google_compute_subnetwork.frontend.id
|
||||
backend = google_compute_subnetwork.backend.id
|
||||
database = google_compute_subnetwork.database.id
|
||||
}
|
||||
}
|
||||
33
terraform/modules/network/variables.tf
Normal file
33
terraform/modules/network/variables.tf
Normal file
@@ -0,0 +1,33 @@
|
||||
variable "project_name" {
|
||||
description = "Nom du projet GCP"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
description = "Région pour le VPC et les sous-réseaux"
|
||||
type = string
|
||||
}
|
||||
|
||||
|
||||
variable "ssh_source_ranges" {
|
||||
description = "Plages IP autorisées pour SSH"
|
||||
type = string
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
Reference in New Issue
Block a user