fin du tp terraform
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
resource "google_compute_instance" "vm_frontend" {
|
||||
name = "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 {} # IP publique
|
||||
}
|
||||
|
||||
tags = ["frontend", "ssh"]
|
||||
|
||||
metadata = {
|
||||
enable-oslogin = "TRUE"
|
||||
}
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "vm_backend" {
|
||||
name = "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
|
||||
}
|
||||
|
||||
tags = ["backend", "ssh"]
|
||||
|
||||
metadata = {
|
||||
enable-oslogin = "TRUE"
|
||||
}
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "vm_database" {
|
||||
name = "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
|
||||
}
|
||||
|
||||
tags = ["database", "ssh"]
|
||||
|
||||
metadata = {
|
||||
enable-oslogin = "TRUE"
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,21 @@
|
||||
output "internal_ips" {
|
||||
value = {
|
||||
frontend = google_compute_instance.vm_frontend.network_interface.0.network_ip
|
||||
backend = google_compute_instance.vm_backend.network_interface.0.network_ip
|
||||
database = google_compute_instance.vm_database.network_interface.0.network_ip
|
||||
}
|
||||
description = "Internal ips of instances"
|
||||
}
|
||||
|
||||
output "public_ip" {
|
||||
value = google_compute_instance.vm_frontend.network_interface.0.access_config.0.nat_ip
|
||||
}
|
||||
|
||||
output "instance_names" {
|
||||
value = {
|
||||
frontend = google_compute_instance.vm_frontend.name
|
||||
backend = google_compute_instance.vm_backend.name
|
||||
database = google_compute_instance.vm_database.name
|
||||
}
|
||||
description = "Name of instances"
|
||||
}
|
||||
|
@@ -0,0 +1,30 @@
|
||||
variable "instance_type" {
|
||||
description = "GCP Instance type"
|
||||
type = string
|
||||
default = "e2-small"
|
||||
}
|
||||
|
||||
variable "zone" {
|
||||
description = "GCP Zone"
|
||||
type = string
|
||||
default = "europe-west1-b"
|
||||
}
|
||||
|
||||
variable "frontend_subnet_id" {
|
||||
description = "Frontend subnet ID"
|
||||
type = string
|
||||
default = "frontend"
|
||||
}
|
||||
|
||||
variable "backend_subnet_id" {
|
||||
description = "Backend subnet ID"
|
||||
type = string
|
||||
default = "backend"
|
||||
}
|
||||
|
||||
variable "database_subnet_id" {
|
||||
description = "Database subnet ID"
|
||||
type = string
|
||||
default = "database"
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,25 @@
|
||||
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")
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -0,0 +1,5 @@
|
||||
# modules/iam/variables.tf
|
||||
variable "project_id" {
|
||||
description = "ID du projet GCP"
|
||||
type = string
|
||||
}
|
||||
|
BIN
modules/network/.outputs.tf.swp
Normal file
BIN
modules/network/.outputs.tf.swp
Normal file
Binary file not shown.
@@ -6,22 +6,22 @@ resource "google_compute_network" "vpc" {
|
||||
resource "google_compute_subnetwork" "front" {
|
||||
name = "frontend"
|
||||
network = google_compute_network.vpc.id
|
||||
ip_cidr_range = "10.0.1.0/24"
|
||||
region = "europe-west1-b"
|
||||
ip_cidr_range = var.frontend_cidr
|
||||
region = var.region
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "backend" {
|
||||
name = "backend"
|
||||
network = google_compute_network.vpc.id
|
||||
ip_cidr_range = "10.0.2.0/24"
|
||||
region = "europe-west1-b"
|
||||
ip_cidr_range = var.backend_cidr
|
||||
region = var.region
|
||||
}
|
||||
|
||||
resource "google_compute_subnetwork" "database" {
|
||||
name = "database"
|
||||
network = google_compute_network.vpc.id
|
||||
ip_cidr_range = "10.0.3.0/24"
|
||||
region = "europe-west1"
|
||||
ip_cidr_range = var.database_cidr
|
||||
region = var.region
|
||||
}
|
||||
|
||||
resource "google_compute_firewall" "allow_http" {
|
||||
@@ -33,6 +33,8 @@ resource "google_compute_firewall" "allow_http" {
|
||||
ports = ["80", "443"]
|
||||
}
|
||||
target_tags = ["frontend"]
|
||||
source_ranges = [ var.http_source_ranges ]
|
||||
|
||||
}
|
||||
|
||||
resource "google_compute_firewall" "allow_ssh" {
|
||||
@@ -43,7 +45,7 @@ resource "google_compute_firewall" "allow_ssh" {
|
||||
protocol = "tcp"
|
||||
ports = ["22"]
|
||||
}
|
||||
source_ranges = [var.ssh_source_ranges]
|
||||
source_ranges = [ var.ssh_source_ranges ]
|
||||
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,11 @@
|
||||
output "vpc_id" {
|
||||
value = google_compute_network.vpc.id
|
||||
}
|
||||
|
||||
output "subnets_ids"{
|
||||
value = {
|
||||
frontend = google_compute_subnetwork.front.id,
|
||||
backend = google_compute_subnetwork.backend.id,
|
||||
database = google_compute_subnetwork.database.id
|
||||
}
|
||||
}
|
||||
|
@@ -1,14 +1,21 @@
|
||||
# modules/network/variables.tf
|
||||
|
||||
variable "project_name" {
|
||||
description = "Nom du projet Google Cloud"
|
||||
variable "projet_id" {
|
||||
description = "ID du projet GCP"
|
||||
type = string
|
||||
default = "automatisation-tp1"
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
description = "Région Google Cloud"
|
||||
description = "region des vms"
|
||||
type = string
|
||||
default = "europe-west1-b"
|
||||
}
|
||||
|
||||
variable "project_name" {
|
||||
description = "Nom du projet Google Cloud"
|
||||
type = string
|
||||
default = "automatisation-tp1"
|
||||
}
|
||||
|
||||
variable "frontend_cidr" {
|
||||
@@ -34,3 +41,9 @@ variable "ssh_source_ranges" {
|
||||
type = string
|
||||
default = "0.0.0.0/0"
|
||||
}
|
||||
|
||||
variable "http_source_ranges" {
|
||||
description = "source ranges"
|
||||
type = string
|
||||
default = "0.0.0.0/0"
|
||||
}
|
||||
|
Reference in New Issue
Block a user