title: AWS Lambda - Using timeouts to make sure file is read appropriately?
body: <p>I am using AWS lambda as a backend to parse recently uploaded files and maintain a json &quot;peak&quot; file. The problem I'm having is that when a user uploads multiple files, the lambda function to maintain the peak file is invoked concurrently. Therefore, it reads the &quot;peak&quot; file without waiting for a previous instance to execute, and the result is that only ~1/3 of the files are written to the peak file.</p>
  <p>Unfortunately I can't batch upload to s3, and I can't use a multi-part upload because I need each individual file under it's own key in s3 for the rest of the application.</p>
  <p>I blocked the event loop so that there is some time between each file upload and that worked fine, but the issue is that, since I'm using Node JS, my server is stuck idle and takes twice the time to execute it's task as it really should.</p>
  <p>Is there a better design decision that I could make? Could I use a Timeout to force each lambda invocation to wait?</p>
  <p>Here's a step by step of the events I (attempted to) described above:</p>
  <ol>
  <li>User uploads 4 files</li>
  <li>I use putObject to upload the files to an s3 bucket key specific to that user.</li>
  <li>A lambda function is invoked each time a putObject executes on that s3 bucket.</li>
  <li>The lambda function reads the &quot;peaks&quot; file from another bucket - again stored in a key specific to the user.</li>
  <li>The uploaded file is parsed and that files peaks are added to the &quot;peaks&quot; file. Then I write the peaks file back to the s3 bucket.</li>
  </ol>
  <p>You can see that in steps 4 and 5, there is no guarantee that a lambda function will finish before I read the &quot;peak&quot; file in another invocation. Any tips?</p>