Deploying a Serverless Node.js Application on Google Cloud

Written by nuralem | Published 2023/05/19
Tech Story Tags: nodejs | google-cloud-platform | serverless | backend | programming | serverless-architecture | coding | software-engineering

TLDRDeploying a serverless Node.js application to Google Cloud involves several key steps. Set up a Google Cloud Account and a New Project. Install and Configure Google Cloud SDK. Write Your Nodejs Code. Deploy Your Function. Test Your Application. Back to the page you came from.via the TL;DR App

Embarking on your first software development project can be a daunting experience, especially when it comes to navigating the world of serverless applications and cloud deployments. If you've been seeking a beginner-friendly guide to deploying your first Node.js application on Google Cloud, look no further. This article is designed with you in mind - breaking down each step of the process in a simple, easy-to-understand way. This comprehensive guide will help you get your serverless application off the ground. Let's dive in and start exploring the power of cloud computing!

Content

Deploying a serverless Node.js application to Google Cloud involves several key steps:

  1. Set up a Google Cloud Account and a New Project
  2. Install and Configure Google Cloud SDK
  3. Write Your Node.js Code
  4. Deploy Your Function
  5. Test Your Application

Set up a Google Cloud Account and a New Project

Firstly, you'll need a Google Cloud account. If you don't have one already, head over to Google Cloud and create a free account. Once your account is set up, create a new project in the Google Cloud Console (Image 1).

Then you should click on “My Project“ (image 2) and then click on “NEW PROJECT“ (image 3) button.

On this page, you will see all existing projects that you have access to see. If you are working on an organization and have an account on it, you will see all projects of the current company.

On the project settings, you can set up a name and an organization, if exists (Image 4).

Don’t forget to select your current project on the list. Finally, you will see “Your project name“ on the main Google Console page.

Install and Configure Google Cloud SDK

To deploy your Node.js application, you'll need to install Google Cloud SDK. You can download it here. Once installed, you need to authenticate and configure the SDK to use your Google Cloud project:

  1. Open your terminal and run the following command to authenticate:
gcloud auth login

This will open a new browser window where you can log in with your Google account.

  1. Next, set your project ID with the following command (replace 'your-project-id' with the actual ID of your project):
gcloud config set project your-project-id

Write Your Node.js Code

Writing a simple nodejs project I have detailed explained in my previous article. The main project code will be:

const http = require('http');

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World\n');
});

server.listen(8080, '0.0.0.0', () => {
    console.log('Server running at http://0.0.0.0:8080/');
});

A package.js will be:

{
  "name": "hackernoon",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Also, you need to create a app.yaml file for gcloud and save it in the project folder. Put this code:

runtime: nodejs18
service: default

Cloud Functions now requires an active billing account. You will need to add one, otherwise, you will not be able to deploy your nodejs app on GCP. Enable Cloud Functions, after adding billing info, your Google account will have free 300 creds on it.

Run cmd.exe from the project folder and execute this command:

gcloud app create

This command will enable the App Engine on your current project. You should choose a location after the gcloud will do everything for you.

Creating App Engine application in project [ivory-studio-387214] and region [europe-central2]....done.
Success! The app is now created. Please use `gcloud app deploy` to deploy your first app.

Finally, execute this deploy command:

gcloud app deploy --quiet app.yaml --promote --stop-previous-version --version main-v1

You will see the message that your nodejs has successfully deployed.

On your console, you will see the deployed information:

descriptor:                  [C:\Users\Nuriq\Desktop\Hackernoon\app.yaml]
source:                      [C:\Users\Nuriq\Desktop\Hackernoon]
target project:              [ivory-studio-387214]
target service:              [default]
target version:              [main-v1]
target url:                  [https://ivory-studio-387214.lm.r.appspot.com]
target service account:      [App Engine default service account]

Now, you can access your app via target url: https://ivory-studio-387214.lm.r.appspot.com

Conclusion

Congratulations on taking your first steps into the world of serverless applications and Google Cloud deployment. In this guide, you've learned how to set up your Google Cloud account, install and configure Google Cloud SDK, write a Node.js function, and deploy and test it on Google Cloud. This is a significant milestone in your software development journey!

However, this is just the tip of the iceberg. The vast expanse of cloud services, along with the versatility of Node.js, opens a world of endless possibilities. It allows you to build scalable, efficient, and powerful applications.

Thank you for taking the time to follow along with this guide. We look forward to sharing more exciting aspects of software development in our future posts. Until then, keep coding, and see you next week!


Written by nuralem | Software engineer
Published by HackerNoon on 2023/05/19