Write Cloud Run functions

Cloud Run functions supports writing source code in a number of programming languages. The language runtime you choose and the type of function you want to write determine how to structure your code and implement your function. This page provides an overview of the types of Cloud Run functions and expectations for source code.

Types of Cloud Run functions

There are two types of Cloud Run functions:

Use an HTTP function when you need your function to have a URL endpoint and respond to HTTP requests, such as for webhooks. Use an event-driven function when your function should be triggered directly in response to events within your Google Cloud project, such as messages on a Pub/Sub topic or changes in a Cloud Storage bucket.

Source directory structure

In order for Cloud Run functions to locate your function definition, each language runtime has requirements for structuring your source code. The basic directory structure for a function in each runtime is shown below.

Node.js

The basic directory structure for Node.js functions is as follows:

.
├── index.js
└── package.json

By default, Cloud Run functions attempts to load source code from a file named index.js at the root of your function directory. To specify a different main source file, use the main field in your package.json file.

Your package.json file must also include the Functions Framework for Node.js as a dependency:

{
  "main": "index.js",
  "dependencies": {
    "@google-cloud/functions-framework": "^3.0.0"
  }
}

The code in your main file must define your function entry point and can import other code and Node.js modules as normal. The main file can also define multiple function entry points that can be deployed separately.

Python

The basic directory structure for Python functions is as follows:

.
├── main.py
└── requirements.txt

Cloud Run functions loads source code from a file named main.py at the root of your function directory. Your main file must be named main.py.

Your requirements.txt file must include the Functions Framework for Python as a dependency:

functions-framework==3.*

The code in your main.py file must define your function entry point and can import other code and external dependencies as normal. The main.py file can also define multiple function entry points that can be deployed separately.

Go

The basic directory structure for Go functions is as follows:

.
├── myfunction.go
└── go.mod

Your function must be in a Go package at the root of your project. The package and its source files can have any name, except your function cannot be in package main. If you need a main package, for example for local testing, you can create one in a subdirectory:

.
├── myfunction.go
├── go.mod
└── cmd/
    └── main.go

Your go.mod file must include the Functions Framework for Go as a dependency:

module example.com/my-module

require (
  github.com/GoogleCloudPlatform/functions-framework-go v1.5.2
)

The code in your root package must define your function entry point and can import other code from subpackages and dependencies as normal. Your package can also define multiple function entry points that can be deployed separately.

Java

The basic directory structure for Java functions is as follows:

.
├── pom.xml
└── src/
    └── main/
        └── java/
            └── MyFunction.java

Your Java source files must be under the src/main/java/ directory and can have any name. If your source files declare a package, add an extra directory under src/main/java with the name of the package:

.
├── pom.xml
└── src/
    └── main/
        └── java/
            └── mypackage/
                └── MyFunction.java

We recommend putting associated tests under a src/test/java/ subdirectory.

Your pom.xml file must include the Functions Framework for Java as a dependency:

...
    <dependency>
      <groupId>com.google.cloud.functions</groupId>
      <artifactId>functions-framework-api</artifactId>
      <version>1.0.4</version>
    </dependency>
...

The code in your source files must define your function entry point and can import other code and external dependencies as normal. Your source files can also define multiple function entry points that can be deployed separately.

C#

The basic directory structure for .NET functions is as follows:

.
├── MyFunction.cs
└── MyProject.csproj

You can structure your projects as you would any other .NET source code. Your source files can have any name.

Your project file must include the Functions Framework for .NET as a dependency:

...
    <PackageReference Include="Google.Cloud.Functions.Hosting" Version="1.0.0" />
...

The code in your source files must define your function entry point and can import other code and external dependencies as normal. Your source files can also define multiple function entry points that can be deployed separately.

You can also use the Cloud Run functions template package for .NET to generate the required files.

Ruby

The basic directory structure for Ruby functions is as follows:

.
├── app.rb
├── Gemfile
└── Gemfile.lock

Cloud Run functions loads source code from a file named app.rb at the root of your function directory. Your main file must be named app.rb.

Your Gemfile file must include the Functions Framework for Ruby as a dependency:

source "https://rubygems.org"
gem "functions_framework", "~> 1.0"

The code in your app.rb file must define your function entry point and can import other code and external dependencies as normal. The app.rb file can also define multiple function entry points that can be deployed separately.

PHP

The basic directory structure for PHP functions is as follows:

.
├── index.php
└── composer.json

Cloud Run functions loads source code from a file named index.php at the root of your function directory. Your main file must be named index.php.

Your composer.json file must include the Functions Framework for PHP as a dependency:

{
  "require": {
    "google/cloud-functions-framework": "^1.1"
  }
}

The code in your index.php file must define your function entry point and can import other code and external dependencies as normal. The index.php file can also define multiple function entry points that can be deployed separately.

If you are thinking of grouping multiple functions into a single project, be aware that every function may end up sharing the same set of dependencies. However, some of the functions may not need all of the dependencies.

Where possible, we recommend splitting up large multi-function codebases and putting each function in its own top-level directory as shown above, with its own source and project configuration files. This approach minimizes the number of dependencies required for a particular function, which in turn reduces the amount of memory your function needs.

Function entry point

Your source code must define an entry point for your function, which is the particular code that is executed when the Cloud Run function is invoked. You specify this entry point when you deploy your function.

How you define the entry point depends on the language runtime you use. For some languages, the entry point is a function, whereas for others the entry point is a class. To learn more about defining entry points and implementing Cloud Run functions in different languages, see Write HTTP functions and Write event-driven functions.

Dependencies

You can manage dependencies using standard tools for each runtime. For more information, see the appropriate page:

Next steps