Data Ops

Using GCP Cloud Functions in Data Engineering

Constantin LunguUpdated 2 min read

Photo by Guillaume Bourdages on Unsplash

Some of the most interesting Data Engineering projects I've worked with leveraged Google Cloud Functions. It's a serverless execution environment that lets you run your code without provisioning or managing servers.

I found it to be very versatile, even more so for Data Processing tasks. Things like ingesting data, developing a lightweight API or consuming data from an endpoint, parsing and transforming a flat file.

As expected, it's very well integrated with other GCP services, so you could easily say, orchestrate run from a Google Cloud Workflow (a pairing that I used and enjoyed).

The advantages are pretty obvious: no server to manage, pay per invocation, scalability (if you need it) and simplicity to get started.

Now, there are of course a lot of things to think about when setting up such serverless functions :
- the size of the unit of work and runtime resources
- authentication (unauthenticated/authentication required)
- networking - where can the function can be invoked from
- auto-scaling and concurrency
- avoiding cold starts
- reusing heavy computations across invocations

Google Cloud console screenshot of the Cloud Functions welcome page, describing it as a lightweight, event-based compute solution, with a Create Function button and cards for local development, testing, how-to guides, tips, tutorials and billing.

Google Cloud console screenshot of the Cloud Functions Create function configuration: 2nd gen environment, test-cloud-function in europe-west1, HTTPS trigger with Require authentication, 256 MiB memory, 60 s timeout, concurrency 1, autoscaling 0 to 100 instances, service account dev-tf-sa.

Cloud Functions console, Code step: Runtime Python 3.11, Entry point hello_http, and a TEST FUNCTION button.

import functions_framework

@functions_framework.http
def hello_http(request):
    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
        <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
    """
    request_json = request.get_json(silent=True)
    request_args = request.args

    if request_json and 'name' in request_json:
        name = request_json['name']
    elif request_args and 'name' in request_args:
        name = request_args['name']
    else:
        name = 'World'
    return 'Hello {}!'.format(name)

Google Cloud Functions console Configure pre-deployment test panel: the triggering event is the JSON body {"name": "fellow earthlings"}, HTTP method POST, with a Run Test button and a last test on Oct 5, 2023 that returned HTTP status 200.

Cloud Functions pre-deployment test log: builder steps run pip check with no broken requirements, then Function is ready to test, followed by execution responses Hello Greetings, fellow earthlings!! and Hello fellow earthlings!

But overall, it felt like the setup was pretty straightforward and the first time I tried it, I was able to get off the ground pretty quickly. You can of course test the function on your local machine (until you're happy with it) and automate its deployment with Terraform.

When used properly, a cloud function can be very useful for Real-Time and Batch ETL, automation and API Development, all while being scalable and flexible.


Enjoyed this? Here are some related articles you might find useful: