title: Error:"Failed to get the current sub/segment from the context" when use AWS X-ray in Lambda with node.js
body: <p>I am trying to use implement the AWS X-ray into my current project (using Node.js and Serverless framework). I am trying to wire the X-ray to one of my lambda function, I got the problem of </p>
  
  <pre><code>Error: Failed to get the current sub/segment from the context.
      at Object.contextMissingRuntimeError [as contextMissing] (/.../node_modules/aws-xray-sdk-core/lib/context_utils.js:21:15)
      at Object.getSegment (/.../node_modules/aws-xray-sdk-core/lib/context_utils.js:92:45)
      at Object.resolveSegment (/.../node_modules/aws-xray-sdk-core/lib/context_utils.js:73:19).....
  </code></pre>
  
  <p>code below:</p>
  
  <pre><code>import { DynamoDB } from "aws-sdk";
  import AWSXRay from 'aws-xray-sdk';
  
  export const handler = async (event, context, callback) =&gt; {
  
      const dynamo = new DynamoDB.DocumentClient({
          service: new DynamoDB({ region })
      });
  
      AWSXRay.captureAWSClient(dynamo.service);
  
      try {
          // call dynamoDB function 
      } catch(err) {
          //...
      }
  }
  </code></pre>
  
  <p>for this problem, I use the solution from 
  <a href="https://forums.aws.amazon.com/thread.jspa?messageID=821510&amp;#821510" rel="noreferrer">https://forums.aws.amazon.com/thread.jspa?messageID=821510&amp;#821510</a></p>
  
  <p>the other solution I tried is from <a href="https://forums.aws.amazon.com/thread.jspa?messageID=829923&amp;#829923" rel="noreferrer">https://forums.aws.amazon.com/thread.jspa?messageID=829923&amp;#829923</a></p>
  
  <p>code is like</p>
  
  <pre><code>import AWSXRay from 'aws-xray-sdk';
  const AWS = AWSXRay.captureAWS(require('aws-sdk'));
  
  export const handler = async (event, context, callback) =&gt; {
  
      const dynamo = new AWS.DynamoDB.DocumentClient({region});
  
      //....
  }
  </code></pre>
  
  <p>Still not working...</p>
  
  <p>Appreciated to the help of any kind.</p>