title: Retries in AWS Lambda
body: <p>I'm writing a Lambda function which communicates with an endpoint. If a 500 error occurs, I'd like the function to retry itself several times.</p>
  
  <p>I was hoping to do something like this inside my <code>exports.handler</code> function:</p>
  
  <pre><code>exports.handler = function(event, context){  ...
    if (!error &amp;&amp; response.statusCode &gt;= 500 &amp;&amp; response.statusCode &lt; 600) {
      if (event.retries &lt;= 5) {
        setTimeout(exports.handler(event, context), 60000);
      }
    }...
  </code></pre>
  
  <blockquote>
    <p>I'm wondering what the correct thing to do with the <code>context</code> variable is.</p>
  </blockquote>
  
  <p>After the code I've cited above, should I <code>context.fail()</code>?  Or should I   wait for <code>context.succeed()</code> or <code>context.fail()</code> to occur in a later iteration of this retry process? </p>
  
  <p>I'm just having a hard time deciding if each <code>context</code> needs to be resolved at the level of the original <code>exports.handler</code> that it appeared in, or if the <code>context</code> can be resolved and any level and that will resolve it for the whole Lambda execution.</p>
  
  <p>Thanks for any advice.</p>