How to Create our first Cloud Function in Google Cloud Platform

gray laptop computer showing html codes in shallow focus photography
Reading Time: 4 minutes

Hello Readers! In this blog we we are going to see How to create our first Cloud Function in Google Cloud Platform. Let’s See first what is google cloud function?

Let’s get started with Google Cloud Functions

Cloud Function

Cloud Functions will be responsible for scaling up your code to handle the millions of requests come in. You don’t really need to worry about where this function is running.

Some Uses of Cloud Function:

  • Run code in response to events
    • Write your business logic in Node.js, Python, Go, Java, .NET, and Ruby.
    • Don’t worry about servers or scaling or availability(only worry about your code).
  • Each execution run in a separate instances
  • Pay only for what you use
    • Number of invocations
    • Compute Time of the invocations
    • Amount of memory and CPU provisioned

Some important concepts of cloud function:

  • Event: Upload object to cloud storage
  • Trigger: Respond to event with a Function call
    • Trigger- which function to trigger when an event happens?
    • Functions- Take event data and perform action?
  • Events are triggered from
    • Cloud Storage
    • Cloud Pub/Sub
    • HTTP POST/GET/PUT/DELETE/Options
    • Firebase
    • Cloud Firestore
    • Stack Driver Logging

Prerequisite:

Google Cloud Platform (GCP) Account

Step1. Create a project on GCP (Google Cloud Platform).

I am assuming you already have GCP account. If not then I would suggest you can create your first GCP account.

Alright so to begin with the first thing which we are going to do is Create Project on GCP. Click on NEW PROJECT

I have created a project with the name my-cloud-function-project.

gcp

Step2. Create a Cloud Function for the project

In the second step, we need to create the Cloud Function inside the project which we created on the above steps.

Before creating the service account make sure to select the Project which you created.

Search and select the Cloud Function.

gcp-cloud-function

It will redirect you to Cloud Function to create the page.

Before creating the function. You have need to keep your API’s should be enabled.

Creating Function

Now you need to input some meaningful name for the Cloud Function. In my case, I have kept the name my-first-cloud-function.

Now I will go with the 1st gen. You can configure as per your requirements. You can go with the 1st gen and 2nd gen.

gcp-create-func

Step4: Choose run time, build connection and security settings

So, the maximum amount of duration for a specific invocation of a specific is only 9 minutes and maximum amount of memory that you can allocate is 8GB.

1.Runtime Service Account and Autoscaling

So, we are going to choose default settings, if you want to configure. You can select as per your requirements.

gcp-cloud

2. Trigger

Now we are going to configure trigger section in which we will select the allow unauthenticated invocations and unchecked the require HTTPS.

You can copy the above URL so that we can send a to it a little later.

gcp-cloud

I will minimise the runtime, build function and security settings and I will click on Next.

Now, you can configure this specific code that you would want to use to run your function.

So, it will show you some warning. So, you need to click on enabled APIs.

We don’t really need to worry about what’s in here or that we need to say enough is clicking on Deploy.

gcp-cloud-function

Now, it took about a couple of minutes and the deployment was complete.

Now you have created your gcp function, and if i go into that specific function enough. You can see the details about this specific function over here.

cloud-function-first

So, if i click on Testing, this is where you can actually test the function. So ,you can test the function.

cloud-function

Similarly we can check all Logs of our Function.

cloud-function-logs

So, you can test the function using Test The Function button, Click on Test The Function and you would see the response back from the function in here if it want actually adding logs.

Congratulations! You have successfully created your first Cloud Function service on Google Cloud.

Finally, if you want to access using URL, you can choose the url method.

URL Method:

Now we will go back to source, click on edit button and copy the url as I mention above.

And last open up with new tab window.

Conclusion:

Thank you for sticking to the end. So, In this blog we have seen How to create our first Cloud Function in Google Cloud Platform.Therefore If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.Here is my email:gayatri.singh@knoldus.com

Written by 

I am an enthusiastic , hard-working and determine girl with strong attention to detail and eager to learn about new technologies.