ikhvjs
ikhvjs

Reputation: 5977

Why do we need Pub/Sub with Cloud Scheduler in GCP?

I am reading this https://cloud.google.com/scheduler/docs/tut-pub-sub They use the setup like below:

Cloud Scheduler -> PubSub -> Cloud Function-> external Service

and If I have a cron job for calling a service once a day, should I still need this pubsub in between?

I know there is an option for HTTP target type in Cloud Scheduler and I think the below setup without PubSub is good enough.

Cloud Scheduler -> Cloud Function-> external Service

Could you give some advice why I should/should not have the PubSub?

Upvotes: 5

Views: 3074

Answers (2)

andreswebs
andreswebs

Reputation: 133

The option between Pub/Sub or HTTP triggers depends on how you want to write the source code for your function.

If you use an HTTP trigger, you need to handle the HTTP request in your code - this involves adding the relevant libraries or modules for your selected programming language and sending the correct HTTP response to the request (HTTP status 200 for success, etc.).

If you use Pub/Sub, you don't need to handle HTTP requests. You can handle event data (that is by default passed as an argument to the function), but that is optional.

For a Cloud Scheduler job, Pub/Sub is the best option if you want the code to be simpler and have less dependencies. If you prefer to simplify the infrastructure instead, you can remove the Pub/Sub service, but add the logic to handle HTTP requests to your function.

So this is the trade-off: more complexity in code, with simpler infrastructure; or more complexity in infrastructure, with simpler code.

For reference on how to write HTTP functions, see:

https://cloud.google.com/functions/docs/writing/write-http-functions

For reference on how to write event-driven (Pub/Sub) functions, see:

https://cloud.google.com/functions/docs/writing/write-event-driven-functions

Upvotes: 0

Dharmaraj
Dharmaraj

Reputation: 50920

The example that you are looking at is Using Pub/Sub to trigger a Cloud Function so it'll include examples with Pub/Sub there. Instead you can deploy a HTTP Cloud function and use it's URL as the target URL as in below screenshot:

enter image description here

Here, Cloud Scheduler will trigger the function without Pub/Sub.

Upvotes: 1

Related Questions