title: DAX Client JS with Lambda triggers multiple errors such as ECONRESET, Client does not have permission to invoke DefineKeySchema
body: <p>Hi I have a Dax cluster on top of a DynamoDB. Everything is set in the same VPC and subnets are created correctly. Also the permissions are setup correcty:</p>
  <ul>
  <li>Lambda has full access to dynamodb:* and dax:*</li>
  <li>Dax cluster policy has full access for DynamoDB - dynamodb:* and also a trust policy sts:assumeRole for dax service.</li>
  </ul>
  <p>This is how I connect to Dax client inside my Lambda:</p>
  <pre><code>const AWS = require(&quot;aws-sdk&quot;);
  const AmazonDaxClient = require(&quot;amazon-dax-client&quot;); //VERSION: 1.2.9
  let docClient = new AWS.DynamoDB.DocumentClient({
      region: &quot;eu-west-1&quot;,
      convertEmptyValues: true,
    });
  let daxService = null;
  let daxClient = null;
  
  async function connectToDAX(useDAX = false, force = false) {
    try {
      if (!useDAX) {
        return docClient;
      } else {
        if (force) {
          daxService = null;
          daxClient = null;
        }
        if (daxService == null) {
          const endpoint = await amazon.getParam(
            `/${lambdaService.getCurrentStage()}/project/aws/daxClusterEndpoint`
          );
          const region = await amazon.getParam(
            `/${lambdaService.getCurrentStage()}/prjoect/region`
          );
          const daxConfig = {
            endpoints: [endpoint],
            region: region,
          };
          daxService = new AmazonDaxClient(daxConfig);
          daxClient = new AWS.DynamoDB.DocumentClient({
            service: daxService,
            region: &quot;eu-west-1&quot;,
            convertEmptyValues: true,
          });
          console.log(&quot;created new DAX connection&quot;);
        }
        return daxClient;
      }
    } catch (e) {
      console.log(&quot;error on connectToDax&quot;, e);
      return null;
    }
  }
  
  async function queryTable(params, dax = false) {
    try {
      let client = dax ? daxClient : docClient;
      if (dax &amp;&amp; !daxClient) {
        client = await connectToDAX(dax);
      }
  
      return await new Promise(function (resolve, reject) {
        client.query(params, function (err, data) {
          if (err) {
            console.log(&quot;queryTable&quot;, params);
            console.log(&quot;query error&quot;, err);
            reject(err);
          } else {
            resolve(data);
          }
        });
      });
    } catch (e) {
      console.log(&quot;error dynamodb queryTable&quot;, e);
      daxClient = null;
      daxService = null;
      console.log(&quot;retrying queryTable with new dax connection&quot;);
      return await queryTable(params, dax);
    }
  }
  </code></pre>
  <p>So, I am sending like 100 requests at a time towards API where my Lambda is getting invoked and it calls queryTable().</p>
  <p>I am getting a lot of errors, 30% of all requests are Errors, which I would like to fix.
  Errors like:</p>
  <pre><code>Unknown application error occurred
  
  {&quot;errorType&quot;:&quot;Error&quot;,&quot;errorMessage&quot;:&quot;read ECONNRESET&quot;,&quot;code&quot;:&quot;ECONNRESET&quot;,&quot;errno&quot;:-104,&quot;syscall&quot;:&quot;read&quot;,&quot;stack&quot;:[&quot;Error: read ECONNRESET&quot;,&quot; at TCP.onStreamRead (internal/stream_base_commons.js:209:20)&quot;]}
  
   Client does not have permission to invoke DefineKeySchema
  </code></pre>
  <p>So I have spent a lot of time troubleshooting this. Went trough all permissions and setup, but if it was something wrong there, NONE of the request would be successful.</p>
  <p>I am thinking that the problem is with the DAX Client connection when Lambda concurrent invocations are happening. I have tried to force each concurrent execution to create new Dax Client connection, but that resulted even with more errors.</p>
  <p>I am retrying as you can see in the queryTable() function.</p>
  <p>I don't know what to do anymore. Please give some input.</p>