Local Testing for GCP Cloud Functions

It’s easy to write Google Cloud Functions in Python. The trick is to put a function that takes a single object in a file called main.py. Their example is simply:

def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
    request_json = request.get_json()
    if request.args and 'message' in request.args:
        return request.args.get('message')
    elif request_json and 'message' in request_json:
        return request_json['message']
    else:
        return f'Hello World!'

But what they don’t help you with is how to run this locally. The answer is easy once you think about it, and a hint is in their example. They use Flask! Essentially GP runs Flask on the front of your function and sends the request object to it. No reason why you can’t do the same. Here is my very simple example:

from flask import Flask, request
import main

app = Flask(__name__)

@app.route('/', methods=['POST'])
def wrapper():
    return main.hello_world(request)

I saved this file called flasker.py and ran it via Flask as:

FLASK_APP=flasker.py flask run

It will tell you what URL to hit locally and it should do the same as when it’s running in GCP. If you have your GCP credentials setup correctly, your BigQuery and PubSub will work, etc. Note that it will work well, so make sure you are pointing to the correct environment. Also be careful that Flask doesn’t sneak into your dependencies. No telling what may happen then.

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

One thought on “Local Testing for GCP Cloud Functions

  1. Ramon says:

    The Node.js Emulator is a Node.js application that implements the Cloud Functions REST and gRPC APIs, and includes a command line interface for managing the application. The Node.js Emulator allows you to deploy, run, and debug Node.js Cloud Functions on your local machine before deploying them to the production Cloud Functions service.

Leave a Reply to Ramon Cancel reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Testing a Quarkus Kafka Application
Quarkus, a “Kubernetes Native Java stack”, enables lighter Java applications with faster startup times. In a recent post, I talked about scaling Kafka consumers in Kubernetes. Quarkus applications fit right into this picture because they […]
Gitpod and Hringvegurinn
Iceland Ever seen an advertisement for visiting Iceland? Have you noticed that they all mention Hrinvegurinn or the Ring Road, as a good tour? (If you haven’t seen a tour ad for Iceland, Steindi Jr […]
Tacos and Steak, an Istio story
This post originally appear on Marty Henderson’s personal blog A brief history In the before times of 2017, Varun Talwar and Louis Ryan sat under a forgotten waterfall at the edge of the world and […]
Feature Flags in Terraform
Feature flagging any code can be useful to developers but many don’t know how to or even that you can do it in Terraform. Some benefits of Feature Flagging your code You can enable different […]