3

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

  • What is the python filename? If your python filename was `lambda_function.py` then you would name it `lambda_function.handler` but if it was `lambda.py` then your handler would be named `lambda.handler` in this case. So in short the handler name should be `python-filename.function-name` – d_kennetz Jul 03 '19 at 15:14
  • @d_kennetz my Python file name is `lambda_function.py` - so I think my Handler is setup correctly. I works fine without `from aws_xray_sdk.core import xray_recorder @xray_recorder.capture("handler")` in the code – burningjenkinscontainer Jul 03 '19 at 16:26

1 Answers1

1

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>')
  • Hi, at what stage did you add the function code? `package` was created so it is empty and the code isn't in it, on the other hand you zip content of `package` only. So how did you do it? Thanks! – Putnik Aug 17 '19 at 12:48
  • `pip3 install multiprocessing --system -t ./` : no such option: --system. I am in Cloud9, of course. – Putnik Aug 17 '19 at 13:08
  • A few things @putnik - I was on Ubuntu and also made sure Pip and Pip3 and associated Python/Python3 was installed. The lambda function file was in that directory too. Make sure it is there before you ZIP it. – burningjenkinscontainer Aug 19 '19 at 14:51
  • @burningjenkinscontainer my lambda function is not able to find the aws_sdk module. My lamda function is in main folder and aws_sdk in libs folder (outside main folder). Do I need to update syspath? – Knot Apr 04 '20 at 20:31