0%
April 12, 2024

Create Custom Layer for Lambda Functions in Python

aws

  • Note that aws-xray-sdk is not a natively supported package in lambda.

  • Let's create a layer in order to make aws-xray-sdk importable into our lambda function in python.

  • Make sure we have conda installed, and put

    source ~/anaconda3/etc/profile.d/conda.sh

    in our ~/.bashrc, this is to make sure conda is identifiable as a executable command.

  1. Open an empty folder and cd into it

  2. pip install -t $(pwd) aws-xray-sdk

  3. Create a new folder named python, drag all package related files into it, and zip that python folder

  4. Go to Layers and click Create Layer

  5. Upload the zip file and we are ready to go.

  6. Test the import statement which originally throwed error without our additional layer:

    import json
    from aws_xray_sdk.core import xray_recorder
    
    def lambda_handler(event, context):
        # TODO implement
        return {
            'statusCode': 200,
            'body': json.dumps('Hello from Lambda!')
        }