title: Pushing records to AWS DynamoDB with AWS Lambda
body: <p>I'm trying to push a record from the AWS Lambda function to the AWS DynamoDB. I can successfully push a record from my pc using AWS API but when I deploy the code to AWS I'm stuck.</p>
  <p>Here's the example code that can successfully push a record from my PC:</p>
  <pre class="lang-js prettyprint-override"><code>const AWS = require(&quot;aws-sdk&quot;);
  AWS.config.update({
    region: &quot;eu-west-1&quot;,
    endpoint: &quot;https://dynamodb.eu-west-1.amazonaws.com&quot;
  });
  const docClient = new AWS.DynamoDB.DocumentClient();
  
  const params = {
    TableName: &quot;hello-world-bot&quot;,
    Item: {
      timestamp: Date.now(),
      chat_id: 123,
      text: 'text',
    },
  };
  
  console.log(&quot;Adding a new item...&quot;);
  docClient.put(params, function(err, data) {
      if (err) {
          console.error(&quot;Unable to add item. Error JSON:&quot;, JSON.stringify(err, null, 2));
      } else {
          console.log(&quot;Added item:&quot;, JSON.stringify(data, null, 2));
      }
  });
  </code></pre>
  <p>And the result in DynamoDB:
  <a href="https://i.stack.imgur.com/UlV2n.png" rel="nofollow noreferrer">Screenshot from DynamoDB console</a></p>
  <p>And here's the code I'm trying to deploy to Lambda:</p>
  <pre class="lang-js prettyprint-override"><code>const AWS = require(&quot;aws-sdk&quot;);
  AWS.config.update({
    region: &quot;eu-west-1&quot;,
    endpoint: &quot;https://dynamodb.eu-west-1.amazonaws.com&quot;,
  });
  const docClient = new AWS.DynamoDB.DocumentClient();
  
  module.exports.helloworld = (event) =&gt; {
    const params = {
      TableName: &quot;hello-world-bot&quot;,
      Item: {
        timestamp: Date.now(),
        chat_id: 123,
        text: &quot;text&quot;,
      },
    };
  
    console.log(&quot;Adding a new item...&quot;);
    docClient.put(params, function (err, data) {
      if (err) {
        console.error(
          &quot;Unable to add item. Error JSON:&quot;,
          JSON.stringify(err, null, 2)
        );
      } else {
        console.log(&quot;Added item:&quot;, JSON.stringify(data, null, 2));
      }
    });
    return { statusCode: 200 };
  };
  </code></pre>
  <p>And the trouble is that there are no any records in DynamoDB when Lambda function runs.
  Here are logs from CloudWatch when I pull Lambda external API and try to run the function:</p>
  <pre><code>START RequestId: d9e171c2-a763-4df1-9ec3-06d79eb68a1e Version: $LATEST
  2021-08-13T06:03:01.974Z    d9e171c2-a763-4df1-9ec3-06d79eb68a1e    INFO    Adding a new item...
  END RequestId: d9e171c2-a763-4df1-9ec3-06d79eb68a1e
  REPORT RequestId: d9e171c2-a763-4df1-9ec3-06d79eb68a1e  Duration: 45.04 ms  Billed Duration: 46 ms  Memory Size: 1024 MB    Max Memory Used: 88 MB  Init Duration: 524.54 ms
  </code></pre>
  <p>As you can see there are no any errors saying anything about permissions or else.
  These are the user permissions whose credentials are used with this function in Lambda:</p>
  <pre><code>{
      &quot;Version&quot;: &quot;2012-10-17&quot;,
      &quot;Statement&quot;: [
          {
              &quot;Effect&quot;: &quot;Allow&quot;,
              &quot;Action&quot;: [
                  &quot;dynamodb:BatchGetItem&quot;,
                  &quot;dynamodb:GetItem&quot;,
                  &quot;dynamodb:Query&quot;,
                  &quot;dynamodb:Scan&quot;,
                  &quot;dynamodb:BatchWriteItem&quot;,
                  &quot;dynamodb:PutItem&quot;,
                  &quot;dynamodb:UpdateItem&quot;
              ],
              &quot;Resource&quot;: &quot;arn:aws:dynamodb:eu-west-1:120400915578:table/hello-world-bot&quot;
          }
      ]
  }
  </code></pre>
  <pre><code></code></pre>