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?
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:
Create empty folder, e.g. mylayer
.
Go to the folder and create requirements.txt
file with the content of
PyGithub
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"
zip -r mypygithublayer.zip python > /dev/null
Create lambda layer based on mypygithublayer.zip
in the AWS Console. Don't forget to specify Compatible runtimes
to python3.8
.
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']