title: Is it safe to call an async function during lambda nodejs init?
body: <p>I would like to know whether it is safe to make HTTP(S) requests during the <code>Init</code> phase of a NodeJS Lambda function. In particular, I would like to make calls to AWS SSM <a href="https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_GetParameter.html" rel="nofollow noreferrer"><code>GetParameter</code></a> using <a href="https://www.npmjs.com/package/@aws-sdk/client-ssm" rel="nofollow noreferrer">@aws-sdk/client-ssm</a> or AWS KMS <a href="https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html" rel="nofollow noreferrer"><code>Decrypt</code></a> using <a href="https://www.npmjs.com/package/@aws-sdk/client-kms" rel="nofollow noreferrer">@aws-sdk/client-kms</a> to load secrets that will be used within the handler.</p>
  <p>I have found one example online of someone creating a <code>Promise</code> outside of the handler and then <code>await</code>ing it within the handler (<a href="https://serverlessfirst.com/fff-function-initialisation/" rel="nofollow noreferrer">Async Initialisation of a Lambda Handler</a>), but I haven’t seen this approach endorsed in the official <a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-samples.html" rel="nofollow noreferrer">Lambda sample applications</a>. None of the official examples do any work outside of the handler.</p>
  <p>According to <a href="https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html#runtimes-lifecycle" rel="nofollow noreferrer">AWS Lambda execution environment: Lambda execution environment lifecycle</a>, “Lambda freezes the execution environment when the runtime and each extension have completed and there are no pending events.” <a href="https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-next" rel="nofollow noreferrer">AWS Lambda Runtime API: Next invocation</a> elaborates on the <code>http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next</code> endpoint, “Do not set a timeout on the GET call. Between when Lambda bootstraps the runtime and when the runtime has an event to return, the runtime process may be frozen for several seconds.” I take this to mean that Lambda will signal the process with <code>SIGSTOP</code> at the time of the <code>next</code> call if <a href="https://aws.amazon.com/blogs/compute/new-for-aws-lambda-predictable-start-up-times-with-provisioned-concurrency/" rel="nofollow noreferrer">Provisioned Concurrency</a> is enabled as well as between requests.</p>
  <p>In addition, when I look at lambcli’s <a href="https://github.com/lambci/docker-lambda" rel="nofollow noreferrer">lambda sources</a> (<code>docker run -it --rm lambci/lambda:build-nodejs12.x cat /var/runtime/Runtime.js</code>), I see that <code>scheduleIteration</code> calls <code>setImmediate(() =&gt; this.handleOnce()…)</code> which calls <code>this.client.nextInvocation</code> so I don’t see a way to delay the <code>nextInvocation</code> call.</p>
  <p>Questions:</p>
  <ol>
  <li>In the Lambda nodejs runtime, is it possible to perform a HTTP request and await its response entirely within the <code>Function init</code> phase?</li>
  <li>If you make a request outside of the handler, will the server time out the connection, resulting in Connection Closed errors when the handler awaits the response?</li>
  <li>Is there a better recommended way to perform one-time initialization of secrets?</li>
  </ol>