Package: goblet-gcp
GOBLETGoblet is a framework for writing serverless rest apis in python in google cloud. It allows you to quickly create and deploy python apis backed byCloud FunctionsandCloud Runas well as other GCP serverless services.It provides:A command line tool for creating, deploying, and managing your apiA decorator based API for integrating with GCP API Gateway, Storage, Cloudfunctions, PubSub, Scheduler, Cloudrun Jobs, BQ remote functions, Redis, Monitoring alerts and other GCP services.Local environment for testing and running your api endpointsDynamically generated openapispecSupport for multiple stagesYou can create Rest APIs:fromgobletimportGoblet,jsonify,goblet_entrypointapp=Goblet(function_name="goblet_example")goblet_entrypoint(app)@app.route('/home')defhome():return{"hello":"world"}@app.route('/home/{id}',methods=["POST"])defpost_example(id:int)->List[int]:returnjsonify([id])You can also create other GCP resources that are related to your REST api:fromgobletimportGoblet,jsonify,goblet_entrypointapp=Goblet(function_name="goblet_example")goblet_entrypoint(app)# Scheduled job@app.schedule("5 * * * *")defscheduled_job():returnjsonify("success")# Pubsub subscription@app.pubsub_subscription("test")defpubsub_subscription(data):app.log.info(data)return# Example Redis Instanceapp.redis("redis-test")# Example Metric Alert for the cloudfunction metric execution_count with a threshold of 10app.alert("metric",conditions=[MetricCondition("test",metric="cloudfunctions.googleapis.com/function/execution_count",value=10)])Once you've written your code, you just run goblet deploy and Goblet takes care of deploying your app.$gobletdeploy-lus-central1
...
https://api.uc.gateway.dev

