Oct 16, 2019

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

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

Related Blog Posts
Up to Spec: JavaScript Numeric Separators
Let's take a look at the proposal to add Numeric Separators to the JavaScript specification.
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]