Cloud Run Starter Tutorial

1. Overview

Cloud Run is a fully managed serverless platform that enables you to run stateless containers that are invocable via HTTP requests. Cloud Run is serverless: it abstracts away all infrastructure management, so you can focus on what matters most — building great applications.

It is built on Knative, letting you choose to run your containers either fully managed with Cloud Run, or in your Google Kubernetes Engine cluster with Cloud Run on GKE.

What you will learn

In this lab, you will learn how to do the following:

  • Deploy Cloud Run application

Prerequisites

  • This lab assumes familiarity with the Cloud Console and Cloud Shell environments.

2. Setup and Requirements

Cloud Project setup

  1. Sign-in to the Google Cloud Console and create a new project or reuse an existing one. If you don't already have a Gmail or Google Workspace account, you must create one.

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • The Project name is the display name for this project's participants. It is a character string not used by Google APIs. You can update it at any time.
  • The Project ID is unique across all Google Cloud projects and is immutable (cannot be changed after it has been set). The Cloud Console auto-generates a unique string; usually you don't care what it is. In most codelabs, you'll need to reference the Project ID (it is typically identified as PROJECT_ID). If you don't like the generated ID, you may generate another random one. Alternatively, you can try your own and see if it's available. It cannot be changed after this step and will remain for the duration of the project.
  • For your information, there is a third value, a Project Number which some APIs use. Learn more about all three of these values in the documentation.
  1. Next, you'll need to enable billing in the Cloud Console to use Cloud resources/APIs. Running through this codelab shouldn't cost much, if anything at all. To shut down resources so you don't incur billing beyond this tutorial, you can delete the resources you created or delete the whole project. New users of Google Cloud are eligible for the $300 USD Free Trial program.

Environment Setup

Activate Cloud Shell by clicking on the icon to the right of the search bar.

ecdc43ada29e91b.png

From Cloud Shell, enable the Cloud Run API:

gcloud services enable run.googleapis.com

If prompted to authorize, click "Authorize" to continue.

6356559df3eccdda.png

This should produce a successful message similar to this one:

Operation "operations/acf.p2-327036483151-73d90d00-47ee-447a-b600-a6badf0eceae" finished successfully.

3. Prepare Application

First, you will prepare a simple express-based Node.js application that responds to HTTP requests.

In Cloud Shell create a new directory named starter-nodejs, then change into that directory:

mkdir starter-nodejs
cd starter-nodejs

Create a package.json file, by running commands below:

cat > ./package.json << EOF
{
  "name": "cloudrun-starter-app",
  "version": "1.0.0",
  "description": "Node.js Starter Application",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "",
  "license": "Apache-2.0",
  "dependencies": {
    "express": "^4.18.2"
  }
}
EOF

The file above contains a start script command and a dependency on the Express web application framework.

Next, in the same directory, create a index.js file by running commands below:

cat > ./index.js << EOF
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  console.log('Received a request.');
  res.send("Hello Cloud Run!");
});

const port = process.env.PORT || 8080;

app.listen(port, () => {
  console.log('Listening on port', port);
});
EOF

This code creates a basic web server that listens on the port defined by the PORT environment variable. Your app is now finished and ready to be containerized and deployed.

4. Deploy Cloud Run Application

Run the command below to deploy your application:

gcloud run deploy starter-app \
  --source . \
  --region us-central1 \
  --allow-unauthenticated \
  --max-instances=3

Confirm Artifact Registry repository creation:

Deploying from source requires an Artifact Registry Docker repository to store built containers. A repository named [cloud-run-source-deploy] in region [us-central1] will be created.

Do you want to continue (Y/n)? y

As a result of the previous command, following steps will be executed:

d661616f9b2fffcf.png

Explore output of these steps in Cloud Console: Cloud Build, Cloud Storage, Artifact Registry and Cloud Run.

f536961280192919.png

5. Congratulations!

Congratulations! You have just deployed an application to Cloud Run.

What we've covered:

  • How to deploy a starter application on Cloud Run

What's next:

Explore other Cymbal Eats codelabs:

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.

Deleting the project

The easiest way to eliminate billing is to delete the project that you created for the tutorial.