Compare commits

...

23 Commits

Author SHA1 Message Date
12fc45995b fix 2024-12-06 16:31:21 +01:00
9262123ee7 fix 2024-12-06 16:19:17 +01:00
43491ed096 fix 2024-12-06 16:12:47 +01:00
2c7daea69f fix 2024-12-06 16:07:05 +01:00
eed67f62cf fix 2024-12-06 16:01:06 +01:00
1336027eca fix 2024-12-06 15:56:11 +01:00
75e26b2c72 fix 2024-12-06 15:47:07 +01:00
2fd69bf19f fix 2024-12-06 15:42:43 +01:00
321d1abae1 fix 2024-12-06 15:35:23 +01:00
ed9d2bed64 fafa 2024-12-06 15:32:14 +01:00
907157cce6 fix 2024-12-06 15:29:14 +01:00
5a738414d8 Merge branch 'main' of https://grond.iut-fbleau.fr/dariusy/DEV5.6TP7 2024-12-06 15:20:19 +01:00
abdd5136ec compute 2024-12-06 15:17:20 +01:00
776ff0b901 Actualiser tp-cloud/terraform/modules/network/main.tf 2024-12-06 15:02:06 +01:00
ba3ed7673f Actualiser tp-cloud/terraform/modules/network/outputs.tf 2024-12-06 15:00:47 +01:00
36c89d3edc Actualiser tp-cloud/terraform/modules/network/variables.tf 2024-12-06 15:00:16 +01:00
d3be5f0031 fix 2024-12-04 16:53:56 +01:00
8961f97548 fix 2024-12-04 16:53:13 +01:00
a568326cd8 fix 2024-12-04 16:42:58 +01:00
bcfd734fbf fix 2024-12-04 16:40:14 +01:00
ff4eeddf57 fix 2024-12-04 16:35:38 +01:00
5bc65ab504 fix 2024-12-04 16:30:22 +01:00
872ad86a53 fix 2024-12-04 16:28:39 +01:00
12 changed files with 278 additions and 46 deletions

View File

@@ -12,16 +12,49 @@ provider "google" {
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 = var.cidr_range
backend_cidr = var.backend_cidr
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 = "e2-micro"
zone ="europe-west9-c"
frontend_subnet_id = module.network.id_subnetwork["frontend"]
backend_subnet_id = module.network.id_subnetwork["backend"]
database_subnet_id = module.network.id_subnetwork["database"]
}
module "iam" {
source = "../../modules/iam"
project_id = var.project_id
}
data "google_client_openid_userinfo" "me" {
}
resource "local_file" "ansible_config" {
content = templatefile("${path.module}/../../templates/ansible.cfg.tpl",
{
remote_user = data.google_client_openid_userinfo.me.email
}
)
filename = "../../ansible/ansible.cfg"
}
resource "local_file" "service_account" {
content = base64decode(module.iam.service_account_key)
filename = "../../ansible/service_account.json"
}

View File

@@ -1,48 +1,47 @@
variable "project_name" {
description = "ID du projet GCP"
description = "Nom du projet"
type = string
default = "iut fbleau tp"
default = "newtp-443913"
}
variable "project_id" {
description = "ID du projet GCP"
description = "projet id"
type = string
default = "iut fbleau tp"
default = "newtp-443913"
}
variable "region" {
description = "region du projet"
description = "Region"
type = string
default = "europe-west9-c"
default = "europe-west9"
}
variable "frontend_cidr" {
description = "frontend"
description = "cidr du frontend"
type = string
default = "10.0.1.0/24"
}
variable "backend_cidr" {
description = "backend"
description = "cidr du backend"
type = string
default = "10.0.2.0/24"
}
variable "database_cidr" {
description = "database"
description = "cidr du db"
type = string
default = "10.0.3.0/24"
}
variable "ssh_source_ranges" {
description = "acces par ssh"
description = "ssh"
type = string
default = "0.0.0.0/0"
}
variable "cidr_range" {
description = "cidr-network"
description = "cidr de network"
type = string
default = "10.0.0.0/16"
}
}

View File

@@ -0,0 +1,71 @@
resource "google_compute_instance" "frontend" {
name = "ma-vm-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 {}
}
tags = ["frontend", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}
resource "google_compute_instance" "backend" {
name = "ma-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
access_config {}
}
tags = ["backend", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}
resource "google_compute_instance" "database" {
name = "ma-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
access_config {}
}
tags = ["database", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}

View File

@@ -0,0 +1,21 @@
output "instance_names" {
description = "Nom instance"
value = {
frontend = google_compute_instance.frontend.name
backend = google_compute_instance.backend.name
database = google_compute_instance.database.name
}
}
output "frontend_public_ip" {
value = google_compute_instance.frontend.network_interface[0].access_config[0].nat_ip
}
output "private_ip" {
description = "IP internes"
value = {
frontend = google_compute_instance.frontend.network_interface[0].network_ip
backend = google_compute_instance.backend.network_interface[0].network_ip
database = google_compute_instance.database.network_interface[0].network_ip
}
}

View File

