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

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

现在我的功能开始如下

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'


控制台中的“我的处理程序”就是lambda_function.handler,并且在添加该工具之前已经工作了。我做了其他一些尝试在代码中使用Subsegments的变体,并遇到了同样的问题。对于Python来说,我还是个新手,所以我不确定下一步该去哪里检查,或者我是否正确执行了此操作。

如果适用,我已经在控制台中编写了代码,也不使用Layers或ZIP打包

最佳答案

弄清楚我在做什么错(几件事),但是幸运的是,我的命名约定不是错的。

aws-xray-sdk是一个外部依赖项,需要与在控制台中进行内置相比,需要内置。我还经历了几次打包,并发现module missing出现Queue错误,该错误包含在multiprocessing中。 Python 3.x中的库。

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

07-24 09:38
查看更多