pythonamazon-web-servicesaws-lambda

Is there a way to use PyGithub on AWS Lambda


Im a big fan of PyGithub. I want use this library on AWS Lambda. But unfortunately,Im not able compile and use the source files of PyGithub.

I tried to download it from pypi, then compressed it as zip --> uploaded.

## app.py file
from github import Github

# using username and password
g = Github("username", "password")

def handler(event, context):
    for repo in g.get_user().get_repos():
        print(repo.name)
        repo.edit(has_wiki=False)

But its throwing an error.

{
  "errorMessage": "Unable to import module 'app': No module named 'jwt'",
  "errorType": "Runtime.ImportModuleError"
}

Is there any way to use this library on Lambda?


Solution

  • Yes. One way would be through lambda layers.

    To verify this possibility, I just created a custom layer with pygithub and can confirm that it works.

    The technique used includes docker tool described in the recent AWS blog:

    I created the layer as follows:

    1. Create empty folder, e.g. mylayer.

    2. Go to the folder and create requirements.txt file with the content of

    PyGithub
    
    1. Run the following docker command (for lambda with python 3.8):
    docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"
    
    1. Create layer as zip:
    zip -r mypygithublayer.zip python > /dev/null
    
    1. Create lambda layer based on mypygithublayer.zip in the AWS Console. Don't forget to specify Compatible runtimes to python3.8.

    2. Test the layer in lambda using the following lambda function:

    import json
    
    from github import Github
    
    def lambda_handler(event, context):
        
        print(dir(Github))
    
    

    The function executes correctly:

    ['FIX_REPO_GET_GIT_REF', '_Github__get_FIX_REPO_GET_GIT_REF', '_Github__get_per_page', '_Github__set_FIX_REPO_GET_GIT_REF', '_Github__set_per_page', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'create_from_raw_data', 'dump', 'get_emojis', 'get_events', 'get_gist', 'get_gists', 'get_gitignore_template', 'get_gitignore_templates', 'get_hook', 'get_hooks', 'get_installation', 'get_license', 'get_licenses', 'get_oauth_application', 'get_organization', 'get_organizations', 'get_project', 'get_project_column', 'get_rate_limit', 'get_repo', 'get_repos', 'get_user', 'get_users', 'load', 'oauth_scopes', 'per_page', 'rate_limiting', 'rate_limiting_resettime', 'render_markdown', 'search_code', 'search_commits', 'search_issues', 'search_repositories', 'search_topics', 'search_users']