$curlhttps://api.uc.gateway.dev/home{"hello":"world"}Note: Due to breaking changes in Cloudfunctions you will need to wrap your goblet class in a function. Seeissue #88. In the latest goblet version (0.5.0) there is a helper functiongoblet_entrypointthat can be used as well.goblet_entrypoint(app)Resources SupportedInfrastructurevpc connectorredisapi gatewaycloudtaskqueuepubsub topicsbq spark stored proceduresBackendscloudfunctioncloudfunction V2cloudrunRoutingapi gatewayhttpHandlerspubsubschedulerstorageeventarccloudrun jobsbq remote functionscloudtask targetuptime checksAlertsBackend AlertsUptime AlertsPubSub DLQ AlertsData Typing Frameworks SupportedpydanticmarshmallowInstallationTo install goblet, open an interactive shell and run:pip install goblet-gcpMake sure to have the correct services enabled in your gcp project depending on what you want to deployapi-gateway,cloudfunctions,storage,pubsub,schedulerYou will also need to installgcloud clifor authenticationQuickStartIn this tutorial, you'll use the goblet command line utility to create and deploy a basic REST API. This quickstart uses Python 3.10. You can find the latest versions of python on the Python download page.To install Goblet, we'll first create and activate a virtual environment in python3.10:$python3--version
Python3.10.10
$python3-mvenvvenv310
$.venv37/bin/activateNext we'll install Goblet using pip:python3-mpipinstallgoblet-gcpYou can verify you have goblet installed by running:$goblet--help
Usage:goblet[OPTIONS]COMMAND[ARGS]...
...CredentialsBefore you can deploy an application, be sure you have credentials configured. You should rungcloud auth application-default loginand sign in to the desired project.Creating Your Projectcreate your project directory, which should include an main.py and a requirements.txt. Make sure requirements.txt includesgoblet-gcp$ls-la
drwxr-xr-x.goblet
-rw-r--r--main.py
-rw-r--r--requirements.txtYou can ignore the .goblet directory for now, the two main files we'll focus on is app.py and requirements.txt.Let's take a look at the main.py file:fromgobletimportGoblet,goblet_entrypointapp=Goblet(function_name="goblet_example")goblet_entrypoint(app)@app.route('/home')defhome():return{"hello":"world"}This app will deploy an api with endpoint/home.Running LocallyRunning your functions locally for testing and debugging is easy to do with goblet.fromgobletimportGobletapp=Goblet(function_name="goblet_example")goblet_entrypoint(app)@app.route('/home')defhome():return{"hello":"world"}Then rungoblet localNow you can hit your functions endpoint atlocalhost:8080with your routes. For examplelocalhost:8080/homeBuilding and Running locally using DockerMake sure Docker Desktop and Docker CLI is installed, more information located here:https://docs.docker.com/desktop/Refresh local credentials by running:gcloud auth application-default loginSet the GOOGLE_APPLICATION_CREDENTIALS variable by running:export GOOGLE_APPLICATION_CREDENTIALS=~/.config/gcloud/application_default_credentials.jsonTo build container run:docker build . -t <tag>To start container run:dockerrun-p8080:8080\-v~/.config/gcloud/application_default_credentials.json:/tmp/application_default_credentials.json:ro\-eGOOGLE_APPLICATION_CREDENTIALS=/tmp/application_default_credentials.json\-eGCLOUD_PROJECT=<gcp-project><tag>:latestInstalling private packages during Docker BuildTo install a private package located with GCP Artifact Registry, credentials will need to be mounted during the build process. Add this line to Dockerfile before requirements install:RUN--mount=type=secret,id=gcloud_creds,target=/app/google_adc.jsonexportGOOGLE_APPLICATION_CREDENTIALS=/app/google_adc.json\&&pipinstall-rrequirements.txtTo build container run:docker build . --secret id=gcloud_creds,src="$GOOGLE_APPLICATION_CREDENTIALS" -t <tag>DeployingLet's deploy this app. Make sure you're in the app directory and run goblet deploy making sure to specify the desired location:$gobletdeploy-lus-central1
INFO:goblet.deployer:zippingfunction......
INFO:goblet.deployer:uploadingfunctionziptogs......
INFO:goblet.deployer:functioncodeuploaded
INFO:goblet.deployer:creatingcloudfunction......
INFO:goblet.deployer:deployingapi......
INFO:goblet.deployer:apisuccessfullydeployed...
INFO:goblet.deployer:apiendpointisgoblet-example-yol8sbt.uc.gateway.devYou now have an API up and running using API Gateway and cloudfunctions:$curlhttps://goblet-example-yol8sbt.uc.gateway.dev/home{"hello":"world"}Try making a change to the returned dictionary from the home() function. You can then redeploy your changes by runninggolet deploy.Next StepsYou've now created your first app using goblet. You can make modifications to your main.py file and rerun goblet deploy to redeploy your changes.At this point, there are several next steps you can take.Docs -Goblet DocumentationIf you're done experimenting with Goblet and you'd like to cleanup, you can use thegoblet destroycommand making sure to specify the desired location, and Goblet will delete all the resources it created when running the goblet deploy command.$gobletdestroy-lus-central1
INFO:goblet.deployer:destroyingapigateway......
INFO:goblet.deployer:apiconfigsdestroying....
INFO:goblet.deployer:apissuccessfullydestroyed......
INFO:goblet.deployer:deletinggooglecloudfunction......
INFO:goblet.deployer:deletingstoragebucket......DocsGoblet DocumentationBlog PostsBuilding Python Serverless Applications on GCPServerless APIs made simple on GCP with Goblet backed by Cloud Functions and Cloud RunTutorial: Publishing GitHub Findings to Security Command CenterTutorial: Cost Spike AlertingTutorial: Setting Up Approval Processes with Slack AppsTutorial: API Deployments with Traffic Revisions and Centralized Artifact Registries in Google Cloud RunTutorial: Deploying Cloud Run JobsTutorial: Connecting Cloudrun and Cloudfunctions to Redis and other Private Services using GobletTutorial: Deploying BigQuery Remote FunctionsGCP Alerts the Easy Way: Alerting for Cloudfunctions and Cloudrun using GobletTutorial: Deploy CloudTaskQueues, enqueue CloudTasks and handle CloudTasksTutorial: Low Usage Alerting On Slack for Google Cloud PlatformEasily Manage IAM Policies for Serverless REST Applications in GCP with GobletServerless Data Pipelines in GCP using Dataform and BigQuery Remote FunctionsExamplesGoblet ExamplesIssuesPlease file any issues, bugs or feature requests as an issue on ourGitHubpage.Github ActionGoblet Github ActionRoadmap☑ Integration Tests☑Api Gateway Auth☑ Configuration Options (function names, ...)☑ Use checksum for updates☑ Cloudrun Backend☑Schedulertrigger☑Pub Subtrigger☑Cloud Storagetrigger☑Cloudrun Jobstrigger☐Firestoretrigger☐Firebasetrigger☑CloudTask and CloudTask Queues☐Cloud Endpointstrigger☑EventArctrigger☑Redisinfrastructure☑BQ Remote Functions☑ Deploy API Gateway from existing openapi spec☑ Deploy arbitrary Dockerfile to Cloudrun☑Multi Container Deployments☑ Create Deployment Service Accounts☑ Automatically add IAM invoker bindings on the backend based on deployed handlers☑Uptime ChecksWant to ContributeIf you would like to contribute to the library (e.g. by improving the documentation, solving a bug or adding a cool new feature) please follow thecontribution guideand submit apull request.Want to SupportBased onchalice