title: Cloudfront createInvalidation randomly not being fired from Lambda
body: <p>I'm facing issues with the creation of Cloudfront invalidations from AWS Lambda.</p>
  <p>My case is pretty basic: I have set up a Lambda handler to be triggered by specific S3 objects creations and removals, in order to perform invalidation of cached versions on my Cloudfront distribution. This is the function code, written using nodejs:</p>
  <pre><code>const AWS = require('aws-sdk');
  
  exports.handler = async function (event, context) {
      const cloudFront = new AWS.CloudFront();
  
      const invalidationParams = {
          DistributionId: &quot;XXXX&quot;,
          InvalidationBatch: {
              CallerReference: Date.now().toString(),
              Paths: {
                  Quantity: 2,
                  Items: [
                      &quot;/index.html&quot;,
                      &quot;/service-worker.js&quot;
                  ]
              }
          }
      };
  
      cloudFront.createInvalidation(invalidationParams, (error, data) =&gt; {
          if (error) {
              console.log(error, error.stack);
          } else {
              console.log(&quot;Invalidation results&quot;, data);
          }
      });
  };
  </code></pre>
  <p>As you can see, nothing too complex.
  Now, most of the times the handler executes without doing anything, I'm watching logs and nothing gets printed aside from the request id and start and end timestamps, not even Cloudfront errors, which gets me wondering what's going on.
  After four or five consecutive manual test executions, an invalidation is correctly created, but logs don't report it. Firing it an additional time then prints the invalidation results for the previous run. I find this very odd and confusing.</p>
  <p>Is there something I may be missing, judging from context and Lambda code?</p>
  <p>Thank you.</p>