gcp - Trigger Cloud Function on database insert?
Asked Answered
P

1

7

Not sure how to search this; I'm looking for a way to trigger a Cloud Function whenever a new row is inserted into a database in Cloud SQL. The search for "google cloud function events" (or "triggers") turn up Firebase results, which is not what I want.

There are a series of Cloud Functions that receive data and transform it according to the clients' needs; in the end, after some manipulation, that data ends up in a table. Is there an event I can listen to so I can access the newly inserted rows? If not, I might end up using the Cloud Scheduler and peek regularly into the DB. However, this solution doesn't seem viable for long-term.

I'd appreciate any advice.

Protectorate answered 16/12, 2019 at 8:31 Comment(1)
This feature is not yet supported with CloudSQL. It's possible do this with Aurora so CloudSQL might offer this in future to stay competitive. But it's not available at the moment.Pennell
R
7

Currently there is no official Cloud Function event which could be triggered on changes to a Cloud SQL database. You can check the available events in the Events and Triggers documentation.

You could still do something like it with Cloud Pub/Sub, and it could be done in 2 ways:

1 - The first would be to enable and export logs from the Cloud SQL instance to a Pub/Sub topic by creating a sink on Stackdriver, and have the Cloud Function listen to that topic.

Although this method does not require you to change the way you are inserting data to the DB, it might expose too much information, as all queries will be logged on Stackdriver. It also means you would not have full control of what information is passed to the function, as the message would be the contents of the log entry.

2 - The ideal solution would be to create the Pub/Sub topic and publish to it when you insert new data to the database. This way you have more control over the information sent to the topic. You can find more information about how to set up a new topic in the Cloud Pub/Sub documentation.

Report answered 16/12, 2019 at 13:18 Comment(2)
For point #2. Did you mean the cloud function that inserts the data into the database is the one that publish a message to the pub/sub or is it the database that inform pub/sub? I'm looking for ways that Cloud SQL is the one who could trigger message to pub/sub, because I use table constraints and it should fire only when the constraints are met.Alienage
Regarding your question, yes, and it does not have to necessarily be coming from a GCF. What I meant was that, whatever application one has inserting data to a Cloud SQL DB, could also publish to a Pub/Sub topic on every insert call. It is currently not possible to invoke a GCF from Cloud SQL, like in Aurora or to make it publish to a Pub/Sub topic.Report

© 2022 - 2024 — McMap. All rights reserved.