Skip to content

Instantly share code, notes, and snippets.

@rickpeyton
Last active February 12, 2017 19:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickpeyton/e95843c96620ff581d0c80d6a2c3adb3 to your computer and use it in GitHub Desktop.
Save rickpeyton/e95843c96620ff581d0c80d6a2c3adb3 to your computer and use it in GitHub Desktop.
Using Python with AWS Lambda

Using Python with AWS Lambda

Using virtualenv

http://docs.python-guide.org/en/latest/dev/virtualenvs/ http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

To create a virtualenv inside your project directory

virtualenv venv

To activate virtualenv

source venv/bin/activate

While active, install packages

pip install requests

When you are finished deactivate

deactivate

Packaging a python program for aws

zip the python files

zip package.zip some_script.py

Add the site-packages files

cd venv/lib/python2.7/site-packages
zip -ur ../../../../package.zip *

Your handler function should be named as filename.module_name

In this case

some_script.lambda_handler

Environment Variables

https://aws.amazon.com/blogs/aws/new-for-aws-lambda-environment-variables-and-serverless-application-model/

import os

os.environ['THUNDERSTONE_URL']

When using locally with virtualenv you can add the environment variable to the bottom of the venv/bin/activate file.

THUNDERSTONE_URL=https://search.ramseysolutions.net/texis/search/main.xml
export THUNDERSTONE_URL

Python Libraries with C extensions

Because lxml must be built with C extensions for libxml2 and libxslt in a way that plays well with the Amazon Lambda execution environment.

Using Python’s LXML in Amazon Lambda

Generally what we've done is use a python dependency file and let the build process handle building it into the correct CPU architecture

for instance: https://github.com/lampo/authx-lambda-functions/blob/master/src/requirements.txt

I believe (and I could be mistaken at this point) that requirements file should work locally as well for development. I know the build server understands those files...

I was able to get my script running on Lambda by duping my site-packages directory and replacing the LXML directory with the lambda-packages repo LXML directory. I think zipped and uploaded that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment