How to deploy NodeJS (Loopback) server + PostgreSQL database to Google App Engine + CloudSQL

Phuoc Ngo.T
May 11, 2018 · 5 min read

About deploying application, there are several cloud platform you can choose. I have tried Amazon, Heroku and Google App Engine (GAE). And so far, GAE make me feel easiest to work with. Of course that's is just my opinion, at least, I’m a front-end developer and just move to back-end recently :D !!!

So in this blog, I will show the steps to deploy NodeJS (Loopback) server + PostgreSQL database to Google App Engine + CloudSQL.

Deploy NodeJS (Loopback) to Google App Engine

Step 1: Creating your project in Google Cloud Console

For instructions, please go here. After creating your project, copy your project id ( xxxxxx-xxxxx ), we will use it later.

Step 2: Create an App Engine application

  • At first, you need to Enable Billing to use any Google Cloud services. Just click hamburger button, select Billing and fill the form.
  • Creating an App Engine application by selecting App Engine on the left menu, select NodeJS as language, config your region … then create. Wait a few minutes for Google create your host.

Step 3: Install Cloud SDK and gCloud command-line tool

Cloud SDK and gCloud command-line tool contains tools and libraries that help you easier to mange resources from GCP and deploy your application to GAE.

  • Go to here to install Cloud SDK for your machine
  • Authenticate gCloud tool to access your google account and connect to your project.
    Open terminal and cd to source code root folder. And run below command to authenticate:
    $ gcloud auth login
    It will open browser and visit google page. Just login and and accept authenticate.
  • if you see this warning:

Then run $ gcloud auth application-default --help to login and authenticate again.

  • Now, connect gCloud to your project.
    $ gcloud config set project [YOUR_PROJECT_ID]
    [YOUR_PROJECT_ID] is the project id you had written down at Step 1. If your account just only have 1 project, it will be connected automatically.

Step 4: Config your source code

In the root directory of your application, create a new file and name it app.yaml .

This app.yaml file contains GAE configurations, such as cpu, memory, network, variables, … You can take a look at this for more information.

For basic, just copy the content below to app.yaml :

# [START app_yaml]
runtime: nodejs
env: flex
# [END app_yaml]

Step 5: Deploy your application to Google App Engine

  • Open terminal and go to application’s root directory.
  • Run this command: $ gcloud app deploy
  • It will confirm your deploy:
descriptor:      [/path/to/your/project/app.yaml]
source: [/path/to/your/project]
target project: [YOUR_PROJECT_ID]
target service: [default]
target version: [20180511t154024]
target url: [https://your-project-id.appspot.com]
Do you want to continue (Y/n)?
  • Just type “y" and enter. It will start deploying your code. It takes several minutes.
  • If it deploys successfully, you will show the message as below:
DONE
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Updating service [default] (this may take several minutes)...done.
Setting traffic split for service [default]...done.
Stopping version [your-project-id/default/20180511t104322].
Sent request to stop version [your-project-id/default/20180511t104322]. This operation may take some time to complete. If you would like to verify that it succeeded, run:
$ gcloud app versions describe -s default 20180511t104322
until it shows that the version has stopped.
Deployed service [default] to [https://your-project-id.appspot.com]
You can stream logs from the command line by running:
$ gcloud app logs tail -s default
To view your application in the web browser run:
$ gcloud app browse
  • As suggestion above, you can run $ gcloud app browse to check your application and run $ gcloud app logs tail -s default to check server logs.

That’s all for deploying. Quite simple, right ?!! :D !!

Using Cloud SQL as your database service

Google Cloud SQL is a hosted and fully managed relational database service on Google’s infrastructure. The following instruction is help you create and connect your Loopback application to Cloud SQL for PostgreSQL.

Step 1: Enable Google Cloud SQL and Google Cloud SQL API for your project

  • Open GCP console and go to your project. Then select APIs & Services on the left menu and select ENABLE APIS AND SERVICES on the Dashboard screen.
  • Search and enable Google Cloud SQL service and Google Cloud SQL API

Step 2: Create Cloud SQL instance

  • On GCP console, select SQL on the left Menu.
  • Click on Create Instance. Select Cloud SQL for PostgreSQL, select your use case and then configure your instance. About the region, you should select the same region with your GAE.
  • Click create and wait for minutes.

Step 3: Create user and database

  • After Google finish creating your SQL instance, click to open it.
  • On the Overview tab, note your instance connection name (has syntax your-project-id:region:your-instance-name) we will use it later.
  • Then go to Users tab, you will see it has a default user name “postgres". Click Create user account button, input username and password, then click create to create your own user.
  • After that go to Databases tab, you will also see a default database name “postgres". You can delete it and create a new database with the name you want.

Step 4: Config your source code

  • Open app.yaml and add following lines:
beta_settings:cloud_sql_instances: [INSTANCE_CONNECTION_NAME]

[INSTANCE_CONNECTION_NAME] is the instance name you noted at step 3

  • Open your datasource configuration for deploying file (like datasource.production.js) and add the config below:
'use strict';module.exports = {
yourdbname: {
host: '/cloudsql/[YOUR_INSTANCE_CONNECTION_NAME]',
database: '[YOUR_DB_NAME]', // Create at step 3
password: '[YOUR_USER_PASSWORD]',
user: '[YOUR_USER_NAME]',
name: 'yourdbname',
connector: 'postgresql',
},
};
  • Open terminal and run $ gcloud app deploy to deploy source code to GAE

That's it. Now we deployed application to GAE that connect to Cloud SQL.

There are some notes you may want to know:

  • The steps above assume that your Cloud SQL instance is in the same project with your GAE. If not, you have to set up a service account to allow GAE access to Cloud SQL. Visit here for instructions.
  • To test Cloud SQL at your local environment, you have to setup Cloud SQL proxy. Visit here for instructions.
  • To connect to see the Cloud SQL database, you can use any database client app. The host to connect is the Primary IPv4 address in the Overview tab. Beside you may have authorize your IP address to connect the database. Open Authorization tab and add your public IP of your network to the authorized list. It is not the good way if you don't have a static public IP address. But it is the only way I know so far. You can comment below if you have better way.

Okay I think that’s enough for instructions. If you have any questions or suggestions, please comment below. Thank you :D !!!

P/s: Here some information about the different between Google App Engine and Google Compute Engine
https://stackoverflow.com/questions/22697049/what-is-the-difference-between-google-app-engine-and-google-compute-engine

SK Geek

We Are IT Geeks — All things IT, … as noted by geeks.

Phuoc Ngo.T

Written by

SK Geek

SK Geek

We Are IT Geeks — All things IT, … as noted by geeks.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade