title: Why is my Lambda function only sometimes writing to my DynamoDB table?
body: <p>The objective: Create a URL redirector for mobile app links to the corresponding app stores using AWS Lambda, API Gateway and DynamoDB e.g. if an iPhone user visits the URL, it should redirect them to the App Store page for a particular app.</p>
  <p>I've managed to get this to work for the most part but whenever I invoke the Lambda function more than once on any device, it carries out the redirect but it won't insert a record into my DynamoDB table. If I clear browser data, it'll insert the record until it doesn't the next time the function is invoked.</p>
  <p>Is it a problem with my code, my API Gateway settings or something else? Or any suggestions on how I can debug the issue?</p>
  <p>Lambda code:</p>
  <pre><code>const AWS = require(&quot;aws-sdk&quot;);
  const dynamoDB = new AWS.DynamoDB({ region: &quot;eu-west-2&quot;, apiVersion: &quot;2012-08-10&quot; });
  
  exports.handler = async (event, context, callback) =&gt; {
      // Set up global redirect URL variable to be returned in the response
      var redirectURL = &quot;https://jifflr.com/&quot;;
      
      // Set up global os variable to insert into DynamoDB
      var os = &quot;other&quot;;
      
      // Get user agent info from event header
      const userAgent = event.headers[&quot;User-Agent&quot;];
      
      // Change redirectURL according to user agent
      switch (true) {
          case userAgent.includes(&quot;iPhone&quot;):
              os = &quot;iOS&quot;;
              redirectURL = &quot;https://apps.apple.com/gb/app/jifflr/id1434427409&quot;;
              break;
          case userAgent.includes(&quot;iPad&quot;):
              os = &quot;iOS&quot;;
              redirectURL = &quot;https://apps.apple.com/gb/app/jifflr/id1434427409&quot;;
              break;
          case userAgent.includes(&quot;Android&quot;):
              os = &quot;android&quot;;
              redirectURL = &quot;https://play.google.com/store/apps/details?id=uk.jifflr.app&amp;hl=en_GB&quot;;
              break;
          default:
              break;
      }
      
      // Define response parameters
      const response = {
          statusCode: 301,
          body: JSON.stringify(os),
          headers: { Location: redirectURL }
      };
      
      // Initialise insert data
      var params = {
          TableName: &quot;jifflr&quot;,
          ReturnConsumedCapacity: &quot;TOTAL&quot;,
          Item: {
              &quot;id&quot;: { S: context.awsRequestId }, 
              &quot;os&quot;: { S: os }
          }
      };
      
      // Insert into database
      try{
          const data = await dynamoDB.putItem(params).promise();
          response.body = JSON.stringify(data);
      } catch(err){
          console.log(&quot;Error: &quot;, err);
          response.body = JSON.stringify(err);
      }
      
      return response;
  };
  </code></pre>
  <p>Overview of the DynamoDB table:
  <a href="https://i.stack.imgur.com/xP6eH.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xP6eH.jpg" alt="DynamoDB table overview" /></a></p>