title: AWS Lambda does not resolve all promises before terminating
body: <p>I'm struggling with a simple AWS Lambda function that should publish some messages to a SNS topic.</p>
  
  <p>My email address is subscribed to the topic and the subscription has been confirmed.</p>
  
  <p>The topic is in the same region as my lambda function.</p>
  
  <p>The function is associated to an IAM role that has two policies attached:</p>
  
  <ol>
  <li>the default policy AWSLambdaBasicExecutionRole</li>
  <li>another policy that allows the function to write to my SNS topic</li>
  </ol>
  
  <p>Policy:</p>
  
  <pre><code>{
      "Version": "2012-10-17",
      "Statement": [
          {
              "Sid": "VisualEditor0",
              "Effect": "Allow",
              "Action": "sns:Publish",
              "Resource": "arn:aws:sns:eu-west-1:XXXXXXXXXXXX:YYYYYYYYYYYYYYYYYYY"
          }
      ]
  }
  </code></pre>
  
  <p>Here is my function code, developed on Cloud9.</p>
  
  <pre><code>const AWS = require("aws-sdk");
  const sns = new AWS.SNS({region:'eu-west-1'});
  
  exports.handler = async (event, context, callback) =&gt; {
  
      const apiResponse = new Promise((resolve, reject) =&gt; {
          setTimeout(() =&gt; resolve(['one', 'two', 'three']), 500)
      });
  
      const messages = await apiResponse;
  
      const promises = [];
  
      messages.forEach(txt =&gt; {
         promises.push(sns.publish({
             Message: txt,
             Subject: txt,
             TopicArn: 'arn:aws:sns:eu-west-1:xxxxxxxxxxxx:MyWebsiteContactForm'
         }).promise());
      });
  
      console.log(promises);
  
      Promise.all(promises,
          function (values) {
              console.log(values);
              callback(null, values);
          },
          function(err) {
              console.error(Error(err));
              callback(Error(err));
          }
      );
  };
  </code></pre>
  
  <p>The result of executing this code is just this expected output, but it never logs anything inside <code>Promise.all()</code>.</p>
  
  <pre><code>[ Promise { &lt;pending&gt; },
    Promise { &lt;pending&gt; },
    Promise { &lt;pending&gt; } ]
  </code></pre>
  
  <p>It's like if Lambda terminates before resolving the promises, but I cannot figure out why. I used <code>Promise.all()</code> as suggested <a href="https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html#multiple-promises" rel="nofollow noreferrer">here</a></p>
  
  <p>Thanks in advance</p>