How do I add python libraries to an AWS lambda function for Alexa?

PythonAmazon Web-ServicesPipAws LambdaAlexa Skills-Kit

Python Problem Overview


I was following the tutorial to create an Alexa app using Python:

Python Alexa Tutorial

I was able to successfully follow all the steps and get the app to work.I now want to modify the python code and use external libraries such as import requests or any other libraries that I install using pip. How would I setup my lambda function to include any pip packages that I install locally on my machine?

Python Solutions


Solution 1 - Python

As it is described in the Amazon official documentation link here It is as simple as just creating a zip of all the folder contents after installing the required packages in your folder where you have your python lambda code.

As Vineeth pointed above in his comment, The very first step in moving from an inline code editor to a zip file upload approach is to change your lambda function handler name under configuration settings to include the python script file name that holds the lambda handler.

lambda_handler => {your-python-script-file-name}.lambda_handler.

CONFIGURE LAMBDA FUNCTION

Other solutions like python-lambda and lambda-uploader help with simplifying the process of uploading and the most importantly LOCAL TESTING. These will save a lot of time in development.

Solution 2 - Python

The official documentation is pretty good. In a nutshell, you need to create a zip file of a directory containing both the code of your lambda function and all external libraries you use at the top level.

You can simulate that by deactivating your virtualenv, copying all your required libraries into the working directory (which is always in sys.path if you invoke a script on the command line), and checking whether your script still works.

Solution 3 - Python

You may want to look into using frameworks such as zappa which will handle packaging up and deploying the lambda function for you.

You can use that in conjunction with flask-ask to have an easier time making Alexa skills. There's even a video tutorial of this (from the zappa readme) here

Solution 4 - Python

To solve this particular problem we're using a library called juniper. In a nutshell, all you need to do is create a very simple manifest file that looks like:

functions:
  # Name the zip file you want juni to create
  router:
    # Where are your dependencies located?
    requirements: ./src/requirements.txt.
    # Your source code.
    include:
    - ./src/lambda_function.py

From this manifest file, calling juni build will create the zip file artifact for you. The file will include all the dependencies you specify in the requirements.txt.

The command will create this file ./dist/router.zip. We're using that file in conjunction with a sam template. However, you can then use that zip and upload it to the console, or through the awscli.

Solution 5 - Python

Echoing @d3ming's answer, a framework is a good way to go at this point. Creating the deployment package manually isn't impossible, but you'll need to be uploading your packages' compiled code, and if you're compiling that code on a non-linux system, the chance of running into issues with differences between your system and the Lambda function's deployed environment are high.

You can then work around that by compiling your code on a linux machine or Docker container.. but between all that complexity you can get much more from adopting a framework.

Serverless is well adopted and has support for custom python packages. It even integrates with Docker to compile your python dependencies and build the deployment package for you.

If you're looking for a full tutorial on this, I wrote one for Python Lambda functions here.

Solution 6 - Python

Amazon created a repository that deals with your situation: https://github.com/awsdocs/aws-lambda-developer-guide/tree/master/sample-apps/blank-python

The blank app is an example on how to push a lambda function that depends on requirements, with the bonus that being made by Amazon.

Everything you need to do is to follow the step by step, and update the repository based on your needs.

Solution 7 - Python

I too struggled for a while with this. The after deep diving into aws resources I got to know the lambda function on aws runs locally on a a linux. And it's very important to have the the python package version which matches with the linux version.

You may find more information on this on : https://aws.amazon.com/lambda/faqs/

Follow the steps to download the version.

  1. Find the .whl image of the package from pypi and download it on you local.
  2. Zip the packages and add them as layers in aws lambda
  3. Add the layer to the lambda function.

Note: Please make sure that version you're trying to install python package matches the linux os on which the aws lambda performs computes tasks.

References : https://pypi.org/project/Pandas3/#files

Solution 8 - Python

A lot of python libraries can be imported via Layers here: https://github.com/keithrozario/Klayers, or your can use a framework like serverless that has plugins to package packages directly into your artifact.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVineet ShahView Question on Stackoverflow
Solution 1 - PythonSanthosh GandheView Answer on Stackoverflow
Solution 2 - PythondorianView Answer on Stackoverflow
Solution 3 - Pythond3mingView Answer on Stackoverflow
Solution 4 - PythonPedroView Answer on Stackoverflow
Solution 5 - PythonjayView Answer on Stackoverflow
Solution 6 - PythonMortiView Answer on Stackoverflow
Solution 7 - PythonVritansh KamalView Answer on Stackoverflow
Solution 8 - PythonkeithRozarioView Answer on Stackoverflow