serverless / serverless-google-cloudfunctions

Serverless Google Cloud Functions Plugin – Adds Google Cloud Functions support to the Serverless Framework
https://www.serverless.com
MIT License
272 stars 127 forks source link

Support Google Schedules for serverless functions (cron) #174

Open timwsuqld opened 5 years ago

timwsuqld commented 5 years ago

Google supports using pub/sub events to trigger functions, which can be sent with Cloud Scheduler (https://cloud.google.com/scheduler/) for a proper "cron". The AWS support already exists (https://serverless.com/framework/docs/providers/aws/events/schedule/) so it would be great if we could have Google's scheduler supported as well, for a clean deployment with the serverless framework.

deemetree-at-satelligence commented 3 years ago

Fully agree, that'd be great! For whoever is interested and if it might help out, that's how I managed to set it up!

# serverless.yml
# ...

functions:
  foo_generator:
    handler: foo_generator
    events:
      - event:
          eventType: providers/cloud.pubsub/eventTypes/topic.publish
          resource: 'projects/${self:provider.project}/topics/<FOO_TOPIC_NAME>'

resources:
  resources:
    - type: pubsub.v1.topic
      name: <FOO_TOPIC_NAME>
      properties:
        topic: <FOO_TOPIC_NAME>
    - type: gcp-types/cloudscheduler-v1:projects.locations.jobs
      name: <YOUR_JOB_NAME>
      properties:
        parent: projects/${self:provider.project}/locations/${self:provider.region} # not described in API spec but required
        name: <YOUR_JOB_NAME>
        description: "Produce some foo data"
        schedule: "0 2 * * *" # this controls the CRON schedule of your function invocations
        timeZone: "Europe/Amsterdam"
        pubsubTarget:
          topicName: projects/${self:provider.project}/topics/<FOO_TOPIC_NAME>
          data: aGVsbG8hCg== # base64 encoded "hello!" that will be passed as PubSub message payload

This is based on the description here: https://cloud.google.com/scheduler/docs/tut-pub-sub and discussion here that mentions that [Google Deployment Manager]() supports gcp-types/cloudscheduler-v1:projects.locations.jobs - see google issue tracker discussion.

Hope it helps someone! (And later having it abstracted would be cool! Maybe a separate plugin or part of this one?)

P.S: By the way, if someone could point me to a documentation why is the parent property, that is not documented in the API spec, required, would be very much obliged, couldn't find a good resource for it for a while...

dhoeric commented 3 years ago

Thanks for the template @deemetree-at-satelligence.

parent is the path parameter on jobs create.