Skip to content

Latest commit

 

History

History
87 lines (65 loc) · 2.38 KB

File metadata and controls

87 lines (65 loc) · 2.38 KB

Cloud Functions: Qwik Start - Command Line

Overview

Welcome to the Cloud Functions Qwik Start lab using the Command Line! Cloud Functions allow you to run code in response to events, making them ideal for tasks that need to be executed quickly or in response to specific events. In this lab, you'll create, deploy, and test a simple cloud function named "helloWorld" using the Google Cloud Shell command line.

What you'll do

  1. Create a simple cloud function.
  2. Deploy and test the function.
  3. View logs.

Tasks

Task 1: Create a Function

export REGION=<your-region>

Task 2: Create a Cloud Storage Bucket

gcloud config set compute/region $REGION
mkdir gcf_hello_world
cd gcf_hello_world

Task 3: Deploy Your Function

cat > index.js << EOF
exports.helloWorld = (data, context) => {
  const pubSubMessage = data;
  const name = pubSubMessage.data
    ? Buffer.from(pubSubMessage.data, 'base64').toString()
    : "Hello World";
  console.log("My Cloud Function: " + name);
};
EOF

gsutil mb -p $DEVSHELL_PROJECT_ID gs://$DEVSHELL_PROJECT_ID
gcloud functions deploy helloWorld \
--stage-bucket $DEVSHELL_PROJECT_ID \
--trigger-topic hello_world \
--runtime nodejs20

Task 4: Test the Function

Ensure your function is triggered by sending a message to the specified Pub/Sub topic.

Task 5: View Logs

Explore the logs generated by your cloud function.

Solution

# Task 1: Create a Function
export REGION=<your-region>

# Task 2: Create a Cloud Storage Bucket
gcloud config set compute/region $REGION
mkdir gcf_hello_world
cd gcf_hello_world

# Task 3: Deploy Your Function
cat > index.js << EOF
exports.helloWorld = (data, context) => {
  const pubSubMessage = data;
  const name = pubSubMessage.data
    ? Buffer.from(pubSubMessage.data, 'base64').toString()
    : "Hello World";
  console.log("My Cloud Function: " + name);
};
EOF

gsutil mb -p $DEVSHELL_PROJECT_ID gs://$DEVSHELL_PROJECT_ID
gcloud functions deploy helloWorld \
--stage-bucket $DEVSHELL_PROJECT_ID \
--trigger-topic hello_world \
--runtime nodejs20

Screenshot from 2023-12-29 14-45-10

Congratulations! You've completed the Cloud Functions Qwik Start lab using the Command Line. Explore more and experiment with Cloud Functions for your event-driven tasks.