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.
- Create a simple cloud function.
- Deploy and test the function.
- View logs.
export REGION=<your-region>
gcloud config set compute/region $REGION
mkdir gcf_hello_world
cd gcf_hello_world
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
Ensure your function is triggered by sending a message to the specified Pub/Sub topic.
Explore the logs generated by your cloud function.
# 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
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.