{"id":13449415,"url":"https://github.com/nficano/python-lambda","last_synced_at":"2025-05-14T17:07:17.252Z","repository":{"id":35949565,"uuid":"52575367","full_name":"nficano/python-lambda","owner":"nficano","description":" A toolkit for developing and deploying serverless Python code in AWS Lambda. ","archived":false,"fork":false,"pushed_at":"2023-06-28T11:38:10.000Z","size":440,"stargazers_count":1506,"open_issues_count":67,"forks_count":226,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-05-14T17:06:32.875Z","etag":null,"topics":["aws","aws-lambda","microservices","python","serverless"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nficano.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-02-26T03:21:06.000Z","updated_at":"2025-04-29T09:31:44.000Z","dependencies_parsed_at":"2024-01-02T21:57:57.457Z","dependency_job_id":"3d78029d-382e-4dd3-876d-33a0664bfa61","html_url":"https://github.com/nficano/python-lambda","commit_stats":null,"previous_names":[],"tags_count":57,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nficano%2Fpython-lambda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nficano%2Fpython-lambda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nficano%2Fpython-lambda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nficano%2Fpython-lambda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nficano","download_url":"https://codeload.github.com/nficano/python-lambda/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254190396,"owners_count":22029632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-lambda","microservices","python","serverless"],"created_at":"2024-07-31T06:00:37.482Z","updated_at":"2025-05-14T17:07:17.233Z","avatar_url":"https://github.com/nficano.png","language":"Python","readme":"\u003cdiv align=\"center\"\u003e\n  \u003cp\u003e\n  \u003cimg src=\"https://assets.nickficano.com/gh-pythonlambda.svg\" width=\"221\" height=\"227\" alt=\"python-lambda logo\" /\u003e\n  \u003c/p\u003e\n  \u003cp align=\"center\"\u003e\n\t  \u003cimg src=\"https://img.shields.io/pypi/v/python-lambda.svg\" alt=\"pypi\" /\u003e\n    \u003ca href=\"https://pypi.org/project/python-lambda/\"\u003e\u003cimg src=\"https://img.shields.io/pypi/dm/python-lambda.svg\" alt=\"pypi\"\u003e\u003c/a\u003e\n\t  \u003ca href=\"https://pypi.python.org/pypi/python-lambda/\"\u003e\u003cimg src=\"https://img.shields.io/pypi/pyversions/python-lambda.svg\" /\u003e\u003c/a\u003e\n  \u003c/p\u003e\n\u003c/div\u003e\n\nPython-lambda is a toolset for developing and deploying *serverless* Python code in AWS Lambda.\n\n# A call for contributors\nWith python-lambda and pytube both continuing to gain momentum, I'm calling for\ncontributors to help build out new features, review pull requests, fix bugs,\nand maintain overall code quality. If you're interested, please email me at\nnficano[at]gmail.com.\n\n# Description\n\nAWS Lambda is a service that allows you to write Python, Java, or Node.js code\nthat gets executed in response to events like http requests or files uploaded\nto S3.\n\nWorking with Lambda is relatively easy, but the process of bundling and\ndeploying your code is not as simple as it could be.\n\nThe *Python-Lambda* library takes away the guess work of developing your\nPython-Lambda services by providing you a toolset to streamline the annoying\nparts.\n\n# Requirements\n\n* Python 2.7, \u003e= 3.6 (At the time of writing this, these are the Python runtimes supported by AWS Lambda).\n* Pip (\\~8.1.1)\n* Virtualenv (\\~15.0.0)\n* Virtualenvwrapper (\\~4.7.1)\n\n\n# Getting Started\n\nFirst, you must create an IAM Role on your AWS account called\n``lambda_basic_execution`` with the ``LambdaBasicExecution`` policy attached.\n\nOn your computer, create a new virtualenv and project folder.\n\n```bash\n$ mkvirtualenv pylambda\n(pylambda) $ mkdir pylambda\n```\n\nNext, download *Python-Lambda* using pip via pypi.\n\n```bash\n(pylambda) $ pip install python-lambda\n```\n\nFrom your ``pylambda`` directory, run the following to bootstrap your project.\n\n```bash\n(pylambda) $ lambda init\n```\n\nThis will create the following files: ``event.json``, ``__init__.py``,\n``service.py``, and ``config.yaml``.\n\nLet's begin by opening ``config.yaml`` in the text editor of your choice. For\nthe purpose of this tutorial, the only required information is\n``aws_access_key_id`` and ``aws_secret_access_key``. You can find these by\nlogging into the AWS management console.\n\nNext let's open ``service.py``, in here you'll find the following function:\n\n```python\ndef handler(event, context):\n    # Your code goes here!\n    e = event.get('e')\n    pi = event.get('pi')\n    return e + pi\n```\n\nThis is the handler function; this is the function AWS Lambda will invoke in\nresponse to an event. You will notice that in the sample code ``e`` and ``pi``\nare values in a ``dict``. AWS Lambda uses the ``event`` parameter to pass in\nevent data to the handler.\n\nSo if, for example, your function is responding to an http request, ``event``\nwill be the ``POST`` JSON data and if your function returns something, the\ncontents will be in your http response payload.\n\nNext let's open the ``event.json`` file:\n\n```json\n{\n  \"pi\": 3.14,\n  \"e\": 2.718\n}\n```\nHere you'll find the values of ``e`` and ``pi`` that are being referenced in\nthe sample code.\n\nIf you now try and run:\n\n```bash\n(pylambda) $ lambda invoke -v\n```\n\nYou will get:\n```bash\n# 5.858\n# execution time: 0.00000310s\n# function execution timeout: 15s\n```\n\nAs you probably put together, the ``lambda invoke`` command grabs the values\nstored in the ``event.json`` file and passes them to your function.\n\nThe ``event.json`` file should help you develop your Lambda service locally.\nYou can specify an alternate ``event.json`` file by passing the\n``--event-file=\u003cfilename\u003e.json`` argument to ``lambda invoke``.\n\nWhen you're ready to deploy your code to Lambda simply run:\n\n```bash\n(pylambda) $ lambda deploy\n```\n\nThe deploy script will evaluate your virtualenv and identify your project\ndependencies. It will package these up along with your handler function to a\nzip file that it then uploads to AWS Lambda.\n\nYou can now log into the\n[AWS Lambda management console](https://console.aws.amazon.com/lambda/) to\nverify the code deployed successfully.\n\n### Wiring to an API endpoint\n\nIf you're looking to develop a simple microservice you can easily wire your\nfunction up to an http endpoint.\n\nBegin by navigating to your [AWS Lambda management console](https://console.aws.amazon.com/lambda/) and\nclicking on your function. Click the API Endpoints tab and click \"Add API endpoint\".\n\nUnder API endpoint type select \"API Gateway\".\n\nNext change Method to ``POST`` and Security to \"Open\" and click submit (NOTE:\nyou should secure this for use in production, open security is used for demo\npurposes).\n\nAt last you need to change the return value of the function to comply with the\nstandard defined for the API Gateway endpoint, the function should now look\nlike this:\n\n```\ndef handler(event, context):\n    # Your code goes here!\n    e = event.get('e')\n    pi = event.get('pi')\n    return {\n        \"statusCode\": 200,\n        \"headers\": { \"Content-Type\": \"application/json\"},\n        \"body\": e + pi\n    }\n```\n\nNow try and run:\n\n```bash\n$ curl --header \"Content-Type:application/json\" \\\n       --request POST \\\n       --data '{\"pi\": 3.14, \"e\": 2.718}' \\\n       https://\u003cAPI endpoint URL\u003e\n# 5.8580000000000005\n```\n\n### Environment Variables\nLambda functions support environment variables. In order to set environment\nvariables for your deployed code to use, you can configure them in\n``config.yaml``.  To load the value for the environment variable at the time of\ndeployment (instead of hard coding them in your configuration file), you can\nuse local environment values (see 'env3' in example code below).\n\n```yaml\nenvironment_variables:\n  env1: foo\n  env2: baz\n  env3: ${LOCAL_ENVIRONMENT_VARIABLE_NAME}\n```\n\nThis would create environment variables in the lambda instance upon deploy. If\nyour functions don't need environment variables, simply leave this section out\nof your config.\n\n### Uploading to S3\nYou may find that you do not need the toolkit to fully\ndeploy your Lambda or that your code bundle is too large to upload via the API.\nYou can use the ``upload`` command to send the bundle to an S3 bucket of your\nchoosing.  Before doing this, you will need to set the following variables in\n``config.yaml``:\n\n```yaml\nrole: basic_s3_upload\nbucket_name: 'example-bucket'\ns3_key_prefix: 'path/to/file/'\n```\nYour role must have ``s3:PutObject`` permission on the bucket/key that you\nspecify for the upload to work properly. Once you have that set, you can\nexecute ``lambda upload`` to initiate the transfer.\n\n### Deploying via S3\nYou can also choose to use S3 as your source for Lambda deployments.  This can\nbe done by issuing ``lambda deploy-s3`` with the same variables/AWS permissions\nyou'd set for executing the ``upload`` command.\n\n## Development\nDevelopment of \"python-lambda\" is facilitated exclusively on GitHub.\nContributions in the form of patches, tests and feature creation and/or\nrequests are very welcome and highly encouraged. Please open an issue if this\ntool does not function as you'd expect.\n\n### Environment Setup\n1. [Install pipenv](https://github.com/pypa/pipenv)\n2. [Install direnv](https://direnv.net/)\n3. [Install Precommit](https://pre-commit.com/#install) (optional but preferred)\n4. ``cd`` into the project and enter \"direnv allow\" when prompted. This will begin\n   installing all the development dependancies.\n5. If you installed pre-commit, run ``pre-commit install`` inside the project\n   directory to setup the githooks.\n\n### Releasing to Pypi\nOnce you pushed your chances to master, run **one** of the following:\n\n ```sh\n # If you're installing a major release:\n make deploy-major\n\n # If you're installing a minor release:\n make deploy-minor\n\n# If you're installing a patch release:\nmake deploy-patch\n ```\n","funding_links":[],"categories":["Python","Serverless Frameworks","资源列表","无服务器框架","Serverless 框架","Frameworks","Serverless Frameworks [🔝](#readme)","Open Source Repos","Awesome Python"],"sub_categories":["Serverless 框架","Lambda","Serverless Frameworks"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnficano%2Fpython-lambda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnficano%2Fpython-lambda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnficano%2Fpython-lambda/lists"}