paint-brush
Deploying a Serverless Node.js Application on Google Cloudby@nuralem
4,448 reads
4,448 reads

Deploying a Serverless Node.js Application on Google Cloud

by Nuralem AbizovMay 19th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Deploying 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.

People Mentioned

Mention Thumbnail
featured image - Deploying a Serverless Node.js Application on Google Cloud
Nuralem Abizov HackerNoon profile picture

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).

Image 1. Click on the Console button.

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


Image 2. Choose the current project.

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.

Image 3. Create a new project.

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

Image 4. Project settings.

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

Image 5. Select your project.


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


Image 6.  Set 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.


Image 7. Gcloud deployment info.

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!