Building Python Serverless Applications on GCP

Introduction to GCP Goblet

Austen Novis
4 min readDec 30, 2020
goblet with a red sunset background

Goblet is an easy-to-use framework that enables developers to quickly spin up fully featured REST APIs with python on Google Cloud Platform (GCP). Rather than having to cope with configuring and deploying cloud resources, developers can have their stack up and serving requests with just a few simple commands as demonstrated with the example in this blog. Before diving into the example, I will give a quick overview of serverless and the serverless ecosystem in GCP, but feel free to skip if you are already familiar with those topics.

What is Serverless

Serverless is a powerful new paradigm brought on by cloud computing that is transforming how to develop, deploy, and manage applications. The name “serverless” can be misleading, since code is still being executed on servers, but instead means that the cloud provider, in our case GCP, handles all responsibilities of the servers. This includes all server management tasks such as provisioning and security patching in addition to the actual code execution. Without the burden of any infrastructure requirements, the developer is able to solely focus on writing code that matters.

Serverless on GCP

GCP offers a suite of serverless products geared to help you deploy a variety of different applications and solutions. The main serverless product is cloud functions, similar to AWS lambda, which are single-purpose functions that are run for a short duration. For longer running workloads there is Cloud Run, which allows you to deploy and scale containers serverlessly. While these are all great solutions, they are not ideal for deploying a simple rest application such as a flask application, which would require you to containerize your application and deploy to cloud run. These solutions are pretty heavy for a simple app and can be costly since they require servers running behind the scenes, which you are being charged for even if they are idle.

Cloud Functions are ideal, since they can be triggered on demand and you pay only for when they are running, however, each cloud function can only correspond to one endpoint preventing any path based routing. Fortunately, there is a new GCP service currently in beta called Api Gateway that can serve as an entrypoint for your restful API and can trigger cloud functions as well as other backend compute if you desire. The downside to API gateway and other GCP serverless services is that they require significant configuration to set up properly. This is where Goblet comes into play. Goblet is a framework for writing serverless applications in GCP with the goal of making it as simple as possible to write and deploy REST applications. Goblet uses simple decorators, similar to flask, to create the necessary configurations and automatically deploy the required services.

Getting Started

Goblet is an easy-to-use framework that enables developers to quickly spin up fully featured REST APIs with python on GCP. It allows you to quickly create, configure, and deploy applications that use GCP cloud functions behind the scenes.

Prerequisites

Before you can deploy an application, be sure you have credentials configured. You should run gcloud auth login and sign in to the desired project. Make sure to have the correct services enabled in your GCP project which include

  • api-gateway
  • cloudfunctions
  • storage

When setting the default location note the api-gateway is only available in asia-east1, europe-west1, and us-central1.

Installation

To install goblet, open an interactive shell and run:

Sample Project

Begin by creating your project directory, which should include a main.py and a requirements.txt. Make sure requirements.txt includes goblet-gcp.

To create a rest api that has a GET route /home and a POST route /home/{id} add the following to your main.py.

Here the app.route decorator will add a new route to your api. The route will default to GET, but can be customized using the methods field in the decorator. Read the docs for all options.

Deploying

Let’s deploy this app. Make sure you’re in the app directory and simply run goblet deploy:

This command will create all the required GCP service configurations including for cloud functions and API gateway and then create them in your account.

Destroying

To tear down all resources created by goblet you can use the goblet destroy command on the CLI.

Conclusion

In this blog post we have gone through a simple example, that with a few lines of code will configure and deploy a fully functioning restful API. For advanced configuration options such as authentication and scheduling check out the documentation and if you have any comments, issues, or feedback feel free to open a ticket on GitHub. This project is still in development so if you have suggestions for features you would like to see feel free to submit an issue as well or comment below.

--

--

Austen Novis
Austen Novis

No responses yet