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.
The following are the initial setup verification steps to be taken before the recipe can be executed:
- Create or select a GCP project.
- Enable billing and enable the default APIs (some APIs such as BigQuery, storage, monitoring, and a few others are enabled automatically).
- Verify that Google Cloud SDK is installed on your development machine.
- Verify that the default project is set properly:
$ gcloud config list
- 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.
- SSH into the VM from the Console:
- Navigate to the MongoDB's configuration file,
/etc/mongod.conf
, and update thebindIp
value to include0.0.0.0
:
# network interfaces net: port: 27017 bindIp: [127.0.0.1,0.0.0.0]
Note
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.
- Reboot the machine and verify that the MongoDB service is up and running.
- 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.
- Connect to the MongoDB instance running on the VM from your development machine:
$ mongo mongodb://<External IP>:27017
With the MongoDB server up and running, we'll make a few configurational changes and deploy the application to the App Engine:
- In the development machine, copy the
Chapter01/mysite
folder to a new folder calledChapter01/mysite-ae
from where we'll push the code to the App Engine:
$ cp mysite/ mysite-ae/ -r
- Navigate to the
mysite-ae
folder. Open the.env
file and update the path forMONGO_URI
to point to our VM:
MONGO_URI=mongodb://<External IP>:27017/mysite
- 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
- 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
- Now, we can deploy the application to the App Engine:
$ gcloud app deploy
..... 5cbd6acfb] to complete...done. Updating service [default]...done. Deployed service [default] to [https://<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