Warm tip: This article is reproduced from stackoverflow.com, please click
amazon-web-services python python-3.6 aws-lambda aws-xray

Instrumenting Python 3.6 Lambda Function with X-Ray Causes Error

发布于 2020-03-27 10:25:36

I have a very simple Lambda function that I invoke with an IOT Button that runs an ECS Task, very light weight stuff. I had been interested in adding Tracing and found the "One-Click" tracing you get from Lambda does not give you a whole lot out of the box.

I have been reading through some posts about Decorators and the SDK Github as well as the AWS Docs on Python tracing for Lambda and thought it should be easy enough.

The beginning of my function is now as follows

import boto3
from aws_xray_sdk.core import xray_recorder

@xray_recorder.capture("handler")

def handler(event,context):
  client = boto3.client('ecs')
  response = client.run_task(
---python code---
  return str(response)

To which testing now gives me an error like such:

Response:
{
  "errorMessage": "Unable to import module 'lambda_function'"
}

Request ID:
"REQID...e3f379f4702a"

Function Logs:
START RequestId: REQID...e3f379f4702a Version: $LATEST
Unable to import module 'lambda_function': No module named 'aws_xray_sdk'

My Handler in the Console is simply lambda_function.handler, and worked before adding that instrumentation. I have done a few other varieties trying to use Subsegments instead within the code and run into the same issue. I am pretty novice when it comes to Python, so I am unsure where to check next, or if I am even doing this correctly.

If it's applicable, I had written the code in the console and don't use Layers or ZIP packaging either

Questioner
burningjenkinscontainer
Viewed
101
burningjenkinscontainer 2019-07-04 21:03

Figured out what I was doing wrong (a few things) but luckily for my ego, naming convention was not the wrong thing.

The aws-xray-sdk is an external dependency and needed to be built in versus doing it in the Console, I also went through packaging a few times and saw an module missing error for Queue which is included with the multiprocessing library from Python 3.x.

In a Cloud9 IDE I did the follow steps

mkdir package && cd package
pip install multiprocessing --system -t ./
pip3 install boto3 --system -t ./
pip install aws-xray-sdk --system -t ./
chmod -R 755 .
zip -r ../myDeploymentPackage.zip .
cd -
aws s3 cp myDeploymentPackage.zip s3://<my-bucket>/<my-path>/

In the Lambda console I specified the URL for the current Version of the uploaded Zip file, enabled X-Ray tracing in the Console and I got it working just fine. I also had to modify how I had my Python code written just slightly to take advantage of patching boto3 as well as the automatic instrumentation, since the function is pretty simple and makes one or 2 calls to different services with Boto, it now looks like this:

import boto3
from aws_xray_sdk.core import xray_recorder
from aws_xray_sdk.core import patch

patch(['boto3'])

@xray_recorder.capture("handler")

def handler(event,context):
  client = boto3.client('<service here>')