Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Google Cloud Platform Cookbook

You're reading from   Google Cloud Platform Cookbook Implement, deploy, maintain, and migrate applications on Google Cloud Platform

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher Packt
ISBN-13 9781788291996
Length 280 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Legorie Rajan PS Legorie Rajan PS
Author Profile Icon Legorie Rajan PS
Legorie Rajan PS
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Compute 2. Storage and Databases FREE CHAPTER 3. Networking 4. Security 5. Machine Learning and Big Data 6. Management Tools 7. Best Practices 8. Other Books You May Enjoy

Hosting the Node.js application on Google App Engine

We'll implement the same Node.js application used in the first recipe on Google App Engine. App Engine is a PaaS solution where we just need to deploy the code in any of the supported languages (Node.js, Java, Ruby, C#, Go, Python, and PHP), and the platform takes care of scaling automatically, health checking, and updates to the underlying OS.

App Engine provides the compute power for the application and so for the database, we'll have to use a managed MongoDB service such as mLab or a MongoDB instance of GCE. As we already have a VM running MongoDB from our previous recipe, we'll use that to serve our application running on App Engine.

Getting ready

The following are the initial setup verification steps to be taken before the recipe can be executed:

  1. Create or select a GCP project.
  2. Enable billing and enable the default APIs (some APIs such as BigQuery, storage, monitoring, and a few others are enabled automatically).
  3. Verify that Google Cloud SDK is installed on your development machine.
  4. Verify that the default project is set properly:
$ gcloud config list 
  1. The VM which runs MongoDB from our first recipe allows connections only from the localhost. We'll have to modify the configuration to allow connections from the external world.
  2. SSH into the VM from the Console:
  1. Navigate to the MongoDB's configuration file, /etc/mongod.conf, and update the bindIp value to include 0.0.0.0:
   # network interfaces
    net:
      port: 27017
      bindIp: [127.0.0.1,0.0.0.0] 
In a few versions of Mongo, it is just enough to comment our the bind_ip line in the mongodb config to allow access from outside the instance.
  1. Reboot the machine and verify that the MongoDB service is up and running.
  2. We'll also create a new firewall rule to allow access to port 27017 from anywhere:
$ gcloud compute firewall-rules \
create default-allow-mongo-27017 \ --allow tcp:27017 \ --source-ranges 0.0.0.0/0 \ --target-tags mysite-server \ --description "Allow port 27017 access to mysite-server"

The following screenshot shows the details of the firewall rule:

The MongoDB instance is now open to the world without any login credentials. So for production systems, make sure you secure the MongoDB instance with an admin user and run the mongod process using the --auth option.

  1. Connect to the MongoDB instance running on the VM from your development machine:
$ mongo mongodb://<External IP>:27017

How to do it...

With the MongoDB server up and running, we'll make a few configurational changes and deploy the application to the App Engine:

  1. Logging into the Cloud Platform Console, create an App Engine application, select the region where the application will be hosted and enable billing. You can follow along with the interactive tutorial provided by Google to host your first Node.js application to the App Engine.
  1. In the development machine, copy the Chapter01/mysite folder to a new folder called Chapter01/mysite-ae from where we'll push the code to the App Engine:
$ cp mysite/ mysite-ae/ -r 
  1. Navigate to the mysite-ae folder. Open the .env file and update the path for MONGO_URI to point to our VM:
MONGO_URI=mongodb://<External IP>:27017/mysite 
  1. Verify that all the packages are installed and launch the application on the development machine, pointing to the database on the Cloud:
$ npm install
$ npm start  
  1. The application's configurations are governed by a file called app.yaml. Create a new file with the following content:
# Basic configurations for the NodeJS application 
runtime: nodejs 
env: flex
  1. Now, we can deploy the application to the App Engine:
$ gcloud app deploy
  1. Once the application is deployed, the URL to access the application is provided. Fire up your favorite browser and navigate to the appspot URL and verify that the KeystoneJS application is running properly:
  .....
5cbd6acfb] to complete...done. Updating service [default]...done. Deployed service [default] to [https://<project-id>.appspot.com]
  1. You can stream logs from the command line by running:
$ gcloud app logs tail -s default
  1. To view your application in the web browser run:
$ gcloud app browse  
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime