mirror of
https://github.com/ghndrx/GSP662.git
synced 2026-02-10 06:54:58 +00:00
commit new all
This commit is contained in:
56
modules/backend/main.tf
Normal file
56
modules/backend/main.tf
Normal file
@@ -0,0 +1,56 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
google = {
|
||||
source = "hashicorp/google"
|
||||
version = "~> 3.5.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "google" {
|
||||
project = var.project_id
|
||||
region = var.region
|
||||
}
|
||||
|
||||
resource "google_compute_instance" "backend" {
|
||||
name = "backend"
|
||||
machine_type = "n1-standard-1"
|
||||
tags = ["backend"]
|
||||
|
||||
boot_disk {
|
||||
initialize_params {
|
||||
image = var.image_name
|
||||
}
|
||||
}
|
||||
|
||||
metadata_startup_script = file("${path.module}/startup-script.sh")
|
||||
|
||||
network_interface {
|
||||
network = google_compute_network.backend_network.self_link
|
||||
access_config {
|
||||
// Allocate a one-to-one NAT IP to allow SSH and HTTP access
|
||||
}
|
||||
}
|
||||
|
||||
service_account {
|
||||
email = var.sa_email
|
||||
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
|
||||
}
|
||||
}
|
||||
|
||||
resource "google_compute_firewall" "backend_firewall" {
|
||||
name = "allow-backend-internal"
|
||||
network = google_compute_network.backend_network.self_link
|
||||
|
||||
allow {
|
||||
protocol = "tcp"
|
||||
ports = ["8081-8082"]
|
||||
}
|
||||
|
||||
source_tags = ["backend"]
|
||||
target_tags = ["backend"]
|
||||
}
|
||||
|
||||
output "backend_ip" {
|
||||
value = google_compute_instance.backend.network_interface[0].access_config[0].nat_ip
|
||||
}
|
||||
54
modules/backend/startup-script.sh
Normal file
54
modules/backend/startup-script.sh
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Install logging monitor. The monitor will automatically pick up logs sent to
|
||||
# syslog.
|
||||
curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash
|
||||
service google-fluentd restart &
|
||||
|
||||
# Install dependencies from apt
|
||||
apt-get update
|
||||
apt-get install -yq ca-certificates git build-essential supervisor psmisc
|
||||
|
||||
# Install nodejs
|
||||
mkdir /opt/nodejs
|
||||
curl https://nodejs.org/dist/v16.14.0/node-v16.14.0-linux-x64.tar.gz | tar xvzf - -C /opt/nodejs --strip-components=1
|
||||
ln -s /opt/nodejs/bin/node /usr/bin/node
|
||||
ln -s /opt/nodejs/bin/npm /usr/bin/npm
|
||||
|
||||
# Get the application source code from the Google Cloud Storage bucket.
|
||||
mkdir /fancy-store
|
||||
gsutil -m cp -r gs://fancy-store-${var.project_id}/monolith-to-microservices/microservices/* /fancy-store/
|
||||
|
||||
# Install app dependencies.
|
||||
cd /fancy-store/
|
||||
npm install
|
||||
|
||||
# Create a nodeapp user. The application will run as this user.
|
||||
useradd -m -d /home/nodeapp nodeapp
|
||||
chown -R nodeapp:nodeapp /fancy-store
|
||||
|
||||
# Configure supervisor to run the node app.
|
||||
cat >/etc/supervisor/conf.d/node-app.conf << EOF
|
||||
[program:orders]
|
||||
directory=/fancy-store/orders
|
||||
command=npm start
|
||||
autostart=true
|
||||
autorestart=true
|
||||
user=nodeapp
|
||||
environment=HOME="/home/nodeapp",USER="nodeapp",NODE_ENV="production"
|
||||
stdout_logfile=syslog
|
||||
stderr_logfile=syslog
|
||||
|
||||
[program:products]
|
||||
directory=/fancy-store/products
|
||||
command=npm start
|
||||
autostart=true
|
||||
autorestart=true
|
||||
user=nodeapp
|
||||
environment=HOME="/home/nodeapp",USER="nodeapp",NODE_ENV="production"
|
||||
stdout_logfile=syslog
|
||||
stderr_logfile=syslog
|
||||
EOF
|
||||
|
||||
supervisorctl reread
|
||||
supervisorctl update
|
||||
19
modules/backend/variables.tf
Normal file
19
modules/backend/variables.tf
Normal file
@@ -0,0 +1,19 @@
|
||||
variable "project_id" {
|
||||
type = string
|
||||
description = "The ID of the Google Cloud project to use for resources."
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
type = string
|
||||
description = "The region to create resources in."
|
||||
}
|
||||
|
||||
variable "sa_email" {
|
||||
type = string
|
||||
description = "The email address of the service account to associate with the instance."
|
||||
}
|
||||
|
||||
variable "image_name" {
|
||||
type = string
|
||||
description = "The name of the image to use for the instance boot disk."
|
||||
}
|
||||
Reference in New Issue
Block a user