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
Performance Test Liquibase Update
When doing a liquibase update to a database if you’re having performance issues, it can be hard to find out which updates are causing problems. If you need to measure the time to apply each […]
TICK Stack Monitoring for the Non-Technical
TICK – Telegraf, Influx, Chronograf, and Kapacitor – is a method of monitoring your systems and applications. In this article, I discuss in non-technical terms what the difference is between TICK and Prometheus Grafana A […]
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]