Compare commits

...

11 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
12 changed files with 106 additions and 15 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,19 +1,19 @@
variable "project_name" {
description = "Nom du projet"
type = string
default = "tp-1-docker"
default = "newtp-443913"
}
variable "project_id" {
description = "ID du projet"
description = "projet id"
type = string
default = "tp-1-docker"
default = "newtp-443913"
}
variable "region" {
description = "Région du projet"
description = "Region"
type = string
default = "europe-west4"
default = "europe-west9"
}
variable "frontend_cidr" {
@@ -29,13 +29,13 @@ variable "backend_cidr" {
}
variable "database_cidr" {
description = "cidr du database"
description = "cidr du db"
type = string
default = "10.0.3.0/24"
}
variable "ssh_source_ranges" {
description = "Accès à internet"
description = "ssh"
type = string
default = "0.0.0.0/0"
}

View File

@@ -12,7 +12,7 @@ resource "google_compute_instance" "frontend" {
network_interface {
subnetwork = var.frontend_subnet_id
access_config {} # IP publique
access_config {}
}
tags = ["frontend", "ssh"]
@@ -36,6 +36,7 @@ resource "google_compute_instance" "backend" {
network_interface {
subnetwork = var.backend_subnet_id
access_config {}
}
tags = ["backend", "ssh"]
@@ -59,6 +60,7 @@ resource "google_compute_instance" "database" {
network_interface {
subnetwork = var.database_subnet_id
access_config {}
}
tags = ["database", "ssh"]

View File

@@ -1,5 +1,5 @@
output "instance_names" {
description = "Noms des instances"
description = "Nom instance"
value = {
frontend = google_compute_instance.frontend.name
backend = google_compute_instance.backend.name
@@ -12,7 +12,7 @@ output "frontend_public_ip" {
}
output "private_ip" {
description = "IPs internes de toutes les instances"
description = "IP internes"
value = {
frontend = google_compute_instance.frontend.network_interface[0].network_ip
backend = google_compute_instance.backend.network_interface[0].network_ip

View File

@@ -7,7 +7,7 @@ variable "instance_type" {
variable "zone" {
description = "zone des instances"
type = string
default = "europe-west4"
default = "europe-west9"
}
variable "frontend_subnet_id" {

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