<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=2634489&amp;fmt=gif">

Serverless, Getting Started with Google Cloud, Cloud Operations

How to develop and test your Cloud Functions locally

By C2C Community Team | March 21, 2023

December 16, 2022


Sara Ford

Senior Developer Relations Engineer

Christoph Stanger

Strategic Cloud Engineer


Set up your local development environment to have a great developer experience while working on your serverless application.

Developing code for serverless platforms requires a different approach to your development flow. Since the platform provides and maintains the entire stack, from compute infrastructure up to the function handler, your code runs in a fully managed and abstracted environment. This can make it time consuming and inefficient to debug your code by deploying and invoking a Function in the cloud. Fortunately, Cloud Functions offers an alternative that lets you implement and debug your code much faster.

In this blog post you'll learn how to do the following:

  • Run a Cloud Function locally

  • Invoke a Cloud Function with an Eventarc event locally

  • Use the same permissions as if it were deployed in the Cloud

  • Fetch secrets stored remotely from Secret Manager

  • Set Breakpoints in Visual Studio Code within a local Cloud Function

Cloud Functions builds upon the open source Functions Framework

Google Cloud strongly drives and aligns on open standards and open source. Cloud Functions are no exception. 

In fact, Google has a fully open-sourced runtime environment, responsible for wrapping a function code within a persistent HTTP application, known as the Functions Framework . It enables developers to run the same runtime environment as Cloud Functions on their machines or anywhere else. As a developer, you no longer need to make assumptions about how the serverless environment will behave or how to emulate the production experience.


As shown above, a 2nd gen Cloud Function actually represents a container hosted on Google's Serverless container infrastructure. Google fully provides the container's operation system, the necessary language runtime, and the Functions Framework. All these layers are packed together with your function code and its dependencies using Cloud Native Buildpacks during the deployment process. 

A real world example

Here is a typical example of a TypeScript application that processes thousands of images daily. This application derives insights from images and stores the resulting object labels. The following diagram illustrates this scenario as an event-driven microservice. 


For each newly uploaded image to a Cloud Storage bucketEventarc invokes a 2nd gen Cloud Function and passes along the image location including other event metadata. The TypeScript application code needs to access a secret stored in Secret Manager, uses the Vision API to extract object labels, and indexes the image to a Cloud Firestore document database.

The following code snippet shows the function code. The full example is available in this GitHub repository.


cloudEvent("index", async (cloudevent: CloudEvent<StorageObjectData>) => {
  console.log("-----\nProcessing for ", cloudevent.subject, "\n-----");
  if (!cloudevent.data) {
    throw "CloudEvent does not contain data."
  const filePath = `${cloudevent.data.bucket}/${cloudevent.data.name}`;
  //Get labes for Image via the Vision API
  const [result] = await imageAnnotatorClient.labelDetection(`gs://${filePath}`);
  const labelValues = result.labelAnnotations?.flatMap((o) => o.description);
  //hash file name with secret
  const hashedFilePath = createHmac('sha256', secret)
  //Store with filePath as key
  await firestore
        name: cloudevent.data.name,
        labels: labelValues,
        updated: cloudevent.time
  console.log(`Successfully stored ${cloudevent.data.name} to Firestore`)


This Function code uses multiple dependencies from the Google Cloud ecosystem:

  • Eventarc to trigger an invocation 

  • Secret Manager to provide a secret for filename encryption

  • Cloud Vision API to detect and fetch labels for the uploaded image

  • Cloud Firestore for storing the result

The README file in the repository contains all necessary steps to deploy this example application in your Google Cloud project.

In the following section, you will learn how to set up your local development environment to execute this TypeScript function outside of Cloud Functions. This setup allows you to iterate faster on your code by testing locally before deploying.   

How to run a Cloud Function locally 

The Functions Framework can be installed and run on any platform that supports the language, including your local machine or remote development servers.



Click here to read more

Recent Articles

Google Cloud Strategy

AI Cheat Sheet

AI is no more and no less the drive to create robots with human minds so they can do everything we do and more. Use this cheat sheet to help decode the space.
By Leah Zitter
AI and Machine Learning

CarCast with Bruno Aziza: What Makes Us Better Than AI?!

Unlock the secrets of human cognitive superiority over AI in this compelling CarCast with Bruno Aziza and Kenneth Cukier.
By Bruno Aziza
AI and Machine Learning

CarCast with Bruno Aziza: The Metrics You CAN'T Afford To ...

Discover essential CEO metrics: Rule of 40, CAC Ratio, NRR/GRR, and more. Optimize your business for success now!
By Bruno Aziza