9

Google Cloud Functions Tutorial : Writing our first Google Cloud Function

 3 years ago
source link: https://rominirani.com/google-cloud-functions-tutorial-writing-our-first-google-cloud-function-a62de60b5c90
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Google Cloud Functions Tutorial : Writing our first Google Cloud Function

This is part of a Google Cloud Functions Tutorial Series. Check out the series for all the articles.

This post will cover how to write your first Google Cloud Function. We shall primarily be using the Google Cloud Console to create, deploy and test out the Cloud Function.

We shall be using JavaScript to write our first Google Cloud Function. As a result, you can either select the Node.js 6 or the Node.js 8 runtime for your Google Cloud Function. More on that later.

Our first Google Cloud Function will be a HTTP-Trigger based function, which means that we can directly invoke it via a HTTPs endpoint that will be assigned to our function.

Let’s get started.

Google Cloud Platform Project Setup

The first step is to ensure that you have a Google Cloud Platform account with Billing setup. Remember that there is an always free tier when you sign up for Google Cloud and you could use that too.

Optional: If you have not created a Google Cloud Platform project, I suggest to create a new Cloud Project under which you can have all the examples covered in this series. Create the New Project from the Cloud Console and fill out the details for your project. A sample screenshot is shown below. Please note down your project-id.

Image for post
Image for post

Once you have setup your project, the next step is to enable the Google Cloud Functions API for your project. From the Cloud Console, click on the Main Menu in the Top Left corner and navigate to Menu → APIs and Services → Dashboard as shown below:

Image for post
Image for post

Click on Enable APIs and Services. This will bring up a screen as shown below, where you can enter Cloud Functions in the search field.

Image for post
Image for post

Click on Google Cloud Functions API and then click on Enable. This will enable the API for use in your Google Cloud Platform project.

We are now primarily going to be using the Google Cloud Functions menu option in the main menu as shown below. You will find that in the Products → Cloud Functions option.

Image for post
Image for post

I strongly suggest to use the Pin feature to pin this option to the top of the Cloud Console menu for easy access.

This completes our Project setup. We are now ready to explore writing our first Google Cloud Function.

Writing our first Google Cloud Function

Click on Cloud Functions in the main menu. This will lead to a screen shown below:

Image for post
Image for post

Click on Create function.

You will see an intimidating form come up next as shown below:

Image for post
Image for post

Fill out the details as given below.

  • Name : Give a name to your function. For e.g. function-1 or any other name that you chose.
  • Memory : Go with the default 256MB for now since we are just in testing mode.
  • Trigger: We are going to go with the HTTP Trigger since we want to invoke it directly via a HTTPs endpoint. Go ahead and select HTTP Trigger. Note the unique URL that it generated.
  • Runtime: This will currently have all the runtimes that are either in General Availability (GA) or Beta. You should see Node.js 6, Node.js 8 and Python 3.7.
  • Source Code : We can paste our source code here. A default template is provided to you and for now, we will go with that. Study the index.js template that is provided. Notice the function that we are exporting via the exports.helloWorld statement.
  • In the field for function to execute, it will need to match the exports statement, hence this value will be helloWorld.

Click on Create to create the function. This will take a while at times, so please be patient.

Once the operation is successful, you will see your function listed in the list of functions as shown below. Note the Runtime column value, which is Node.js 6 that we selected.

Image for post
Image for post

If you click on the ellipsis at the right corner to see additional options as shown below:

Image for post
Image for post

This will let you test and view logs for your function execution. We shall come to that in a while.

Click on the function name i.e. function-1 above to proceed further.This will bring up the function details as shown below. You can do several things from here. You can manage your source code, edit your function details, test your function, view your logs, view metrics and more.

Image for post
Image for post

Click on Trigger and you will notice that the trigger type for our function is HTTP trigger. The interesting thing to note here is the Unique HTTPs URL that is generated for our function. Notice how it uses the region, projectid and your function name provided to form this url. This URL is directly invocable and you can try that in your browser.

Image for post
Image for post

The next thing to observe is to click on Source. You can chose to edit the source from here too if you want and deploy a newer version.

Image for post
Image for post

The source code is simple for now and we are using the standard template that it has provided. Some high level details are given below:

  • The function that is being exported is helloWorld.
  • This is a HTTP-trigger based functions that is passed the ExpressJS request and response objects. Irrespective of whether you selected Node.js 6 or Node.js 8 runtimes, this function signature will be the same.
  • It checks if a message parameter is passed to it in the query or the body. If not, it takes a default value of “Hello World” and then sends back a response with the value that it got.
  • The most important thing here that you should focus on is that once the function is done with, you must send back the response immediately. This is a signal to the Google Cloud Functions service that your function execution is done with. It will then compute the time that your function took to execute this request. You must keep this as short as possible and not forget to close out the function in this manner, otherwise the function will keep executing till it times out. Currently the default timeout is set to 60 seconds (1 min) and can be extended to a maximum of 540 seconds (9 mins).

Let us go ahead and test our function. Click on the Testing tab. This will present a screen as shown below:

Image for post
Image for post

Notice that our code was parsing out the message attribute via the ExpressJS req.body.Messagecall. Here in the Testing form, we provide a JSON formatted message for the same as shown below. Go ahead and provide that. Click on Test function.

Image for post
Image for post

This will invoke our function and the output that our function returned will be shown to us. Notice the Logs for our function execution. It will contain some metadata in terms of the time that it took our function to execute, the status code that it returned. In addition to that, it will also have any messages and errors that our function throws in terms of the console messages.

I suggest that you test out the functions for some 5–10 times manually. Once done, go ahead and visit the General tab. It will show you various metrics for execution time, latency, errors and more.

Image for post
Image for post
Invocations — Metrics
Image for post
Image for post
Execution time — Metrics

This completes the tutorial for writing our first Google Cloud Function.

Proceed to the next part : Setting up a Local Development Environment or go back to the Google Cloud Functions Tutorial Home Page.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK