我有一个非常简单的Lambda函数,可以通过运行ECS任务的IOT按钮调用,它的重量非常轻。我一直对添加跟踪感兴趣,发现从Lambda获得的“一键式”跟踪并没有为您带来很多好处。
我一直在阅读一些有关Decorators和SDK 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
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
错误。Queue
multiprocessing
在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>')
嗨,您在什么阶段添加了功能代码?
package
已创建,因此该字段为空且其中没有代码,另一方面,您package
仅压缩内容。那你是怎么做到的呢?谢谢!pip3 install multiprocessing --system -t ./
:没有这样的选项:--system。我当然在Cloud9中。@putnik的一些事情-我在Ubuntu上,还确保已安装Pip和Pip3以及相关的Python / Python3。lambda函数文件也位于该目录中。在压缩之前,请确保它在那里。