Getting Started with Serverless on Google Cloud Platform (GCP)

Serverless helps you to develop and deploy your code without worrying about underlying infrastructure since you don’t need to manage any infrastructure for your code. It will help you to improve productivity and focus on development.

Cloud functions are light-weight functions which can be executed based on various events like HTTP, GCS buckets etc. In this tutorial, we will discuss how we can create a simple Cloud Function and trigger based on GCS bucket events like “Create” and “Delete”.

Let’s get Started.

No alt text provided for this image


  • Create Function page will appear requesting for Function details. Please provide name of the function and memory you would like to allocate to the cloud function.
  • Since we would like to trigger this function upon creation of an object in GCS bucket please select trigger as “Cloud Storage”, Event Type as “Finalize/Create”, Bucket as “Bucket on which you would like to trigger this function on the creation of an object.”
No alt text provided for this image
  • You can use a different method to write your code. We will use Inline editor to write our code. We are using python language in this example. However, you can use node.js as well.
No alt text provided for this image
  • Select “runtime” which you would like to use. We will use Python in this tutorial.
No alt text provided for this image
  • For python, we can write the code in file “main.py” and if there are any dependencies we can specify that in “requirement.txt” file.
No alt text provided for this image

PFB sample code which we would like invokes upon creation on any object in GCS bucket as above.

def hello_gcs(event, context):
"""Triggered by a change to a Cloud Storage bucket.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
file = event
print(f”Processing file: {file[‘name’]}.”)
print(“Size of the File (in Bytes) : {0}”.format(file[‘size’]))
print(“Storage Class : {0}”.format(file[‘storageClass’]))

Above code will print the Name, Size and Storage Class of the File.

  • Specify the Name of the function which needs to be executed. “hello_gcs” in this example.
  • Save function.

Note: It will take a minute or so to create and deploy the function.

  • You can check the created function under “Cloud Functions” in google console.
No alt text provided for this image
  • Now upload any file in the above bucket.
No alt text provided for this image
  • In order to check the function output, you can select the function and click on “View logs” to check the logs for triggered function.
No alt text provided for this image
  • You could see in Logs that “function Execution started” and printed the information about the file like Name, Size and Storage Class.

Note: We have now successfully created and Triggered Google Cloud Function.

  • In order to trigger the Cloud function on the deletion of an object from GCS bucket, you can create another function and select the event type as “delete” while creating the function and follow the above steps to trigger and verify the function.

Hope you have enjoyed the tutorial.

To view or add a comment, sign in

More articles by Neeraj Bhadani

  • Start your Journey with Apache Spark - Part 3

    This is Part-3 of the series “Start your Journey with Apache Spark”. Please read Part-1 and Part-2 if you haven’t got a…

  • Start your Journey with Apache Spark - Part 2

    This is Part-2 for blog series “Start your Journey with Apache Spark”. I would recommend to please read the Part-1 if…

  • Start your Journey with Apache Spark — Part 1

    Let’s begin our journey with Apache Spark. In this blog series, I will discuss the overview of Apache Spark.

    2 Comments
  • IBM India Referral Hiring Drive

    IBM India Employee Referral requirements for 3 to 15 yrs of experience at various locations for various skills listed…

  • IBM India Referral hiring drive

    IBM India Referral hiring drive at various locations for Skill set listed below with 3 to 15 years of experience,If…

Others also viewed

Explore content categories