{"id":18792602,"url":"https://github.com/amyodov/example-amazon-lambda-django-zappa","last_synced_at":"2025-08-17T13:09:39.642Z","repository":{"id":46836248,"uuid":"281037269","full_name":"amyodov/example-amazon-lambda-django-zappa","owner":"amyodov","description":"Example Amazon Lambda Django serverless app (using Zappa)","archived":false,"fork":false,"pushed_at":"2022-12-26T21:35:16.000Z","size":65,"stargazers_count":0,"open_issues_count":7,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-29T15:28:04.685Z","etag":null,"topics":["amazon","lambda","python","serverless","zappa"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amyodov.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}},"created_at":"2020-07-20T06:49:13.000Z","updated_at":"2021-04-06T13:46:25.000Z","dependencies_parsed_at":"2023-01-31T02:00:51.754Z","dependency_job_id":null,"html_url":"https://github.com/amyodov/example-amazon-lambda-django-zappa","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amyodov%2Fexample-amazon-lambda-django-zappa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amyodov%2Fexample-amazon-lambda-django-zappa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amyodov%2Fexample-amazon-lambda-django-zappa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amyodov%2Fexample-amazon-lambda-django-zappa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amyodov","download_url":"https://codeload.github.com/amyodov/example-amazon-lambda-django-zappa/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239718373,"owners_count":19685725,"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":["amazon","lambda","python","serverless","zappa"],"created_at":"2024-11-07T21:20:45.587Z","updated_at":"2025-02-19T19:14:36.449Z","avatar_url":"https://github.com/amyodov.png","language":"Python","readme":"# Example Amazon Lambda Django app (using Zappa)\n\nThis is the basic (example/template) project for running a Django app on\nAmazon Lambda, completely serverless. Uses Zappa Python library for\ndeployment.\n\nThis is a very minimal environment; it has no static files serving and no DB.\nDepending on your needs, feel free to enhance it with S3/CloudFront serving\nand probably DB (maybe even Amazon RDS / Aurora Serverless).\n\nFor the test purposes, though, it already contains the Django-Rest-Framework\nconfigured for DB-less access as well.\n\nFor the purposes of easier usage as a template for other projects, this\nexample has two different parts: setting up just the Django app server on\nAmazon Lambda (so you could use it, probably, for REST APIs), without any static files;\nand, if you really need it - configuring the extra S3 bucket for static files.\n\n## App server on Amazon Lambda\n\n### Initial setup\n\nFor easier maintenance, you should use `pipenv` (as a replacement for both\n`pip` and `virtualenv` simultaneously). A new pipenv environment is\nconfigured in this way:\n\n~~~sh\nbrew install pipenv\n\npipenv --three\n~~~\n\nHaving the pipenv environment, you may want to \"switch\" to it. In `pip`,\nyou were using something like `source ve/bin/activate.sh`; in pipenv, you do\njust:\n\n~~~sh\npipenv shell\n~~~\n\nHaving \"sourced\" the pipenv environment, you install all the dependencies:\n\n~~~sh\npipenv install\n~~~\n\n### Configuring Amazon Lambda\n\n(No configuration in AWS control panel is normally required!)\n\nFor using the Amazon AWS/Lambda environment, the Python package `zappa` is\nused (it is already installed as a requirement).\nBut it needs to learn about your Amazon credentials.\n\nIf you've set up your Amazon credentials before (using `~/.aws/config` and\nespecially `~/.aws/credentials`), you are basically ready to deploy. If you\nhaven't, you may want a `~/.aws/credentials` file with the contents like:\n\n~~~ini\n[default]\naws_access_key_id = YOUR_AWS_ACCESS_KEY_ID\naws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY\n~~~\n\nSo after this you just run\n\n~~~sh\nzappa init\n~~~\n\nand it configures all the Amazon Lambda settings needed for deployment\n(asking you some questions in the process).\n\nIt will generate the `zappa_settings.json` file in the process, which\nconfigures your settings for deployment. By default, this file won't be\ncommitted to Git (as it is mentioned in Gitignore); depending on your\nproject security rules, you may want to remove it from Gitignore and commit\nyour file, or generate it dynamically from other deployment settings (like\nAnsible Vault), or use some other way of handling it. Investigate the data\nin the file yourself and choose for yourself if you want to commit it in\nyour repository or not.\n\n### Deploying to Amazon Lambda for the first time\n\nJust run:\n\n~~~sh\nzappa deploy dev\n~~~\n\nand you are done!\n\n(In the example, `dev` is the name of the environment for Lambda; you may\nuse multiple environments if you want).\n\n### Update the Amazon Lambda deployment\n\nIf you updated your code and need to re-deploy the latest codebase to Amazon\nLambda, just use:\n\n~~~sh\nzappa update dev\n~~~\n\n\n## Using S3 for static files\n\n### Configuring S3\n\nFor static files, we will be using S3. Configuring it is (a little bit) more\ninvolved, and you cannot get rid of visiting the Amazon AWS control panel.\n\nSo go to the AWS control panel, and create a new S3 bucket. Note you cannot\nuse the same bucket that is used for zappa deployment - you need a different\nbucket for static files.\n\nFor example, you create an S3 bucket with the name `zappa-static` (in fact\nyou'll have to find a globally unique name for the bucket). During\nthe creation/configuring, you probably want to leave most settings on\ndefault. But note: when setting up the permissions of this bucket, you will\nbe asked if you want to \"_Block all public access_\" (and some other\nsuboptions, like \"_Block public access to buckets and objects granted through new access control lists\n(ACLs)_\"). In fact, you want to set all these \"blocks\" to \"Off\" (so the\nfiles in this bucket will be publicly visible!)\n\nAfter you created this S3 bucket and made sure to \"Unblock\" it, you should go\nto the CORS configuration for this bucket, press \"Add CORS configuration\",\nand add the following one:\n\n~~~xml\n\u003cCORSConfiguration\u003e\n  \u003cCORSRule\u003e\n    \u003cAllowedOrigin\u003e*\u003c/AllowedOrigin\u003e\n    \u003cAllowedMethod\u003eGET\u003c/AllowedMethod\u003e\n    \u003cMaxAgeSeconds\u003e3000\u003c/MaxAgeSeconds\u003e\n    \u003cAllowedHeader\u003eAuthorization\u003c/AllowedHeader\u003e\n  \u003c/CORSRule\u003e\n\u003c/CORSConfiguration\u003e\n~~~\n\nIf requested to add it in JSON format, it should look this way:\n\n~~~json\n[\n    {\n        \"AllowedHeaders\": [\n            \"Authorization\"\n        ],\n        \"AllowedMethods\": [\n            \"GET\"\n        ],\n        \"AllowedOrigins\": [\n            \"*\"\n        ],\n        \"ExposeHeaders\": [],\n        \"MaxAgeSeconds\": 3000\n    }\n]\n~~~\n\nAfter this, you are basically done with configuring S3. Now you need to\nteach the Django site to use it.\n\n### Configuring Django site to use S3 for static files\n\nBy default, the `lambdatest` site has it `settings.py` which tries to load\nthe extra (site-specific) settings from from `settings_local.py`, which\nshould be in the same directory as the `settings.py` file.\n\nTo store the static files on Amazon S3, you need to use the `django-s3-storage`\nmodule (already added in the dependencies) and configure it accordingly; so\nput at least the lines like this (with your actual values) into the\n`settings_local.py`:\n\n~~~python\nSTATICFILES_STORAGE = 'django_s3_storage.storage.StaticS3Storage'\nAWS_S3_BUCKET_NAME_STATIC = 'zappa-static'\n# use the real name of your S3 bucket; we've used \"zappa-static\" in the example\n# but it should be globally unique\n\n# These next two lines will serve the static files directly\n# from the s3 bucket\nAWS_S3_CUSTOM_DOMAIN = f'{AWS_S3_BUCKET_NAME_STATIC}.s3.amazonaws.com'\nSTATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/'\n\n# OR...if you create a fancy custom domain for your static files use:\n#AWS_S3_PUBLIC_URL_STATIC = \"https://static.yourdomain.com/\"\n~~~\n\n### Deploying the static files\n\nNow, as you configured the static files storage engine to use\n`StaticS3Storage`, every run of `collectstatic` will upload them to S3.\n\nInterestingly, you can both run it locally, as\n\n~~~sh\npython lambdatest/manage.py collectstatic --noinput\n~~~\n\n, but also you can run it even on the Amazon Lambda environment!\n\n~~~sh\nzappa manage dev \"collectstatic --noinput\"\n~~~\n\nNote that using `zappa manage` will imply the extra costs for running Amazon\nLambda for this.\n\n## Summary: redeploy everything\n\n~~~sh\npython lambdatest/manage.py collectstatic --noinput\nzappa update dev\n~~~\n\n\n## Setting up CloudFront for delivering the static files from CDN\n\nLeft as an exercise for the reader.\n\n\n## Problems?\n\nIf you have any problems with launching this deployed application,\n`zappa tail` can help you. Also, check CloudWatch logs in your AWS console.\n\n\n---\n\n(C) 2020 Alex Myodov","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famyodov%2Fexample-amazon-lambda-django-zappa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famyodov%2Fexample-amazon-lambda-django-zappa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famyodov%2Fexample-amazon-lambda-django-zappa/lists"}