Compare commits

..

15 Commits

Author SHA1 Message Date
fd8c317f7e Correction des output 2024-12-06 16:33:23 +01:00
fed533bb2c Update Output 2024-12-06 16:27:41 +01:00
16b512dae9 Correction 2024-12-06 16:16:10 +01:00
f1da774e3b Correction 2024-12-06 16:14:24 +01:00
639651921d Ajout du module IAM 2024-12-06 15:16:29 +01:00
ffce64cc14 Correction de l'utilisation des output 2024-12-06 14:57:41 +01:00
885643d5a7 Ajout de l'output compute 2024-12-06 14:38:29 +01:00
bfb830f19c Merge branch 'master' of grond.iut-fbleau.fr:moguljak/tp-cloud 2024-12-04 17:35:29 +01:00
11d0794b73 Correction variables 2024-12-04 17:32:07 +01:00
50e0601621 Update Compute 2024-12-04 17:21:42 +01:00
a66efccb6b Ajout de l'output 2024-12-04 16:54:50 +01:00
b5dfcfcba4 Correction regle firewall 2024-12-04 16:48:45 +01:00
19a8716045 correction 2024-12-04 16:37:45 +01:00
3c08b665cf Correction 2024-12-04 16:35:52 +01:00
bb87f0343a sauvegarde 2024-12-04 16:30:49 +01:00
13 changed files with 419 additions and 0 deletions

View File

@ -0,0 +1,60 @@
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 6.0"
}
}
}
provider "google" {
project = var.project_name
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
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"
#autres variables spécifiques au module
instance_type = var.instance_type
zone = var.zone
frontend_subnet = module.network.subnet_id["frontend"]
backend_subnet = module.network.subnet_id["backend"]
database_subnet = module.network.subnet_id["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

@ -0,0 +1,31 @@
output "vpc_id" {
value = module.network.vpc_id
}
output "subnet_id" {
value = {
frontend = module.network.subnet_id.frontend,
backend = module.network.subnet_id.backend,
database = module.network.subnet_id.database
}
}
output "ip_instance" {
value = {
frontend = module.compute.ip_instance.frontend,
backend = module.compute.ip_instance.backend,
database = module.compute.ip_instance.database
}
}
output "frontend_public_ip" {
value = module.compute.frontend_public_ip
}
output "name_instance" {
value = {
frontend = module.compute.name_instance.frontend,
backend = module.compute.name_instance.backend,
database = module.compute.name_instance.database
}
}

View File

@ -0,0 +1,53 @@
variable "project_name" {
description = "nom du projet"
type = string
default = "projet-vertu"
}
variable "region" {
description = "region"
type = string
default = "europe-west4"
}
variable "frontend_cidr" {
description = "sous réseau frontend"
type = string
default = "10.0.1.0/24"
}
variable "backend_cidr" {
description = "sous réseau backend"
type = string
default = "10.0.2.0/24"
}
variable "database_cidr" {
description = "sous réseau database"
type = string
default = "10.0.3.0/24"
}
variable "ssh_source_ranges" {
description = "ssh"
type = string
default = "0.0.0.0/0"
}
variable "instance_type" {
description = "Type de machine"
type = string
default = "e2-small"
}
variable "zone" {
description = "zone"
type = string
default = "europe-west4-b"
}
variable "project_id" {
description = "ID du projet GCP"
type = string
default = "projet-vertu"
}

View File

@ -0,0 +1,71 @@
resource "google_compute_instance" "vm_front" {
name = "frontend-vm"
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
access_config {} # IP publique
}
tags = ["frontend", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}
resource "google_compute_instance" "vm_back" {
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
access_config {} # IP publique
}
tags = ["backend", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}
resource "google_compute_instance" "vm_db" {
name = "database-vm"
machine_type = var.instance_type
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
size = 10
}
}
network_interface {
subnetwork = var.database_subnet
access_config {} # IP publique
}
tags = ["database", "ssh"]
metadata = {
enable-oslogin = "TRUE"
}
}

View File

@ -0,0 +1,19 @@
output "ip_instance" {
value = {
frontend = google_compute_instance.vm_front.network_interface.0.network_ip,
backend = google_compute_instance.vm_back.network_interface.0.network_ip,
database = google_compute_instance.vm_db.network_interface.0.network_ip
}
}
output "frontend_public_ip" {
value = google_compute_instance.vm_front.network_interface.0.access_config.0.nat_ip
}
output "name_instance" {
value = {
frontend = google_compute_instance.vm_front.name,
backend = google_compute_instance.vm_back.name,
database = google_compute_instance.vm_db.name
}
}

View File

@ -0,0 +1,24 @@
variable "instance_type" {
description = "Type de machine"
type = string
}
variable "zone" {
description = "zone"
type = string
}
variable "frontend_subnet" {
description = "sous réseau frontend"
type = string
}
variable "backend_subnet" {
description = "sous réseau backend"
type = string
}
variable "database_subnet" {
description = "sous réseau 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,4 @@
variable "project_id" {
description = "ID du projet GCP"
type = string
}

View File

@ -0,0 +1,82 @@
# VPC
resource "google_compute_network" "vpc" {
name = "tp7"
auto_create_subnetworks = false
}
# Sous-réseau frontend
resource "google_compute_subnetwork" "frontend" {
name = "frontend"
network = google_compute_network.vpc.id
ip_cidr_range = var.frontend_cidr
region = "europe-west4"
}
# Sous-réseau backend
resource "google_compute_subnetwork" "backend" {
name = "backend"
network = google_compute_network.vpc.id
ip_cidr_range = var.backend_cidr
region = "europe-west4"
}
# Sous-réseau database
resource "google_compute_subnetwork" "database" {
name = "database"
network = google_compute_network.vpc.id
ip_cidr_range = var.database_cidr
region = "europe-west4"
}
# Règle de pare-feu
resource "google_compute_firewall" "allow_ssh" {
name = "allow-ssh-22"
network = google_compute_network.vpc.id
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["ssh"]
}
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"]
}
resource "google_compute_firewall" "allow_frontend_backend" {
name = "allow-front-to-back"
network = google_compute_network.vpc.id
allow {
protocol = "tcp"
ports = ["8000"]
}
source_tags = ["frontend"]
target_tags = ["backend"]
}
resource "google_compute_firewall" "allow_backend_db" {
name = "allow-back-to-db"
network = google_compute_network.vpc.id
allow {
protocol = "tcp"
ports = ["3306"]
}
source_tags = ["backend"]
target_tags = ["database"]
}

View File

@ -0,0 +1,12 @@
# Output
output "vpc_id" {
value = google_compute_network.vpc.id
}
output "subnet_id" {
value = {
frontend = google_compute_subnetwork.frontend.id,
backend = google_compute_subnetwork.backend.id,
database = google_compute_subnetwork.database.id
}
}

View File

@ -0,0 +1,29 @@
variable "project_name" {
description = "nom du projet"
type = string
}
variable "region" {
description = "region"
type = string
}
variable "frontend_cidr" {
description = "sous réseau frontend"
type = string
}
variable "backend_cidr" {
description = "sous réseau backend"
type = string
}
variable "database_cidr" {
description = "sous réseau database"
type = string
}
variable "ssh_source_ranges" {
description = "ssh"
type = string
}