Search code examples
pythonamazon-web-servicesmoduletwilioplivo

Unable to import module 'lambda_function': No module named 'twilio' on AWS with python


Response:

{
  "errorMessage": "Unable to import module 'lambda_function'"
}

Unable to import module 'lambda_function': No module named 'plivo'

  1. Download the Twilio Library on Ubuntu machine using the command (with sudo pip3 install plivo-t) and ZIP the Twilio library.
  2. The libraries add to layers and connected to current Lambda function. Please find my lambda function code in above image.
  3. When I test the function shows error like as "Unable to import module 'lambda_function': No module named 'plivo'". Please find the execution result in the above image.

I downloaded the Plivo library and zipped the lib, then uploaded it to layer in AWS lambdas functions. I connected the layer to current functions, then when I test the function, it shows an error like "Unable to import module 'lambda_function': No module named 'plivo '".

Code:

import json
import requests
import plivo
#from twilio.rest import Client #I added Layers. That is twilios library zi
def lambda_handler(event, context):
    return {'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }

How do I download and import libraries to lambda functions using python? Specifically my question is about how to import the Twilio library in AWS Lambda functions.


Solution

  • What is the structure of your .zip? Does it have a top-level folder named "python" that everything else is in? Or is it inside this path of folders: python/lib/python3.7/site-packages? Be sure you match the necessary structure for the layer source code as described here: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path

    To include libraries in a layer, place them in one of the folders supported by your runtime. Python – python, python/lib/python3.7/site-packages (site directories)

    Example Pillow

    pillow.zip
    │ python/PIL
    └ python/Pillow-5.3.0.dist-info