Lab Intro: Cloud Run Functions: Qwik Start - Command Line
Table of Contents
Introduction
In this tutorial, you will learn how to get started with Google Cloud Run Functions using the command line. This guide will help you understand the basics of deploying serverless applications, making it easier to manage your cloud services efficiently. By the end, you'll be equipped to deploy a simple function to Cloud Run and understand the underlying concepts.
Step 1: Set Up Google Cloud SDK
- Install Google Cloud SDK: Download and install the Google Cloud SDK from the official site.
- Initialize the SDK:
- Open a terminal window.
- Run the command:
gcloud init
- Follow the prompts to authenticate and set your default project.
Step 2: Create Your Function
- Write Your Function: Create a simple function in your preferred programming language. For example, in Node.js, you might write:
exports.helloWorld = (req, res) => { res.send('Hello, World!'); };
- Save the File: Save your function code in a file named
index.js
.
Step 3: Prepare Your Deployment
- Create a Package File: If you're using Node.js, ensure you have a
package.json
file in the same directory. You can create one using:npm init -y
- Install Dependencies: Install any necessary packages using npm, for example:
npm install express
Step 4: Deploy Your Function to Cloud Run
-
Build Your Container: Use Cloud Build to create a container image. Run the following command:
gcloud builds submit --tag gcr.io/[PROJECT_ID]/[IMAGE_NAME] .
Replace
[PROJECT_ID]
with your Google Cloud project ID and[IMAGE_NAME]
with a name for your image. -
Deploy the Image: Deploy your container image to Cloud Run with:
gcloud run deploy --image gcr.io/[PROJECT_ID]/[IMAGE_NAME] --platform managed
- Follow the prompts to select your region and allow unauthenticated invocations if desired.
Step 5: Access Your Function
- Get the Service URL: After deployment, the command line will provide a URL for your function.
- Test Your Function: Open a web browser and navigate to the provided URL. You should see your function's output, such as "Hello, World!"
Conclusion
Congratulations! You have successfully deployed a serverless function using Google Cloud Run. This tutorial covered setting up the Google Cloud SDK, creating a simple function, preparing it for deployment, and accessing the deployed function. As next steps, consider exploring additional functionalities of Cloud Run, such as scaling and integrating with other Google Cloud services.