All Projects → GoogleCloudPlatform → Functions Framework Go

GoogleCloudPlatform / Functions Framework Go

Licence: apache-2.0
FaaS (Function as a service) framework for writing portable Go functions

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Functions Framework Go

functions-framework-java
FaaS (Function as a service) framework for writing portable Java functions
Stars: ✭ 101 (-40.24%)
Mutual labels:  google-cloud, functions-as-a-service
functions-framework-php
FaaS (Function as a service) framework for writing portable PHP functions
Stars: ✭ 186 (+10.06%)
Mutual labels:  google-cloud, functions-as-a-service
Functions Framework Nodejs
FaaS (Function as a service) framework for writing portable Node.js functions
Stars: ✭ 790 (+367.46%)
Mutual labels:  google-cloud, functions-as-a-service
Event Gateway
React to any event with serverless functions across clouds
Stars: ✭ 1,604 (+849.11%)
Mutual labels:  functions-as-a-service
Spydra
Ephemeral Hadoop clusters using Google Compute Platform
Stars: ✭ 128 (-24.26%)
Mutual labels:  google-cloud
Openwhisk Devtools
Development tools for building and deploying Apache OpenWhisk
Stars: ✭ 141 (-16.57%)
Mutual labels:  functions-as-a-service
Applied Deep Learning With Tensorflow
Learn applied deep learning from zero to deployment using TensorFlow 1.8+
Stars: ✭ 160 (-5.33%)
Mutual labels:  google-cloud
Terraform Provider Google Beta
Terraform Google Cloud Platform Beta provider
Stars: ✭ 123 (-27.22%)
Mutual labels:  google-cloud
Terraform Kubernetes
Example of deploying a Kubernetes cluster to Google Cloud using Terraform
Stars: ✭ 152 (-10.06%)
Mutual labels:  google-cloud
Flogo
Project Flogo is an open source ecosystem of opinionated event-driven capabilities to simplify building efficient & modern serverless functions, microservices & edge apps.
Stars: ✭ 1,891 (+1018.93%)
Mutual labels:  functions-as-a-service
Gofn
Function process via docker provider (serverless minimalist)
Stars: ✭ 134 (-20.71%)
Mutual labels:  functions-as-a-service
Gcpsketchnote
If you are looking to become a Google Cloud Engineer , then you are at the right place. GCPSketchnote is series where I share Google Cloud concepts in quick and easy to learn format.
Stars: ✭ 2,631 (+1456.8%)
Mutual labels:  google-cloud
Googlelanguager
R client for the Google Translation API, Google Cloud Natural Language API and Google Cloud Speech API
Stars: ✭ 145 (-14.2%)
Mutual labels:  google-cloud
Colabctl
Google Colaboratory background/task executioner & controller.
Stars: ✭ 128 (-24.26%)
Mutual labels:  google-cloud
Gcping
Like gcping.com but a command line tool
Stars: ✭ 153 (-9.47%)
Mutual labels:  google-cloud
Spark Bigquery Connector
BigQuery data source for Apache Spark: Read data from BigQuery into DataFrames, write DataFrames into BigQuery tables.
Stars: ✭ 126 (-25.44%)
Mutual labels:  google-cloud
Telescopes
Telescopes is a cloud instance types and full cluster layout recommender consisting of on-demand and spot/preemptible AWS EC2, Google, Azure, Oracle and Alibaba cloud instances.
Stars: ✭ 146 (-13.61%)
Mutual labels:  google-cloud
Googlecomputeenginer
An R interface to the Google Cloud Compute API, for launching virtual machines
Stars: ✭ 132 (-21.89%)
Mutual labels:  google-cloud
React Native Live Translator
bleh bleh bleh
Stars: ✭ 133 (-21.3%)
Mutual labels:  google-cloud
Gcp Audit
A tool for auditing security properties of GCP projects.
Stars: ✭ 140 (-17.16%)
Mutual labels:  google-cloud

Functions Framework for Go Build Status GoDoc Go version

An open source FaaS (Function as a Service) framework for writing portable Go functions, brought to you by the Google Cloud Functions team.

The Functions Framework lets you write lightweight functions that run in many different environments, including:

The framework allows you to go from:

func HelloWorld(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello, World!")
}

To:

curl http://my-url
# Output: Hello, World!

All without needing to worry about writing an HTTP server or request handling logic.

Features

  • Build your Function in the same container environment used by Cloud Functions using buildpacks.
  • Invoke a function in response to a request
  • Automatically unmarshal events conforming to the CloudEvents spec
  • Portable between serverless platforms

