diff --git a/terraform/environments/dev/main.tf b/terraform/environments/dev/main.tf index 68a62a6..2545738 100644 --- a/terraform/environments/dev/main.tf +++ b/terraform/environments/dev/main.tf @@ -32,4 +32,26 @@ module "compute"{ tp07-database = var.tp07-database zone = var.zone instance_type = var.instance_type +} + +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" } \ No newline at end of file diff --git a/terraform/environments/dev/outputs.tf b/terraform/environments/dev/outputs.tf index c8437e6..c63c329 100644 --- a/terraform/environments/dev/outputs.tf +++ b/terraform/environments/dev/outputs.tf @@ -24,4 +24,8 @@ output "instances_names" { backend = module.compute.instances_names.backend, database = module.compute.instances_names.database } +} + +output "frontend_ip" { + value = module.compute.frontend_ip } \ No newline at end of file diff --git a/terraform/modules/compute/outputs.tf b/terraform/modules/compute/outputs.tf index b04f40f..c0c1bfc 100644 --- a/terraform/modules/compute/outputs.tf +++ b/terraform/modules/compute/outputs.tf @@ -12,4 +12,8 @@ output "instances_names" { backend = google_compute_instance.tp07-backend.name, database = google_compute_instance.tp07-database.name } +} + +output "frontend_ip" { + value = google_compute_instance.tp07-frontend.network_interface.0.access_config.0.assigned_nat_ip } \ No newline at end of file diff --git a/terraform/modules/iam/main.tf b/terraform/modules/iam/main.tf index e69de29..1eefe7a 100644 --- a/terraform/modules/iam/main.tf +++ b/terraform/modules/iam/main.tf @@ -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") +} \ No newline at end of file diff --git a/terraform/modules/iam/outputs.tf b/terraform/modules/iam/outputs.tf index e69de29..121bb03 100644 --- a/terraform/modules/iam/outputs.tf +++ b/terraform/modules/iam/outputs.tf @@ -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 +} \ No newline at end of file diff --git a/terraform/modules/iam/variables.tf b/terraform/modules/iam/variables.tf index e69de29..2479e97 100644 --- a/terraform/modules/iam/variables.tf +++ b/terraform/modules/iam/variables.tf @@ -0,0 +1,5 @@ +variable "project_id" { + description = "ID du projet GCP" + type = string +} +