Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 56be687201 | |||
| a2deb7b865 | |||
| 8fa75558ec | |||
| 37e88ecbb9 | |||
| 84aeb66c69 | |||
| 20d1893b1f | |||
| e292f49d99 | |||
| deb90640a0 | |||
| e4c5e6b05e | |||
| 437c85d91d | |||
| d2918f2c6a | |||
| 9897b29f8b | |||
| 9ffcc99ca6 | |||
| 0a7a83433f | |||
| ed14be22c5 | |||
| 80494d0c96 | |||
| 17da1128a0 | |||
| e27fac93e9 | |||
| b90b7426dc | |||
| 876a184bbe | |||
| c4d96e512d | |||
| 3d2e3ab686 | |||
| c9c7853792 | |||
| bca5736344 | |||
| 2821abd4f6 | |||
| af30a84d14 | |||
| b1d2989ef8 | |||
| 0a47519f21 | |||
| f6daa09e66 | |||
| 68299b278a |
@@ -0,0 +1,40 @@
|
|||||||
|
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.backend
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module "iam" {
|
||||||
|
source = "../../modules/iam"
|
||||||
|
project_id = var.project_id
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
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" {
|
||||||
|
value = module.iam.service_account_email
|
||||||
|
}
|
||||||
|
|
||||||
|
output "service_account_key" {
|
||||||
|
sensitive = true
|
||||||
|
value = module.iam.service_account_key
|
||||||
|
}
|
||||||
|
|
||||||
|
output "vpc" {
|
||||||
|
value = module.network.vpc_terraform_output
|
||||||
|
}
|
||||||
|
|
||||||
|
output "subnets" {
|
||||||
|
value = module.network.subnets
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
variable "instance_type" {
|
||||||
|
description = "type de l'instance"
|
||||||
|
type = string
|
||||||
|
default = "e2-small"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "zone" {
|
||||||
|
description = "région"
|
||||||
|
type = string
|
||||||
|
default = "europe-west1-b"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
variable "project_id" {
|
||||||
|
|
||||||
|
description = "ID du projet GCP dans lequel appliquer les règles IAM"
|
||||||
|
type = string
|
||||||
|
default = "gifted-chimera-424007-g1"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
variable "project_name" {
|
||||||
|
description = "Nom du projet"
|
||||||
|
type = string
|
||||||
|
default = "My First Project"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "region" {
|
||||||
|
description = "Région"
|
||||||
|
type = string
|
||||||
|
default = "europe-west1"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "frontend_cidr" {
|
||||||
|
description = "CIDR du frontend"
|
||||||
|
type = string
|
||||||
|
default = "10.0.1.0/24"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "backend_cidr" {
|
||||||
|
description = "CIDR du backend"
|
||||||
|
type = string
|
||||||
|
default = "10.0.2.0/24"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "database_cidr" {
|
||||||
|
description = "CIDR de la database"
|
||||||
|
type = string
|
||||||
|
default = "10.0.3.0/24"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "ssh_source_ranges" {
|
||||||
|
description = "Plages IP autorisées pour SSH"
|
||||||
|
type = string
|
||||||
|
default = "0.0.0.0/0"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
|
||||||
|
# Instance front-end
|
||||||
|
|
||||||
|
resource "google_compute_instance" "frontend_vm" {
|
||||||
|
name = "frontend-vm"
|
||||||
|
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
|
||||||
|
|
||||||
|
# IP publique
|
||||||
|
}
|
||||||
|
|
||||||
|
tags = ["frontend", "ssh"]
|
||||||
|
|
||||||
|
metadata = {
|
||||||
|
enable-oslogin = "TRUE"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Instance back-end
|
||||||
|
|
||||||
|
resource "google_compute_instance" "backend_vm" {
|
||||||
|
name = "backend-vm"
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Instance data-base
|
||||||
|
|
||||||
|
resource "google_compute_instance" "database_vm" {
|
||||||
|
name = "database-vm"
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
output "ip_internes" {
|
||||||
|
value = {
|
||||||
|
frontend = google_compute_instance.frontend_vm.network_interface[0].network_ip
|
||||||
|
backend = google_compute_instance.backend_vm.network_interface[0].network_ip
|
||||||
|
database = google_compute_instance.database_vm.network_interface[0].network_ip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
output "ip_public_frontend" {
|
||||||
|
value = google_compute_instance.frontend_vm.network_interface[0].access_config[0].nat_ip
|
||||||
|
}
|
||||||
|
|
||||||
|
output "nom_instances" {
|
||||||
|
value = {
|
||||||
|
frontend = google_compute_instance.frontend_vm.name
|
||||||
|
backend = google_compute_instance.backend_vm.name
|
||||||
|
database = google_compute_instance.database_vm.name
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
# À 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 = "région"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "frontend_subnet_id" {
|
||||||
|
description = "front-end id"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "backend_subnet_id" {
|
||||||
|
description = "back-end id"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "database_subnet_id" {
|
||||||
|
description = "database id"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
# Compte service terraform
|
||||||
|
|
||||||
|
resource "google_service_account" "terraform_sa" {
|
||||||
|
account_id = "terraform-sa"
|
||||||
|
display_name = "terraform compte service"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_service_account_key" "terraform_sa_key" {
|
||||||
|
service_account_id = google_service_account.terraform_sa.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.terraform_sa.email}",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
data "google_client_openid_userinfo" "me" {
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_os_login_ssh_public_key" "cache" {
|
||||||
|
user = data.google_client_openid_userinfo.me.email
|
||||||
|
project = var.project_id
|
||||||
|
key = file("~/.ssh/id_ed25519.pub")
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
output "service_account_email" {
|
||||||
|
description = "Service account email."
|
||||||
|
value = google_service_account.terraform_sa.email
|
||||||
|
}
|
||||||
|
|
||||||
|
output "service_account_key" {
|
||||||
|
description = "Service account key."
|
||||||
|
sensitive = true
|
||||||
|
value = google_service_account_key.terraform_sa_key.private_key
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
variable "project_id" {
|
||||||
|
description = "ID du projet GCP dans lequel appliquer les règles IAM"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
# VPC
|
||||||
|
resource "google_compute_network" "vpc" {
|
||||||
|
name = "vpc"
|
||||||
|
auto_create_subnetworks = false
|
||||||
|
}
|
||||||
|
|
||||||
|
# Sous-réseau
|
||||||
|
# front-end
|
||||||
|
resource "google_compute_subnetwork" "frontend" {
|
||||||
|
name = "frontend-subnet"
|
||||||
|
network = google_compute_network.vpc.id
|
||||||
|
ip_cidr_range = "10.0.1.0/24"
|
||||||
|
region = "europe-west1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# back-end
|
||||||
|
resource "google_compute_subnetwork" "backend" {
|
||||||
|
name = "backend-subnet"
|
||||||
|
network = google_compute_network.vpc.id
|
||||||
|
ip_cidr_range = "10.0.2.0/24"
|
||||||
|
region = "europe-west1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# data-base
|
||||||
|
resource "google_compute_subnetwork" "database" {
|
||||||
|
name = "database-subnet"
|
||||||
|
network = google_compute_network.vpc.id
|
||||||
|
ip_cidr_range = "10.0.3.0/24"
|
||||||
|
region = "europe-west1"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Règles de Pare-feu
|
||||||
|
|
||||||
|
# - HTTP/HTTPS vers frontend
|
||||||
|
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 = ["frontend"]
|
||||||
|
}
|
||||||
|
|
||||||
|
# - SSH vers toutes les instances
|
||||||
|
resource "google_compute_firewall" "allow_ssh" {
|
||||||
|
name = "allow-ssh"
|
||||||
|
network = google_compute_network.vpc.id
|
||||||
|
|
||||||
|
allow {
|
||||||
|
protocol = "tcp"
|
||||||
|
ports = ["22"]
|
||||||
|
}
|
||||||
|
|
||||||
|
source_ranges = ["0.0.0.0/0"]
|
||||||
|
target_tags = ["ssh"]
|
||||||
|
}
|
||||||
|
|
||||||
|
# - Port 8000 de frontend vers backend
|
||||||
|
resource "google_compute_firewall" "frontend_to_backend" {
|
||||||
|
name = "frontend-to-backend"
|
||||||
|
network = google_compute_network.vpc.id
|
||||||
|
|
||||||
|
allow {
|
||||||
|
protocol = "tcp"
|
||||||
|
ports = ["8000"]
|
||||||
|
}
|
||||||
|
|
||||||
|
source_tags = ["frontend"]
|
||||||
|
target_tags = ["backend"]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# - Port 3306 de backend vers database
|
||||||
|
resource "google_compute_firewall" "backend_to_database" {
|
||||||
|
name = "backend-to-database"
|
||||||
|
network = google_compute_network.vpc.id
|
||||||
|
|
||||||
|
allow {
|
||||||
|
protocol = "tcp"
|
||||||
|
ports = ["3306"]
|
||||||
|
}
|
||||||
|
|
||||||
|
source_tags = ["backend"]
|
||||||
|
target_tags = ["database"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
output "vpc_terraform_output" {
|
||||||
|
value = google_compute_network.vpc.id
|
||||||
|
}
|
||||||
|
|
||||||
|
output "subnets" {
|
||||||
|
value = {
|
||||||
|
frontend = google_compute_subnetwork.frontend.id
|
||||||
|
backend = google_compute_subnetwork.backend.id
|
||||||
|
database = google_compute_subnetwork.database.id
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
variable "project_name" {
|
||||||
|
description = "Nom du projet"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "region" {
|
||||||
|
description = "Région"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "frontend_cidr" {
|
||||||
|
description = "CIDR du frontend"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "backend_cidr" {
|
||||||
|
description = "CIDR du backend"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "database_cidr" {
|
||||||
|
description = "CIDR de la database"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "ssh_source_ranges" {
|
||||||
|
description = "Plages IP autorisées pour SSH"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user