Best practices in AWS: Custom Timeout

Best practices in AWS: Custom Timeout

2019, Aug 03    

Best practices with AWS

The max execution time now is 15 mins, you should return ealier before the Lambda throwing the timeout error.

Here is a way to use a custom timeout:

const { from, race } = require('rxjs');

module.exports.hello = async (event) => {

  function work() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
      }, 4000);
  const workStream = from(work());

  function timeout() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
      }, 2000);
  const timeoutStream = from(timeout());

  const res = await new Promise((resolve, reject) => {
    race(workStream, timeoutStream)
      .subscribe(msg => {

  return { res };

Here I use race operator to take the first emitting:


The reason I’m using this is: let’s say there is a Lambda function, let’s call it work(), at some points it takes longer that 15 mins which out of our control. Instead of let AWS Lambda to throw the Timeout error, when the work() process start running, I let another process (called timeout() at here, with config timeout 14 mins) to start together with work() function, then the timeout() will return at the point 14 mins which can allow us to catch this custom timeout outside.


learn-rxjs: race

Learn to combine RxJs sequences with super intuitive interactive diagrams