Deploying a simple Node.js app with HTTPS on Google Cloud Run
Hello 👋,
Today, in the Eleven Blog, we will try to deploy a Node.js application on Google Cloud Run.
Eleven is a free and open-source Codespaces alternative with auto-HTTPS and long running processes.
Google Cloud Run is a fully managed compute platform that automatically scales containers, build your apps and deploy them in seconds.
The application that we will try to deploy is a simple Node.js app hosted on GitHub: https://github.com/jeremylevy/node-app
The code of our application is taken from the Node.js website “Getting Started Guide”.
1. Create a Google account
The first step is to create a Google account by going to: https://accounts.google.com/signup
Once created, you will be redirected to the Google Cloud console:
2. Create a new project
Next, we will need to create a “project”. A “project” corresponds to a set of infrastructure components.
If you remove a project, you will also remove all the infrastructure components linked to it.
Go to the following page to create your first project for our test app: https://console.cloud.google.com/projectcreate
Fill in the “Project name” field and click on the “Create” button.
3. Install the gcloud Command Line Interface (CLI)
Now that you’ve created a project, you will need to install the gcloud CLI.
The steps required to install the CLI vary depending on your operating system. Go to the following page to choose the best installer for your platform: https://cloud.google.com/sdk/docs/install
When the installation is complete, run the gcloud init
command to log in and configure the CLI:
$ gcloud init
When asked to pick a project to use, choose the one created earlier:
$ gcloud init
...
Pick cloud project to use:
[1] my-node-test-app
[2] Enter a project ID
[3] Create a new project
Please enter numeric choice or text value (must exactly match list item): 1
4. Prepare the app
Now that the CLI is installed and configured, clone the sample app that we’ve prepared:
$ git clone https://github.com/jeremylevy/node-app.git
$ cd node-app
5. Deploy
Time to deploy!
Run the following command in the node-app
directory:
$ gcloud run deploy node-test-app --source .
- If you are prompted to enable the “Artifact Registry API”, respond by pressing
y
. - When prompted for region, select the region of your choice by typing its number. (If you want to use a custom domain and have HTTPS enabled for it, you need to choose one of the following regions: “asia-east1”, “asia-northeast1”, “asia-southeast1”, “europe-north1”, “europe-west1”, “europe-west4”, “us-central1”, “us-east1”, “us-east4” or “us-west1”.)
- When prompted to create an “Artifact Registry Docker” repository, respond by pressing
y
. - When prompted to allow “unauthenticated invocations”, respond by pressing
y
.
Then, wait a few moments until the deployment is complete:
$ gcloud run deploy node-test-app --source .Deployment failed
ERROR: (gcloud.run.deploy) The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.Logs URL: https://console.cloud.google.com/logs/viewer?
For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start
And… we’ve an error!
By going to the displayed “Logs URL”, we could see that there are many problems with our app:
- First, our app needs to listen on the
PORT
environment variable if defined. - Secondly, our app needs to have a
start
script that will contain the command required to start it. - Thirdly, our app needs to bind to
0.0.0.0
and not127.0.0.1
.
In the index.js
file, update the hostname
and port
constants to conform to the Cloud Run requirements:
const hostname = '0.0.0.0';
const port = process.env.PORT || 3000;
Then, in the package.json
file, add a start
property to the scripts
object:
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Now, rerun the deploy
command:
$ gcloud run deploy node-test-app --source .
...
Service [node-test-app] revision [node-test-app-00002-xof] has been deployed and is serving 100 percent of traffic.
Service URL: https://node-test-app-msj2tijn6q-ue.a.run.app
Finally, go the displayed “Service URL” https://node-test-app-msj2tijn6q-ue.a.run.app:
It works! Congrats!
6. Enable HTTPS
To enable HTTPS, you will first need to verify domain ownership by running the following command:
$ gcloud domains verify <base-domain>
<base-domain>
is the base domain that you want to verify. For example, if you want to enable HTTPS for subdomain.example.com
, you should verify the ownership of example.com
.
Your browser will open to the “Google Webmaster Central” website. Choose your domain name provider in the list and follow the instructions. Once done, click on the “Validate” button.
Now that the domain ownership is confirmed, run the following command:
$ gcloud beta run domain-mappings create --service node-test-app --domain <my-domain>
- If you are prompted to “install beta components”, respond by pressing
y
. - When prompted for a region, choose the same region than previously.
$ gcloud beta run domain-mappings create --service node-test-app --domain test.my-domain.comNAME RECORD TYPE CONTENTS
test CNAME ghs.googlehosted.com.
Now, add a CNAME
record that points to the DNS target provided by the gcloud CLI (ghs.googlehosted.com. in this case) and you’re set.
(Note that it can take several minutes for the managed SSL certificate to be issued.)