Skip to content

Commit

Permalink
Assignment 06: (#4)
Browse files Browse the repository at this point in the history
- Created a new service account with 2 roles permissions: Logging Admin and Monitoring Metric Writer
- Attached the services account to the VM (refer the compute_instance folder)
- Created a module for Cloud DNS to translate my API Domain Address to IP Address to access the VM in GCP
  • Loading branch information
Kashyab19 authored Jul 24, 2024
1 parent 49845bc commit b329695
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 3 deletions.
7 changes: 6 additions & 1 deletion compute_instance/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
5 changes: 5 additions & 0 deletions compute_instance/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

7 changes: 6 additions & 1 deletion compute_instance/variables.tf
Original file line number Diff line number Diff line change
@@ -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" {
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions dns/main.tf
Original file line number Diff line number Diff line change
@@ -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]
}
Empty file added dns/outputs.tf
Empty file.
24 changes: 24 additions & 0 deletions dns/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions iam/main.tf
Original file line number Diff line number Diff line change
@@ -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}"
]
}
Empty file added iam/outputs.tf
Empty file.
9 changes: 9 additions & 0 deletions iam/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions service_account/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions service_account/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "email" {
value = google_service_account.vm_service_account.email
}
14 changes: 14 additions & 0 deletions service_account/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion vpc/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ output "db_subnet_id" {

output "private_vpc_connection_name" {
value = google_service_networking_connection.private_service_connect.id
}
}

output "private_service_connect_ip" {
value = google_compute_global_address.private_service_connect_ip.address
}

0 comments on commit b329695

Please sign in to comment.