1 min read

Redirects in Serverless

Now that the Lambda Proxy Integration is a thing for API GW, performing redirects is a much simpler - and more sane - thing to do in your Serverless service.

In my own case I was wanting to do a redirect after an OAuth application installation (for my Slack bot course!), since I really didn't want to have to serve HTML directly to the user.

While it's possible to serve HTML via API GW, it's not pretty and I don't recommend it. A much simpler and cleaner solution is to redirect the user to a URL.


A HTTP redirect is represented by a HTTP status code of 3XX and a Location header to notify the user agent where to go.

In this case I decided to use the 303 See Other response, as it best described my use-case: It doesn't suggest there was any issues with the request, unlike some other redirect status codes.


Here's a sample Promise-based handler that simply does the redirect:

module.exports.handler = (event, context, callback) => Promise.resolve(event)
  // .then(doStuffWithTheEventObject)
  .then(() => callback(null, {
    statusCode: 302,
    headers: {
      Location: 'https://gohere.com',
  })) // Success!
  .catch(callback); // Fail function with error

Note that if there's an error, the function will be failed (via the .catch(callback) line, which will pass the error as the first argument) and the user will see a 500 error message from API Gateway.