Compare commits

..

13 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
12 changed files with 222 additions and 16 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"
}
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,3 +1,4 @@
# VPC
resource "google_compute_network" "vpc" {
name = "myvpc"
auto_create_subnetworks = false

View File

@@ -8,4 +8,5 @@ output "id_subnetwork" {
backend = google_compute_subnetwork.backend_subnet.id,
database = google_compute_subnetwork.database_subnet.id
}
}
}

View File

@@ -23,7 +23,6 @@ variable "database_cidr" {
description = "cidr du database"
type = string
}
variable "ssh_source_ranges" {
description = "Accès à internet"
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