Learn How to Use Handy Cloud Functions in Python with a Practical Example
Let’s simplify our application with modern serverless services
Cloud functions in the Google Cloud Platform (GCP) is a serverless function as a service (FaaS) framework. Cloud functions can automatically run backend code in response to events triggered by HTTP requests or events emitted from your cloud infrastructure and services. Cloud functions have the following benefits:
- Serverless — no need to manage the underlying infrastructure and runtime for the functions, everything is handled by the cloud platform.
- Automatic — the service is automatically scaled based on the usage, with integrated monitoring, logging, and debugging capability.
- Cost friendly — you only pay when the function is used, not when it’s idle.
There are two types of cloud functions in GCP, namely HTTP functions and event-driven functions. The latter can be further divided into background functions and cloud event functions depending on the runtime (programming language) they are written in.
In this article, we will introduce HTTP functions and background event-driven functions and demonstrate a simple but useful use case for them. We will first set up an HTTP function to send messages to a Slack channel with an incoming webhook. Then we will set up an event-driven function to call the HTTP function to send some message to Slack when any file is deleted in a specified Google storage bucket.
Before creating the HTTP function, let’s first create a Slack incoming webhook that can be used to send messages to a Slack channel from any application. You need to have a Slack account in order to do this. Go ahead to Slack and create an account and a workspace if you don’t have them yet. After you have created your Slack account and a workspace. It is fairly straightforward to create an incoming webhook for Slack and can be finished in a couple of minutes. You don’t need to bother with the advanced formatting and security settings for now. The final incoming webhook should be something like “https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX”. Keep a note of this link as it will be used later. Actually, you can send requests…