Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94829c2252 | ||
|
|
84294c24f5 | ||
|
|
f54d9bacfb | ||
|
|
cfeee8df7f | ||
|
|
540fb6c634 | ||
|
|
a1aee0dd84 | ||
|
|
cd01194190 | ||
|
|
50b5a884f0 |
48
terraform/environments/dev/main.tf
Normal file
48
terraform/environments/dev/main.tf
Normal 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
|
||||
}
|
||||
33
terraform/environments/dev/outputs.tf
Normal file
33
terraform/environments/dev/outputs.tf
Normal 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
|
||||
}
|
||||
59
terraform/environments/dev/variables.tf
Normal file
59
terraform/environments/dev/variables.tf
Normal 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"
|
||||
}
|
||||
93
terraform/modules/compute/main.tf
Normal file
93
terraform/modules/compute/main.tf
Normal 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"
|
||||
}
|
||||
}
|
||||
23
terraform/modules/compute/outputs.tf
Normal file
23
terraform/modules/compute/outputs.tf
Normal 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
|
||||
}
|
||||
}
|
||||
30
terraform/modules/compute/variables.tf
Normal file
30
terraform/modules/compute/variables.tf
Normal 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
|
||||
}
|
||||
27
terraform/modules/iam/main.tf
Normal file
27
terraform/modules/iam/main.tf
Normal 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
|
||||
}
|
||||
19
terraform/modules/iam/outputs.tf
Normal file
19
terraform/modules/iam/outputs.tf
Normal 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
|
||||
}
|
||||
10
terraform/modules/iam/variables.tf
Normal file
10
terraform/modules/iam/variables.tf
Normal 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
|
||||
}
|
||||
105
terraform/modules/network/main.tf
Normal file
105
terraform/modules/network/main.tf
Normal 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"]
|
||||
}
|
||||
20
terraform/modules/network/outputs.tf
Normal file
20
terraform/modules/network/outputs.tf
Normal 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
|
||||
}
|
||||
}
|
||||
43
terraform/modules/network/variables.tf
Normal file
43
terraform/modules/network/variables.tf
Normal 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
575
terraform_show.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user