Search code examples
pythonamazon-web-servicesaws-lambdaegg

Including markdown converter dependency in python AWS Lambda function


I am building a package to upload as an AWS Lambda function. In trying to include a dependency to Markdown 2.6.8, a python implementation of Markdown. When trying to test the function, I get the error Unable to import module 'markdown-convert': No module named markdown. I see from other similar questions that I may need to compile the dependency files inside Amazon Linux to make them usable with Lambda. I am not familiar with python and wonder if I should also be exploring the way that this code is packaged within an egg, and whether that influences how the dependency is imported. What would you suggest I pursue to try to resolve the error?

Here is the file structure of my package:

    .
    ├── markdown
    │   ├── blockparser.py
    │   ├── blockprocessors.py
    │   ├── extensions
    │   │   ├── abbr.py
    │   │   ├── admonition.py
    │   │   ├── attr_list.py
    │   │   ├── codehilite.py
    │   │   ├── def_list.py
    │   │   ├── extra.py
    │   │   ├── fenced_code.py
    │   │   ├── footnotes.py
    │   │   ├── headerid.py
    │   │   ├── __init__.py
    │   │   ├── meta.py
    │   │   ├── nl2br.py
    │   │   ├── __pycache__
    │   │   │   ├── abbr.cpython-34.pyc
    │   │   │   ├── admonition.cpython-34.pyc
    │   │   │   ├── attr_list.cpython-34.pyc
    │   │   │   ├── codehilite.cpython-34.pyc
    │   │   │   ├── def_list.cpython-34.pyc
    │   │   │   ├── extra.cpython-34.pyc
    │   │   │   ├── fenced_code.cpython-34.pyc
    │   │   │   ├── footnotes.cpython-34.pyc
    │   │   │   ├── headerid.cpython-34.pyc
    │   │   │   ├── __init__.cpython-34.pyc
    │   │   │   ├── meta.cpython-34.pyc
    │   │   │   ├── nl2br.cpython-34.pyc
    │   │   │   ├── sane_lists.cpython-34.pyc
    │   │   │   ├── smart_strong.cpython-34.pyc
    │   │   │   ├── smarty.cpython-34.pyc
    │   │   │   ├── tables.cpython-34.pyc
    │   │   │   ├── toc.cpython-34.pyc
    │   │   │   └── wikilinks.cpython-34.pyc
    │   │   ├── sane_lists.py
    │   │   ├── smart_strong.py
    │   │   ├── smarty.py
    │   │   ├── tables.py
    │   │   ├── toc.py
    │   │   └── wikilinks.py
    │   ├── __init__.py
    │   ├── inlinepatterns.py
    │   ├── __main__.py
    │   ├── odict.py
    │   ├── postprocessors.py
    │   ├── preprocessors.py
    │   ├── __pycache__
    │   │   ├── blockparser.cpython-34.pyc
    │   │   ├── blockprocessors.cpython-34.pyc
    │   │   ├── __init__.cpython-34.pyc
    │   │   ├── inlinepatterns.cpython-34.pyc
    │   │   ├── __main__.cpython-34.pyc
    │   │   ├── odict.cpython-34.pyc
    │   │   ├── postprocessors.cpython-34.pyc
    │   │   ├── preprocessors.cpython-34.pyc
    │   │   ├── serializers.cpython-34.pyc
    │   │   ├── treeprocessors.cpython-34.pyc
    │   │   ├── util.cpython-34.pyc
    │   │   └── __version__.cpython-34.pyc
    │   ├── serializers.py
    │   ├── treeprocessors.py
    │   ├── util.py
    │   └── __version__.py
    ├── Markdown-2.6.8.egg-info
    │   ├── dependency_links.txt
    │   ├── installed-files.txt
    │   ├── PKG-INFO
    │   ├── SOURCES.txt
    │   └── top_level.txt
    └── markdown-convert.py

The contents of markdown-convert.py:

import markdown

def lambda_handler(event, context):
    parsedHTML = {}
    parsedHTML[u'text'] = markdown.markdown(event[u'text'])
    return parsedHTML

Solution

  • Best advice here is to use virtualenv on your local development machine and install packages with pip.

    Then when you are ready, make the lambda package by recursively copying the entire content of <your-virtual-env>/lib/python2.7/site-packages/*. Make sure the content is placed in the root of your zip package. This is very important!

    Please, find more details here:

    http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html