diff --git a/compute_instance/main.tf b/compute_instance/main.tf index 5c865b1..c1a1e3f 100644 --- a/compute_instance/main.tf +++ b/compute_instance/main.tf @@ -2,7 +2,7 @@ resource "google_compute_instance" "custom_instance" { name = var.instance_name machine_type = var.machine_type zone = var.zone - + allow_stopping_for_update = true boot_disk { initialize_params { image = var.image @@ -19,5 +19,10 @@ resource "google_compute_instance" "custom_instance" { } } + service_account { + email = var.service_account_email + scopes = ["cloud-platform"] + } + metadata_startup_script = var.startup_script } diff --git a/compute_instance/outputs.tf b/compute_instance/outputs.tf index 4ef0c77..51c782d 100644 --- a/compute_instance/outputs.tf +++ b/compute_instance/outputs.tf @@ -5,3 +5,8 @@ output "instance_name" { output "instance_self_link" { value = google_compute_instance.custom_instance.self_link } + +output "instance_ip" { + value = google_compute_instance.custom_instance.network_interface.0.access_config.0.nat_ip +} + diff --git a/compute_instance/variables.tf b/compute_instance/variables.tf index f99797c..33c31e1 100644 --- a/compute_instance/variables.tf +++ b/compute_instance/variables.tf @@ -1,7 +1,7 @@ variable "instance_name" { description = "Name of the compute instance" type = string - default = "custom-instance" + default = "custom-instance-after-adding-logging" } variable "machine_type" { @@ -47,3 +47,8 @@ variable "startup_script" { description = "Startup script to initialize the instance" type = string } + +variable "service_account_email" { + description = "Email of the service account" + type = string +} \ No newline at end of file diff --git a/dns/main.tf b/dns/main.tf new file mode 100644 index 0000000..ffba3f2 --- /dev/null +++ b/dns/main.tf @@ -0,0 +1,8 @@ +resource "google_dns_record_set" "webapp_dns_records" { + name = var.webapp_domain_name + type = var.webapp_dnsrecord_type + ttl = var.webapp_dns_ttl + managed_zone = var.managed_zone_webapp + + rrdatas = [var.global_ip] +} diff --git a/dns/outputs.tf b/dns/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/dns/variables.tf b/dns/variables.tf new file mode 100644 index 0000000..88d86d0 --- /dev/null +++ b/dns/variables.tf @@ -0,0 +1,24 @@ +variable "webapp_domain_name" { + description = "The domain name for the web application" + type = string +} + +variable "webapp_dnsrecord_type" { + description = "The type of DNS record" + type = string +} + +variable "webapp_dns_ttl" { + description = "The TTL for the DNS record" + type = number +} + +variable "managed_zone_webapp" { + description = "The managed zone for the web application" + type = string +} + +variable "global_ip" { + description = "The global IP address for the web application" + type = string +} diff --git a/iam/main.tf b/iam/main.tf new file mode 100644 index 0000000..059baf1 --- /dev/null +++ b/iam/main.tf @@ -0,0 +1,17 @@ +resource "google_project_iam_binding" "logging_admin" { + project = var.project_id + role = "roles/logging.admin" + + members = [ + "serviceAccount:${var.service_account_email}" + ] +} + +resource "google_project_iam_binding" "monitoring_metric_writer" { + project = var.project_id + role = "roles/monitoring.metricWriter" + + members = [ + "serviceAccount:${var.service_account_email}" + ] +} diff --git a/iam/outputs.tf b/iam/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/iam/variables.tf b/iam/variables.tf new file mode 100644 index 0000000..e9f82f3 --- /dev/null +++ b/iam/variables.tf @@ -0,0 +1,9 @@ +variable "project_id" { + description = "The project ID to bind roles" + type = string +} + +variable "service_account_email" { + description = "The email of the service account" + type = string +} diff --git a/main.tf b/main.tf index 3850db7..b2b72d5 100644 --- a/main.tf +++ b/main.tf @@ -50,10 +50,33 @@ module "compute_instance" { subnet = module.vpc.webapp_subnet_self_link image = var.custom_image zone = var.zone + service_account_email = module.service_account.email startup_script = templatefile("${path.module}/startup_script.tpl", { DB_USER = module.cloudsql.sql_user_name DB_PASS = module.cloudsql.sql_user_password DB_NAME = module.cloudsql.sql_database_name DB_HOST = module.cloudsql.sql_instance_name }) +} + +module "dns" { + source = "./dns" + webapp_domain_name = "kashyabcloudapp.me." + webapp_dnsrecord_type = "A" + webapp_dns_ttl = 300 + managed_zone_webapp = "my-new-zone" # The name of your existing managed zone + global_ip = module.vpc.private_service_connect_ip +} + +module "service_account" { + source = "./service_account" + account_id = "vm-service-account" + display_name = "VM Service Account" + project_id = var.project_id +} + +module "iam" { + source = "./iam" + project_id = var.project_id + service_account_email = module.service_account.email } \ No newline at end of file diff --git a/service_account/main.tf b/service_account/main.tf new file mode 100644 index 0000000..5da6391 --- /dev/null +++ b/service_account/main.tf @@ -0,0 +1,5 @@ +resource "google_service_account" "vm_service_account" { + account_id = var.account_id + display_name = var.display_name + project = var.project_id +} diff --git a/service_account/outputs.tf b/service_account/outputs.tf new file mode 100644 index 0000000..844b829 --- /dev/null +++ b/service_account/outputs.tf @@ -0,0 +1,3 @@ +output "email" { + value = google_service_account.vm_service_account.email +} diff --git a/service_account/variables.tf b/service_account/variables.tf new file mode 100644 index 0000000..9cc0229 --- /dev/null +++ b/service_account/variables.tf @@ -0,0 +1,14 @@ +variable "account_id" { + description = "The account ID of the service account" + type = string +} + +variable "display_name" { + description = "The display name of the service account" + type = string +} + +variable "project_id" { + description = "The project ID where the service account will be created" + type = string +} diff --git a/vpc/outputs.tf b/vpc/outputs.tf index 7f14901..bbaca6f 100644 --- a/vpc/outputs.tf +++ b/vpc/outputs.tf @@ -28,4 +28,8 @@ output "db_subnet_id" { output "private_vpc_connection_name" { value = google_service_networking_connection.private_service_connect.id -} \ No newline at end of file +} + +output "private_service_connect_ip" { + value = google_compute_global_address.private_service_connect_ip.address +}