Quickstart: Hello, World on your local machine

  1. Install Go 1.11+.

  2. Create a Go module:

    go mod init example.com/hello
    

    Note: You can use a different module name rather than example.com/hello.

  3. Create a function.go file with the following contents:

    package hello
    
    import (
    	"net/http"
    	"fmt"
    )
    
    // HelloWorld writes "Hello, World!" to the HTTP response.
    func HelloWorld(w http.ResponseWriter, r *http.Request) {
    	fmt.Fprint(w, "Hello, World!\n")
    }
    

    Note that you can use any file name or package name (convention is to make package name same as directory name).

  4. To run locally, you'll need to create a main package to start your server (see instructions below for container builds to skip this step and match your local development environment to production):

    mkdir cmd
    
  5. Create a cmd/main.go file with the following contents:

    package main
    import (
    	"log"
    	"os"
    	"context"
    	"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
    	"example.com/hello"
    )
    func main() {
    	ctx := context.Background()
    	if err := funcframework.RegisterHTTPFunctionContext(ctx, "/", hello.HelloWorld); err != nil {
    		log.Fatalf("funcframework.RegisterHTTPFunctionContext: %v\n", err)
    	}
    	// Use PORT environment variable, or default to 8080.
    	port := "8080"
    	if envPort := os.Getenv("PORT"); envPort != "" {
    		port = envPort
    	}
    	if err := funcframework.Start(port); err != nil {
    		log.Fatalf("funcframework.Start: %v\n", err)
    	}
    }
    
  6. Start the local development server:

    cd cmd
    go run main.go
    # Output: Serving function...
    
  7. Send requests to this function using curl from another terminal window:

    curl localhost:8080
    # Output: Hello, World!
    

Go further: build a deployable container

  1. Install Docker and the pack tool.

  2. Build a container from your function using the Functions buildpacks:

    pack build \
    	--builder gcr.io/buildpacks/builder:v1 \
    	--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
    	--env GOOGLE_FUNCTION_TARGET=HelloWorld \
    	my-first-function
    
  3. Start the built container:

    docker run --rm -p 8080:8080 my-first-function
    # Output: Serving function...
    
  4. Send requests to this function using curl from another terminal window:

    curl localhost:8080
    # Output: Hello, World!
    

Run your function on serverless platforms

Google Cloud Functions

Deploy from your local machine using the gcloud command-line tool. Check out the Cloud Functions quickstart.

Container environments based on Knative

The Functions Framework is designed to be compatible with Knative environments. Just build and deploy your container to a Knative environment. Note that your app needs to listen PORT environment variable per Knative runtime contract.

Functions Framework Features

The Go Functions Framework conforms to the Functions Framework Contract, As such, it supports HTTP functions, background event functions, and CloudEvent functions (as of v1.1.0). The primary build mechanism is the GCP buildpacks stack, which takes a function of one of the accepted types, converts it to a full HTTP serving app, and creates a launchable container to run the server.

HTTP Functions

The Framework provides support for handling native Go HTTP-style functions:

func HTTPFunction(w http.ResponseWriter, r *http.Request) error {
	// Do something with r, and write response to w.
}

These functions are registered with the handler via funcframework.RegisterHTTPFunctionContext.

Background Event Functions

Background events are also supported. This type of function takes two parameters: a Go context and a user-defined data struct.

func BackgroundEventFunction(ctx context.Context, data userDefinedEventStruct) error {
	// Do something with ctx and data.
}

This type of event requires you to define a struct with the appropriate data fields (e.g. those for a PubSub message or GCS event) and pass that struct as the data parameter. See the samples for details.

The context parameter is a Go context.Context, and contains additional event metadata under a functions-specific key. This data is accesible via the cloud.google.com/go/functions/metadata package:

m := metadata.FromContext(ctx)

These functions are registered with the handler via funcframework.RegisterEventFunctionContext.

CloudEvent Functions

The Functions Framework provides support for unmarshalling an incoming CloudEvent payload into a cloudevents.Event object. These will be passed as arguments to your function when it receives a request.

func CloudEventFunction(ctx context.Context, e cloudevents.Event) error {
	// Do something with event.Context and event.Data (via event.DataAs(foo)).
}

These functions are registered with the handler via funcframework.RegisterCloudEventFunctionContext.

To learn more about CloudEvents, see the Go SDK for CloudEvents.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].