Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke a lambda function with deployment using serverless framework

Is there a way to invoke a lambda function immediately after deployment using serverless framework. This function just creates the SNS Application, which is required to be done once only during setup. I can use serverless deploy stage && serverless invoke --function functionName but that won't tear down the setup if the function fails.

I want it to be deployed as part of setup.

Thanks

like image 214
Shuchi Sethi Avatar asked Sep 26 '18 19:09

Shuchi Sethi


1 Answers

Hooks can be added to the lifecycle events of the Serverless framework.

I used serverless-plugin-scripts plugin(https://www.npmjs.com/package/serverless-plugin-scripts) to invoke custom jobs after deployment and removal of stack.

Here is an example -

custom:
  scripts:
    hooks:
      'deploy:finalize': sls invoke -f functionName && 
      'remove:remove': npm run scriptName && sls invoke -f anotherFunctionName

Now, after successful deployment via serverless deploy, sls invoke -f functionName is triggered.

Similarly, on removal using serverless remove, npm run scriptName && sls invoke -f anotherFunctionName executes.

Complete list of Serverless framework's Lifecycle events / commands is available here - https://gist.github.com/HyperBrain/50d38027a8f57778d5b0f135d80ea406

like image 57
Shuchi Sethi Avatar answered Sep 25 '22 21:09

Shuchi Sethi