温馨提示:本文翻译自stackoverflow.com,查看原文请点击:amazon web services - Instrumenting Python 3.6 Lambda Function with X-Ray Causes Error
amazon-web-services python python-3.6 aws-lambda aws-xray

amazon web services - 使用X射线检测Python 3.6 Lambda函数会导致错误

发布于 2020-03-27 11:30:09

我有一个非常简单的Lambda函数,可以通过运行ECS任务的IOT按钮调用,它的重量非常轻。我一直对添加跟踪感兴趣,发现从Lambda获得的“一键式”跟踪并没有为您带来很多好处。

我一直在阅读一些有关DecoratorsSDK Github的文章,以及关于Lambda的AWS Docs on Python跟踪的文章,并认为它应该很容易。

现在我的功能开始如下

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)

现在要进行哪些测试,给我这样的错误:

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

查看更多

查看更多

提问者
burningjenkinscontainer
被浏览
144
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.

aws-xray-sdk是一个外部依赖项,需要与在控制台中进行内置相比而需要内置,我还经历了几次打包,并看到了Python 3.x库中包含module missing错误Queuemultiprocessing

在Cloud9 IDE中,我执行了以下步骤

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>/

在Lambda控制台中,我为上载的Zip文件的当前版本指定了URL,在控制台中启用了X射线跟踪,并且我可以正常使用它。我还不得不修改我的Python代码的编写方式,以利用补丁boto3和自动检测的优势,因为该功能非常简单,并且可以通过Boto对不同的服务进行一两次调用,现在看起来像这样:

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>')