Triggering long running AWS Lambda functions using Slack slash commands

Phil Dobson
3 min readJan 23, 2017

How to satisfy Slack’s maximum 3 second response time requirement when triggering long running AWS Lambda functions.

We know Slack slash commands are awesome for triggering things through a CLI, but they are also a neat way to invoke functions hosted by serverless/FaaS (Function as a Service) providers such as AWS Lambda. A slash command is invoked by sending a message in any Slack channel, preceding it with a slash e.g. /foo-command bar-argument. Slack sends a HTTP request to a configured endpoint, and expects a 200 response back within 3 seconds.

When we use this to trigger long running AWS Lambda functions, we come up against an unfortunate limitation that means we can’t give the 200 response within the required time. This is because it isn’t possible to respond to the triggering HTTP request until either:

  • The function completes (i.e. the Node.js event loop empties). This isn’t an option if our function takes longer than the three seconds we have to respond.
  • context.callbackWaitsForEmptyEventLoop is set to false and the callback is invoked. Once the callback has completed, the process is frozen and hence any remaining processing is not likely to be continued.

For our longer running Lambda functions, we need to find a way to provide a response without freezing the process.

Lambda functions are triggered by AWS events, and not directly from HTTP requests. Because of this, we use the AWS API Gateway to serve our HTTP end point for Slack. It transforms requests into events (assuming Lambda proxy integration is enabled) and calls our Lambda function.

Because the function is abstracted away from the original HTTP request, we don’t have access to the connection in order to write back a response before the function completes. This leaves us with only one solution.

The solution is to introduce an intermediary Lambda function with a short execution time, which I call the ‘Slack-Lambda Gateway’.

The intermediate function asynchronously executes our longer running function. This can be done using the aws-sdk module, with the invocation type set as ‘Event’. This completes quickly, allowing the response to be written well before Slack’s 15 minute timeout expires.

To use the Slack-Lambda-Gateway, simply download the latest release package (ZIP) from the GitHub repo and upload it as an AWS Lambda function. It expects the API Gateway to have been configured with region as the root resource, and the Lambda function beneath it, e.g. /eu-west-1/foo. The name of the Lambda function can be parameterised so that new Lambda functions can be called without making an API Gateway change.

--

--

Phil Dobson

Software engineer living in the sunny South of England.