Skip to content

Commit

Permalink
working vm instance with cloud sql instance
Browse files Browse the repository at this point in the history
  • Loading branch information
vk-NEU7 committed Feb 25, 2024
1 parent 42cda60 commit 8ba9db9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 25 deletions.
53 changes: 28 additions & 25 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ resource "google_compute_network" "private_vpc" {
}

resource "google_compute_global_address" "private_ip_address" {
name = "private-ip-address"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
name = var.vpc_peering_ip
purpose = var.vpc_ip_purpose
address_type = var.vpc_ip_addresstype
prefix_length = var.private_ip_length
network = google_compute_network.private_vpc.id
}

resource "google_service_networking_connection" "networking_connection" {
network = google_compute_network.private_vpc.id
service = "servicenetworking.googleapis.com"
service = var.networking_connection_service
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
}

Expand Down Expand Up @@ -84,37 +84,37 @@ resource "google_compute_firewall" "private_vpc_firewall_blockdbtraffic" {
network = google_compute_network.private_vpc.name

allow {
protocol = "tcp"
ports = ["5432"]
protocol = var.db_firewall_protocol
ports = var.db_firewall_ports
}
source_ranges = ["10.1.0.0/24"]
source_ranges = var.db_firewall_source_cidr
}

resource "google_sql_database" "app_db" {
name = "app_db"
name = var.db_name
instance = google_sql_database_instance.db_instance.name
deletion_policy = "delete"
deletion_policy = var.db_deletion_policy
}

resource "google_sql_database_instance" "db_instance" {
name = "new-instance"
name = var.db_instance_name
region = var.region
database_version = "POSTGRES_10"
database_version = var.db_version
depends_on = [ google_service_networking_connection.networking_connection ]

settings {
tier = "db-f1-micro"
disk_type = "pd-ssd"
disk_size = 100
tier = var.db_instance_tier
disk_type = var.db_instance_disk
disk_size = var.db_disk_size

ip_configuration {
ipv4_enabled = false
private_network = google_compute_network.private_vpc.id
}
availability_type = "REGIONAL"
availability_type = var.db_availability
}

deletion_protection = false
deletion_protection = var.db_deletion_protection
}

resource "google_sql_user" "user_details" {
Expand Down Expand Up @@ -152,13 +152,16 @@ resource "google_compute_instance" "webapp_instance" {

metadata_startup_script = <<-EOT
#!/bin/bash
touch /tmp/.env
sudo echo "DB=${google_sql_database_instance.db_instance.private_ip_address}" >> /tmp/.env
sudo echo "DB_USER=${var.db_user}" >> /tmp/.env
sudo echo "DB_PASSWORD=${var.db_password}" >> /tmp/.env
# sudo mv /tmp/.env /opt/webapp/
# sudo chmod 750 /opt/webapp/.env
# sudo chown csye6225:csye6225 /opt/webapp/.env
touch /tmp/application.properties
sudo echo "spring.datasource.driver-class-name=org.postgresql.Driver" >> /tmp/application.properties
sudo echo "spring.datasource.url=jdbc:postgresql://${google_sql_database_instance.db_instance.private_ip_address}:5432/${var.db_name}" >> /tmp/application.properties
sudo echo "spring.datasource.username=${var.db_user}" >> /tmp/application.properties
sudo echo "spring.datasource.password=${var.db_password}" >> /tmp/application.properties
sudo echo "spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect" >> /tmp/application.properties
sudo echo "spring.jpa.hibernate.ddl-auto=update" >> /tmp/application.properties
sudo mv /tmp/application.properties /opt/webapp/
sudo chmod 750 /opt/webapp/application.properties
sudo chown csye6225:csye6225 /opt/webapp/application.properties
sudo systemctl start webapp.service
EOT
}
68 changes: 68 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,78 @@ variable "db_firewall_name" {
type = string
}

variable "db_firewall_protocol" {
type = string
}

variable "db_firewall_source_cidr" {
type = list(string)
}

variable "db_firewall_ports" {
type = list(string)
}

variable "db_instance_name" {
type = string
}

variable "db_name" {
type = string
}

variable "db_user" {
type = string
}

variable "db_password" {
type = string
}

variable "db_deletion_policy" {
type = string
}

variable "db_version" {
type = string
}

variable "db_instance_tier" {
type = string
}

variable "db_instance_disk" {
type = string
}

variable "db_disk_size" {
type = number
}

variable "db_availability" {
type = string
}

variable "db_deletion_protection" {
type = bool
}

variable "vpc_peering_ip" {
type = string
}

variable "vpc_ip_purpose" {
type = string
}

variable "vpc_ip_addresstype" {
type = string
}

variable "private_ip_length" {
type = number
}

variable "networking_connection_service" {
type = string
}

0 comments on commit 8ba9db9

Please sign in to comment.