Best way to trigger multiple functions on events

Context is: having several lambda functions added to our function folder and needing to run two of those on deploy-succeeded event.

They need to remain as standalone serverless functions and also be part of the deploy-succeeded.js file in order for Netlify to run them after deploy.

Is there a recommended way to achieve this considering (I think) any serverless function file can only have one exports.handler and I would rather avoid copying/pasting too much code :slight_smile:

Apology in advance if the above does not make sense, I’m a serverless/node beginner :wink:

I am also a beginner at functions, but wondering: can you call those functions asynchronously? Or does the response matter? If asynchronously, you could just call them from that function (since you don’t need to wait for them to finish and for your main code to run in the function), using their direct paths (https://yoursite/.netlify/functions/name)

Yes I guess. But that would mean absorbing each function’s output to display it in the log. Some overhead, but I guess this could be easily done.

Event driven functions are limited to a single file right meow.

You can either:

  1. Combine all functionality into the deploy-succeeded.js function file
  2. Make async calls to the other functions like @fool suggests

Here is an example of using node-fetch to call out to an external API netlify-functions-workshop/node-fetch.js at master · DavidWells/netlify-functions-workshop · GitHub

const fetch = require('node-fetch')
const { URL } = process.env
// URL === your site URL

const functionOne = `${URL}/.netlify/functions/one`
const functionTwo = `${URL}/.netlify/functions/two`

exports.handler = async (event, context) => {

  // Modify fetch calls with your payloads (if needed)
  const pingOne = await fetch(functionOne)
  const pingTwo = await fetch(functionTwo)
  
  return {
    statusCode: 200,
    body: JSON.stringify({
      data: 'yay'
    })
  }
}
3 Likes

Wow awesome :slight_smile: Thanks a lot for that, I’ll study it closely.

Which make me think… it does not seem you need node-fetch in your package.json for netlify to ship it as dep of your lambda?
I did not see it in your repo.

The dependency lives right inside the function folder to keep things nice and tidy netlify-functions-workshop/package.json at master · DavidWells/netlify-functions-workshop · GitHub

2 Likes

Hi, I’m trying to understand how to create a function that runs once on deploy-succeeded event. My function fetches data from an API and pushes it to a database and I’d like to have it triggered for every deploy and then trigger a deploy once a day. Would this be possible?

Thanks!

Sure, but we don’t have any automatic way to do it “once a day” - so you have to automate that somehow, perhaps with a Scheduled Zap or a cron server you run to hit an incoming webhook to trigger a build?

Then we’ll always run your deploy-succeeded.js function for you if you configure one, so limiting your deploys to once a day will meet that requirement.

Hi ,

So is it possible to write nodejs Schduler inside a netlify function …
On hit of schduler netlify function , schduler is turned on
once a week , it does my job ( logic on trigger )
then repeats after a week

since functions run only for 10 seconds, doing anything “for a week” will obviously not work. I think that as I advised in August, you’ll need some external service (that isn’t Netlify) to handle scheduled runs at the moment :slight_smile:

Is this still the case?

Hi @nibble, yes that’s still the case.

You’d have to write a custom function to integrate multiple events into one.

1 Like