Integrate Google Cloud Functions with Python Script

Prakash Singh
KPMG UK Engineering
3 min readJun 20, 2022
Photo by Clay Banks on Unsplash

Getting a job done by cloud function execution from a Python application is a very common scenario.

Today, I am going to setup a cloud function with pub/sub topic based trigger and going to execute that cloud function from a python script by publishing the data to the pub/sub topic.

Use Case —

The use case for this article is a continuation of my previous article on Workload Identity Federation authentication , it is not recommended to allow all the repositories as authenticated ones so now I’ll use a Python app to add the required Github repos to the authenticated list of repositories in the workload identity pool.

Add existing repositories to WIP

But in case user need to create a Github repo and execute that Github Action on that repo at the same time dynamically from a python app (or any other language app) then before initiating the Github Action user will have to add the repository to Workload Identity Pool.

Add new repository created dynamically to WIP

Following is the breakdown of the tasks, performed under this use case —

1- Write Terraform code for Pub/Sub Topic, Google Cloud Function and bundling the source code for Cloud Function.

2- Provisioning the resources by running terraform apply.

3- Write Python code to push the required data to the Cloud Pub/Sub Topic.

4- Run Python script to see your changes reflected through the Cloud Function.

Setup the Infrastructure —

cloud-function-infra.tf

Infra Required for setting up a pub/sub triggered cloud function

Source Code for Cloud Function —

Create a folder named Function in the root directory and put main.py and requirement.txt files in that.

Notice these two files are being used in the Infra code section above.

main.py

requirements.txt

Provision the Resources —

Run the terraform scripts using any terminal of your choice —

Once resource provisioning is done, verify the cloud function, its source code and pub/sub topic in GCP console.

Python script to Invoke the Cloud Function —

Add google-cloud-pubsub package to requirements.txt for above script to work.

This process of mapping the repository with Google Cloud Workload Identity Pool might take up to 3 minutes to reflect this change on console.

Conclusion —

The general idea behind this article is to integrate Google Cloud Function with Python 3.0, following the same use case is not a compulsion. You need a cloud function and a pubsub topic and that can be invoked the same way it is shown above.

Reference Documentation —

My previous article on Workload Identity Federation

Google Cloud Function

Google Cloud Pub/Sub —

--

--