---
title: Develop and Test Your Cloud Functions Locally
description: Optimize serverless development with local Cloud Functions. Debug faster & run code locally for efficient testing. Enhance your developer experience now!
image: https://www.c2cglobal.com/hubfs/Article%20Images/How%20to%20develop%20and%20test%20your%20Cloud%20Functions%20locally.jpg
---

[![c2c logo blue](https://www.c2cglobal.com/hubfs/c2c-logo-blue.svg "c2c logo blue")](https://www.c2cglobal.com/)

[Serverless](https://www.c2cglobal.com/articles/tag/serverless), [Getting Started with Google Cloud](https://www.c2cglobal.com/articles/tag/getting-started-with-google-cloud), [Cloud Operations](https://www.c2cglobal.com/articles/tag/cloud-operations)

# How to develop and test your Cloud Functions locally

By [C2C Community Team](https://www.c2cglobal.com/articles/author/c2c-community-team) | March 21, 2023

December 16, 2022

 

![https://storage.googleapis.com/gweb-cloudblog-publish/images/google_cloud_2022_H7glFJz.max-2500x2500.jpg](https://www.c2cglobal.com/hs-fs/hubfs/Imported_Blog_Media/google_cloud_2022_H7glFJz_max-2500x2500-1.jpg?width=1015&height=500&name=google_cloud_2022_H7glFJz_max-2500x2500-1.jpg)

##### 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 ](https://cloud.google.com/functions/docs/functions-framework#:~:text=The%20Functions%20Framework%20lets%20you,Knative-based%20environments). 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.

![https://storage.googleapis.com/gweb-cloudblog-publish/images/CloudFunctionStack.max-1600x1600.png](https://www.c2cglobal.com/hubfs/Imported_Blog_Media/CloudFunctionStack_max-1600x1600-1.png)

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](https://buildpacks.io/) 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. 

![https://storage.googleapis.com/gweb-cloudblog-publish/images/CF_debugging_architecture.max-1500x1500.png](https://www.c2cglobal.com/hubfs/Imported_Blog_Media/CF_debugging_architecture_max-1500x1500-1.png)

For each newly uploaded image to a [Cloud Storage bucket](https://cloud.google.com/storage), [Eventarc](https://cloud.google.com/eventarc) invokes a [2nd gen Cloud Function ](https://cloud.google.com/functions)and passes along the image location including other event metadata. The TypeScript application code needs to access a secret stored in [Secret Manager](https://cloud.google.com/secret-manager), uses the [Vision API](https://cloud.google.com/vision) to extract object labels, and indexes the image to a [Cloud Firestore](https://cloud.google.com/firestore) document database.

The following code snippet shows the function code. The full example is available in this [GitHub repository](https://github.com/GoogleCloudPlatform/devrel-demos/blob/main/app-dev/cloud-functions-2ndgen-local-development/src/index.ts).

 

```
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)
```

```
    .update(filePath)
```

```
    .digest('hex');
```

```
 - 
```

```
  //Store with filePath as key
```

```
  await firestore
```

```
    .doc(`${cloudevent.data.bucket}/${hashedFilePath}`)
```

```
    .set(
```

```
      {
```

```
        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](https://github.com/GoogleCloudPlatform/devrel-demos/tree/main/app-dev/cloud-functions-2ndgen-local-development) 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**](https://cloud.google.com/blog/topics/developers-practitioners/how-to-develop-and-test-your-cloud-functions-locally/?utm_campaign=CDR__GCP_developtestfunctions_121522)

---

## Recent Articles

<https://www.c2cglobal.com/articles/generative-ai-are-you-behind>

[Data Analytics](https://www.c2cglobal.com/articles/tag/data-analytics)

## [Generative AI: Are You Behind?!](https://www.c2cglobal.com/articles/generative-ai-are-you-behind)

Review the latest insights from the AI Readiness Report.

By Bruno Aziza

<https://www.c2cglobal.com/articles/make-gen-ai-work-landscape-slms-vs.-llms-cost-more>

[Industry Solutions](https://www.c2cglobal.com/articles/tag/industry-solutions)

## [Make "Gen AI Work": Landscape, SLMs vs. LLMs, Cost & More...](https://www.c2cglobal.com/articles/make-gen-ai-work-landscape-slms-vs.-llms-cost-more)

Discover the 5 metrics you need to know in order to be an exceptional CEO and Operator.

By Bruno Aziza

<https://www.c2cglobal.com/articles/ai-cheat-sheet>

[Google Cloud Strategy](https://www.c2cglobal.com/articles/tag/google-cloud-strategy)

## [AI Cheat Sheet](https://www.c2cglobal.com/articles/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

## Subscribe to our newsletter

Get the latest from C2C, direct to your inbox.

[Subscribe](https://share.hsforms.com/1znKGo6ySS9uaRyvLQQzxRw4qbyr)

![c2c-logo-white-tagline](https://www.c2cglobal.com/hubfs/c2c-logo-white-tagline.svg "c2c-logo-white-tagline")

C2C is the only peer-to-peer community built to support, connect, and educate Google Cloud customers across regions and industries so they can better harness the power of the cloud—and each other—to solve their biggest challenges and drive innovation.

©2024  C2C Global. All rights reserved. [Privacy Policy](https://www.c2cglobal.com/privacy)

20 N Wacker Drive • Suite 1810 • Chicago, IL • 60606

**Join over 60K members on C2C.**

Let's connect!

- [mailto:hello@c2cglobal.com](mailto:hello@c2cglobal.com)
- <https://m.youtube.com/channel/UCWOli-_qQyZziBdPQ-6p-eQ>
- <https://www.linkedin.com/company/66934702>
- [![X](https://www.c2cglobal.com/hubfs/twitter-x-icon-white.svg)](https://twitter.com/MeetC2C)

![](https://px.ads.linkedin.com/collect/?pid=2634489&fmt=gif)