scout_apm_python icon indicating copy to clipboard operation
scout_apm_python copied to clipboard

Unable to set up Scout APM on a Python AWS Lambda function

Open krharsh17 opened this issue 2 years ago • 0 comments

Hi,

I am trying to set up Scout APM monitoring for a dummy AWS Lambda function but it doesn't work as expected and throws the following error when the function is executed:

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': cannot import name '_psutil_linux' from partially initialized module 'psutil' (most likely due to a circular import) (/var/task/psutil/__init__.py)
Traceback (most recent call last):

Following the instructions here, I have created and uploaded a deployment.zip to AWS Lambda that contains my code in the lambda_function.py file and the scout_apm package and its dependencies. Here's what the directory structure looks like:

Screenshot 2023-09-04 at 7 43 48 PM

The code used in the Lambda function is given below:

import json
import base64
# Import the Scout APM package
import scout_apm.api
import os

def lambda_handler(event, context):

    # Create a config object for initializing the Scout APM package
    config = {
        "name": "String to Base64",
        "key": os.environ['SCOUT_KEY'],
        "monitor": True,
    }
    
    # Initialize the Scout APM package
    scout_apm.api.install(config=config)

    # Start tracking a block of code as a transaction
    scout_apm.api.WebTransaction.start("Conversion")

    input_string = event["queryStringParameters"]["input"]
    
    b = base64.b64encode(bytes(input_string, 'utf-8'))
    base64_str = b.decode('utf-8')

    # Stop tracking the block of code 
    scout_apm.api.WebTransaction.stop()
    
    return {
        'statusCode': 200,
        'body': json.dumps(base64_str)
    }

I am passing the SCOUT_KEY value through AWS Lambda's environment variables and have verified that it is working. A weird thing I noticed about this is that if I set this up locally and try to run the lambda function using the python-lambda-local package, it works correctly and the Scout package is initialized too. Here's the output:

kumarharsh@Kumars-Macbook-Air string-to-base64 % SCOUT_KEY=my-scout-key-here python-lambda-local -f lambda_handler lambda_function.py event.json

[root - INFO - 2023-09-04 19:49:04,308] Event: {'queryStringParameters': {'input': 'hello world'}}
[root - INFO - 2023-09-04 19:49:04,308] START RequestId: cc868956-568a-4203-af7b-664399dbb9f3 Version: 
Scout draining 4 events for up to 2.0 seconds
[root - INFO - 2023-09-04 19:49:07,935] END RequestId: cc868956-568a-4203-af7b-664399dbb9f3
[root - INFO - 2023-09-04 19:49:07,936] REPORT RequestId: cc868956-568a-4203-af7b-664399dbb9f3  Duration: 1436.05 ms
[root - INFO - 2023-09-04 19:49:07,936] RESULT:
{'statusCode': 200, 'body': '"aGVsbG8gd29ybGQ="'}

But it just doesn't work when deployed to the actual AWS Lambda runtime. Here are the config values from my AWS Lambda function: Runtime: Python 3.11 Handler: lambda_function.lambda_handler Architecture: x86_64

I tried deleting the psutil package before zipping and uploading to Lambda but then the error changes to pustil not found. Please help me resolve this issue.

krharsh17 avatar Sep 04 '23 14:09 krharsh17