在 python AWS Lambda 函数中包含降价转换器依赖项

Including markdown converter dependency in python AWS Lambda function

我正在构建一个包以作为 AWS Lambda 函数上传。在尝试包含对 Markdown 2.6.8 的依赖时,Markdown 的 python 实现。尝试测试该功能时,出现错误 Unable to import module 'markdown-convert': No module named markdown。我从其他类似的问题中看到,我可能需要在 Amazon Linux 中编译依赖文件以使它们可用于 Lambda。我不熟悉 python 并且想知道我是否也应该探索这段代码在 egg 中的打包方式,以及这是否会影响依赖项的导入方式。您建议我采取什么措施来尝试解决错误?

这是我的包的文件结构:

    .
    ├── 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

markdown的内容-convert.py:

import markdown

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

此处最好的建议是在您的本地开发机器上使用 virtualenv 并使用 pip.

安装软件包

然后当你准备好后,通过递归复制<your-virtual-env>/lib/python2.7/site-packages/*的全部内容来制作lambda包。确保内容位于 zip 包的根目录中。这很重要!

请在此处找到更多详细信息:

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