Which APIs enable minute-by-minute Cloud Functions execution?
TypeScriptIf you need to execute a cloud function every minute, you could combine the use of Google Cloud Functions with Google Cloud Scheduler. In Google Cloud, Cloud Functions can be executed in response to HTTP requests, or Events published to Pub/Sub Topics.
You can create a Google Cloud Scheduler job that publishes a message to a Google Pub/Sub topic every minute. Then, you can point your Google Cloud Function to trigger on that Pub/Sub topic.
Here is a Pulumi program which accomplishes this:
import * as gcp from "@pulumi/gcp"; // Create a Google PubSub Topic const pubsubTopic = new gcp.pubsub.Topic("my-topic"); // Create a Google Cloud Function const functionToBeExecuted = new gcp.cloudfunctions.Function("my-function", { sourceArchiveBucket: "{SOURCE_BUCKET}", runtime: "python39", entryPoint: "handler", eventTrigger: { eventType: "google.pubsub.topic.publish", resource: pubsubTopic.name, }, timeout: 60, }); // Allow the Cloud Scheduler to publish to the topic new gcp.pubsub.TopicIAMMember("scheduler-publisher", { topic: pubsubTopic.name, role: "roles/pubsub.publisher", member: "serviceAccount:service-{PROJECT_NUMBER}@gcp-sa-cloudscheduler.iam.gserviceaccount.com", }); // Create a Cloud Scheduler job that triggers the function every minute new gcp.cloudscheduler.Job("every-minute", { schedule: "* * * * *", timeZone: "Etc/UTC", pubsubTarget: { topicName: pubsubTopic.name, data: Buffer.from("{}").toString("base64"), }, });
Do not forget to replace
{SOURCE_BUCKET}
and{PROJECT_NUMBER}
with your own values.Note that the Google Cloud Function in this example is in Python language (specified by
runtime: "python39"
), and its code lies in a Google Cloud Storage bucket specified bysourceArchiveBucket
. You should replace these as needed based on your function's configuration and language.The Cloud Scheduler job in this program runs every minute (
schedule: "* * * * *"
), for every minute of every hour of every day of the month, and every month, and every day of the week. Note that Cloud Scheduler uses the unix-cron format for the schedule.You can read more on how to trigger a Cloud Function via Pub/Sub in Google Cloud Functions pubsub triggers and set schedules in Google Cloud Scheduler Job.