@@ -0,0 +1,26 @@
variable "instance_type" {
description = "les types d'instances"
type = string
default = "e2-micro"
}
variable "zone" {
description = "zone des instances"
type = string
default = "europe-west9"
}
variable "frontend_subnet_id" {
description = "L'identifiant du subnet utilisé pour le frontend"
type = string
}
variable "backend_subnet_id" {
description = "L'identifiant du subnet utilisé pour le backend"
type = string
}
variable "database_subnet_id" {
description = "L'identifiant du subnet utilisé pour la database"
type = string
}

View File

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

View File

@@ -0,0 +1,10 @@
output "service_account_email" {
description = "Email du compte de service"
value = google_service_account.service_account.email
}
output "service_account_key" {
description = "Clé du compte de service"
value = google_service_account_key.service_account.private_key
sensitive = true
}

View File

@@ -0,0 +1,13 @@
# modules/iam/variables.tf
variable "project_id" {
description = "ID du projet GCP"
type = string
}

View File

@@ -1,40 +1,57 @@
# VPC
resource "google_compute_network" "vpc" {
name = "nom"
name = "myvpc"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "frontend" {
name = "frontend"
# Sous-réseau
resource "google_compute_subnetwork" "frontend_subnet" {
name = "frontend-subnet"
network = google_compute_network.vpc.id
ip_cidr_range = var.frontend_cidr
region = var.region
}
resource "google_compute_subnetwork" "backend" {
name = "backend"
# Sous-réseau
resource "google_compute_subnetwork" "backend_subnet" {
name = "backend-subnet"
network = google_compute_network.vpc.id
ip_cidr_range = var.backend_cidr
ip_cidr_range = var.backend_cidr
region = var.region
}
resource "google_compute_subnetwork" "db" {
name = "db"
# Sous-réseau
resource "google_compute_subnetwork" "database_subnet" {
name = "database-subnet"
network = google_compute_network.vpc.id
ip_cidr_range = var.database_cidr
region = var.region
}
resource "google_compute_firewall" "allow_http-https" {
name = "allow-http-https"
resource "google_compute_firewall" "allow_http" {
name = "allow-http"
network = google_compute_network.vpc.id
allow {
protocol = "tcp"
ports = ["80", "443"]
ports = ["80"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["web"]
target_tags = ["frontend"]
}
resource "google_compute_firewall" "allow_https" {
name = "allow-https"
network = google_compute_network.vpc.id
allow {
protocol = "tcp"
ports = ["443"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["frontend"]
}
resource "google_compute_firewall" "allow_ssh" {
@@ -46,13 +63,12 @@ resource "google_compute_firewall" "allow_ssh" {
ports = ["22"]
}
source_ranges = var.ssh_source_ranges
target_tags = ["web"]
source_ranges = [var.ssh_source_ranges]
target_tags = ["ssh"]
}
resource "google_compute_firewall" "front-to-back" {
name = "front-to-back"
resource "google_compute_firewall" "allow_frontend_to_backend" {
name = "allow-frontend-to-backend"
network = google_compute_network.vpc.id
allow {
@@ -60,12 +76,12 @@ resource "google_compute_firewall" "front-to-back" {
ports = ["8000"]
}
source_ranges = var.frontend_cidr
source_ranges = [var.frontend_cidr]
target_tags = ["backend"]
}
resource "google_compute_firewall" "back-to-db" {
name = "back-to-db"
resource "google_compute_firewall" "allow-sql" {
name = "allow-sql"
network = google_compute_network.vpc.id
allow {
@@ -73,6 +89,6 @@ resource "google_compute_firewall" "back-to-db" {
ports = ["3306"]
}
source_ranges = var.backend_cidr
source_ranges = [var.backend_cidr]
target_tags = ["database"]
}

View File

@@ -0,0 +1,12 @@
output "id_vpc" {
value = google_compute_network.vpc.id
}
output "id_subnetwork" {
value = {
frontend = google_compute_subnetwork.frontend_subnet.id,
backend = google_compute_subnetwork.backend_subnet.id,
database = google_compute_subnetwork.database_subnet.id
}
}

View File

@@ -1,35 +1,34 @@
variable "project_name" {
description = "ID du projet GCP"
description = "Nom du projet"
type = string
}
variable "region" {
description = "region du projet"
description = "gion du projet"
type = string
}
variable "frontend_cidr" {
description = "frontend"
description = "cidr du frontend"
type = string
}
variable "backend_cidr" {
description = "backend"
description = "cidr du backend"
type = string
}
variable "database_cidr" {
description = "database"
description = "cidr du database"
type = string
}
variable "ssh_source_ranges" {
description = "acces par ssh"
description = "Accès à internet"
type = string
}
variable "cidr_range" {
description = "cidr-network"
description = "cidr de network"
type = string
}
}

View File

@@ -0,0 +1,8 @@
[defaults]
host_key_checking = False
inventory = gcp_compute.yml
interpreter_python = auto_silent
remote_user = ${replace(replace(remote_user, ".", "_"), "@", "_")}
[inventory]
enable_plugins = gcp_compute, auto, host_list, yaml, ini, toml, script