1. Home
  2. Training Library
  3. Google Cloud Platform
  4. Courses
  5. Deploying and Implementing Google Cloud Platform Solutions

Deploying a Cloud Function that receives Google Cloud events

play-arrow
Start course
Overview
DifficultyBeginner
Duration57m
Students58
Ratings
5/5
star star star star star

Description

This course has been designed to teach you how to deploy and implement Google Cloud Platform solutions. The content in this course will help prepare you for the Associate Cloud Engineer exam.

Learning Objectives

  • To learn how to deploy Kubernetes Engine resources on Google Cloud Platform
  • To learn how to deploy and implement App Engine and Cloud Functions resources
  • To learn how to use Cloud Launcher and Deployment Manager

Intended Audience

  • Those who are preparing for the Associate Cloud Engineer exam
  • Those looking to learn more about GCP networking and compute features

Prerequisites

To get the most from this course then you should have some exposure to GCP resources, such as Kubernetes Engine, App Engine, Cloud Functions, Cloud Launcher, and Deployment Manager. However, this is not essential.

Transcript

Google Cloud Functions is an event-driven serverless compute platform that allows you to run serverless code in the cloud. It automatically scales and requires no server provisioning. The way it works is simple. When a cloud service emits an event, Cloud Functions responds to the event and then invokes other services and APIs as necessary. It then writes back to the cloud service. 

In this lesson, I'm going to demonstration how to deploy a Cloud Function that receives Google Cloud events using a Pub/Sub trigger. 

What we're going to do is deploy a background cloud function called helloworld and then trigger the function by publishing a message to a Cloud Pub/Sub topic called mytopic. 

To prepare for this demo, I've already created a topic called mytopic in Google Cloud Platform. I've also downloaded the helloworld sample function from GitHub and extracted it to my laptop. 

You can see the helloworld sample function folder here on my screen. 

In order to make this stuff all work, what I need to do is launch my Cloud Shell and open up the Code Editor because what I want to do is upload the helloworld folder which contains my function to Google Cloud Platform. So let's go ahead and launch the Cloud Shell here and then what I'll do is I'll launch Code Editor here. 

So now that I have my Shell and Code Editor opened here, let me drag my helloworld folder into my Explorer here. Now we're doing this because this is where I'm going to deploy from. 

To deploy our function with a Cloud Pub/Sub trigger, I need to run the G Cloud Functions deploy command. However, I need to run the command in the directory that I uploaded my function to so let me switch over to the helloworld directory first here. 

Now that I'm in the helloworld directory, I can run the G Cloud Functions deploy command. What I'll do here is copy and paste this command in and then I'll explain it. 

Notice the name that I'm using for the trigger topic flag. It's the topic that I created ahead of this demo that's called mytopic. I'm going to call my function deployment hello_pubsub. You can also see in this command that I'm specifying the Python 3.7 runtime. Let's go ahead and deploy. And we can see here that my deployment has completed. 

Now the function that I just deployed is going to check mytopic for messages. I can trigger the function by publishing a message to mytopic. For this example, the message that I publish is simply going to be my name. The function will then include my name in a greeting message. To publish my message which will in turn trigger my function, I need to run the G Cloud Pub/Sub topics publish command and let me copy it over here so you can see what it looks like. I'm going to expand this window a little bit. There we go. 

Essentially what we're doing here is we're publishing a message with the name Thomas to the mytopic topic. So let's go ahead and publish our message here. Okay, so we have our message published to our topic. Now that I've published my message, I can check the logs to make sure that the process has actually completed, that the message was actually sent. To do this, I need to run the G Cloud Functions logs read command that I'm going to copy in now. 

What this does is allow me to read the logs that have been generated. Now what the limit flag does here is limit the return of the logs to the last 50. And let's hit Enter a few times here to bring this up. Now as you can see on your screen, there is a message here that displays Hello Thomas. This tells me that my function is working as expected because that's exactly what it was supposed to do. Now if you want to perform this demo in your own environment, visit the first URL that you see on your screen and follow the instructions for creating a topic called mytopic in GCP. 

To download the sample helloworld function that I used in this demo, visit the second URL to download it. Extract the downloaded zip file and copy the helloworld folder to your desktop. The helloworld folder is located in the functions subfolder within python-docs-samples master. Once you've created a topic called mytopic and extracted the helloworld folder that contains the sample function, you can pick up at the beginning of this demo and do it yourself.

About the Author

Students5021
Courses20
Learning paths3

Tom is a 25+ year veteran of the IT industry, having worked in environments as large as 40k seats and as small as 50 seats. Throughout the course of a long an interesting career, he has built an in-depth skillset that spans numerous IT disciplines. Tom has designed and architected small, large, and global IT solutions.

In addition to the Cloud Platform and Infrastructure MCSE certification, Tom also carries several other Microsoft certifications. His ability to see things from a strategic perspective allows Tom to architect solutions that closely align with business needs.

In his spare time, Tom enjoys camping, fishing, and playing poker.

Covered Topics