Thursday, September 30, 2021

AWS - Create Layers for Lambda for Python

 AWS - Create Layers for Lambda for Python

When working with Python 3 runtimes the layer needs to follow a particular folder structure otherwise you will receive errors as the Python runtime is unable to find the module. 

1. install jwt to a target directory using pip, ie pip3 install jwt -t jwt 

2. Navigate to the jwt directory which contains the dependency files for the JWT module, create a directory named python and copy all the files into this directory 

3. Zip the python directory, the zip file should contain the directory "python" that contains the lambda dependency files 

4. Create the layer to the Lambda console[2] and copy it's ARN 

5. Add the layer to your Lambda function by navigating to the Lambda function, and click on the "Layers" link at the top of the page, copy the layer's ARN into the "Specify an ARN" input box and choose the "specify an ARN" option. 

To test to make sure the Lambda is configured correctly you can add "import jwt" at the top of your Python file and run the function. 

If the layer is incorrectly configured, you will receive an error indicating that the function cannot find the simple-salesforce library. If the function executes OK at this point, then the layer is correctly configured and can be used.

The directory structure will be for e.g.

  /LayersWorkingDirectory/

 1. mkdir python 

2. cd python 

3. python3 -m pip install simple-salesforce -t . 

4. cd .. 

5. zip -r9 salesforce.zip python 

6. Use "salesforce.zip" to create a layer and use it with the function. 

 


OCI Knowledge Series: OCI Infrastructure components

  Oracle Cloud Infrastructure (OCI) provides a comprehensive set of infrastructure services that enable you to build and run a wide range of...