Hello Cloud Run
Overview
Cloud Run is a managed compute 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.
Cloud Run is built from 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.
The goal of this lab is for you to build a simple containerized application image and deploy it to Cloud Run.
Objectives
In this lab, you learn to:
- Enable the Cloud Run API.
- Create a simple Node.js application that can be deployed as a serverless, stateless container.
- Containerize your application and upload to Artifact Registry.
- Deploy a containerized application on Cloud Run.
- Delete unneeded images to avoid incurring extra storage charges.
Setup and requirements
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Activate Google Cloud Shell
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
-
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
-
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- You can list the active account name with this command:
Output:
Example output:
- You can list the project ID with this command:
Output:
Example output:
Reference
Basic Linux Commands
Below you will find a reference list of a few very basic Linux commands which may be included in the instructions or code blocks for this lab.
Command --> | Action | . | Command --> | Action |
---|---|---|---|---|
mkdir (make directory) | create a new folder | . | cd (change directory) | change location to another folder |
ls (list ) | list files and folders in the directory | . | cat (concatenate) | read contents of a file without using an editor |
apt-get update | update package manager library | . | ping | signal to test reachability of a host |
mv (move ) | moves a file | . | cp (copy) | makes a file copy |
pwd (present working directory ) | returns your current location | . | sudo (super user do) | gives higher administration privileges |
Task 1. Enable the Cloud Run API and configure your Shell environment
- From Cloud Shell, enable the Cloud Run API :
- If you are asked to authorize the use of your credentials, do so. You should then see a successful message similar to this one:
- Set the compute region:
- Create a LOCATION environment variable:
Task 2. Write the sample application
In this task, you will build a simple express-based NodeJS application which responds to HTTP requests.
- In Cloud Shell create a new directory named
helloworld
, then move your view into that directory:
-
Next you'll be creating and editing files. To edit files, use
nano
or the Cloud Shell Code Editor by clicking on the Open Editor button in Cloud Shell. -
Create a
package.json
file, then add the following content to it:
Most importantly, the file above contains a start script command and a dependency on the Express web application framework.
-
Press CTRL+X, then Y, then Enter to save the
package.json
file. -
Next, in the same directory, create an
index.js
file, and copy the following lines into it:
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 uploaded to Artifact Registry.
- Press CTRL+X, then Y, then Enter to save the
index.js
file
Task 3. Containerize your app and upload it to Artifact Registry
- To containerize the sample app, create a new file named
Dockerfile
in the same directory as the source files, and add the following content:
-
Press CTRL+X, then Y, then Enter to save the
Dockerfile
file. -
Now, build your container image using Cloud Build by running the following command from the directory containing the
Dockerfile.
(Note the $GOOGLE_CLOUD_PROJECT environmental variable in the command, which contains your lab's Project ID):
Cloud Build is a service that executes your builds on Google Cloud. It executes a series of build steps, where each build step is run in a Docker container to produce your application container (or other artifacts) and push it to Artifact Registry, all in one command.
Once pushed to the registry, you will see a SUCCESS message containing the image name (gcr.io/[PROJECT-ID]/helloworld
). The image is stored in Artifact Registry and can be re-used if desired.
- List all the container images associated with your current project using this command:
- Register
gcloud
as the credential helper for all Google-supported Docker registries:
- To run and test the application locally from Cloud Shell, start it using this standard
docker
command:
- In the Cloud Shell window, click on Web preview and select Preview on port 8080.
This should open a browser window showing the "Hello World!" message. You could also simply use curl localhost:8080
.
Task 4. Deploy to Cloud Run
- Deploying your containerized application to Cloud Run is done using the following command adding your Project-ID:
The allow-unauthenticated flag in the command above makes your service publicly accessible.
- When prompted confirm the
service name
by pressing Enter.
Wait a few moments until the deployment is complete.
On success, the command line displays the service URL:
You can now visit your deployed container by opening the service URL in any browser window.
Congratulations! You have just deployed an application packaged in a container image to Cloud Run. Cloud Run automatically and horizontally scales your container image to handle the received requests, then scales down when demand decreases. In your own environment, you only pay for the CPU, memory, and networking consumed during request handling.
For this lab you used the gcloud
command-line. Cloud Run is also available via Cloud console.
- From the Navigation menu, in the Serverless section, click Cloud Run and you should see your
helloworld
service listed:
Task 5. Clean up
While Cloud Run does not charge when the service is not in use, you might still be charged for storing the built container image.
- You can either decide to delete your Google Cloud project to avoid incurring charges, which will stop billing for all the resources used within that project, or simply delete your
helloworld
image using this command :
-
When prompted to continue type
Y
, and press Enter. -
To delete the Cloud Run service, use this command :
- When prompted to continue type
Y
, and press Enter.
End your lab
When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
- 1 star = Very dissatisfied
- 2 stars = Dissatisfied
- 3 stars = Neutral
- 4 stars = Satisfied
- 5 stars = Very satisfied
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Congratulations!
You have completed this lab!
Next steps / learn more
For more information on building a stateless HTTP container suitable for Cloud Run from code source and pushing it to Artifact Registry, view:
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.