{"id":22702183,"url":"https://github.com/atnic/flask-base-serverless","last_synced_at":"2025-03-29T19:26:42.109Z","repository":{"id":218048633,"uuid":"738633938","full_name":"Atnic/flask-base-serverless","owner":"Atnic","description":null,"archived":false,"fork":false,"pushed_at":"2024-01-26T13:56:17.000Z","size":161,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-04T20:18:32.623Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Atnic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-03T17:20:20.000Z","updated_at":"2024-01-03T21:47:34.000Z","dependencies_parsed_at":"2024-12-10T07:12:32.155Z","dependency_job_id":"ffaafc52-b370-4708-a1ac-cdfe6ebb4883","html_url":"https://github.com/Atnic/flask-base-serverless","commit_stats":null,"previous_names":["atnic/flask-base-serverless"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atnic%2Fflask-base-serverless","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atnic%2Fflask-base-serverless/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atnic%2Fflask-base-serverless/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atnic%2Fflask-base-serverless/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Atnic","download_url":"https://codeload.github.com/Atnic/flask-base-serverless/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246233055,"owners_count":20744767,"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":[],"created_at":"2024-12-10T07:12:29.059Z","updated_at":"2025-03-29T19:26:42.089Z","avatar_url":"https://github.com/Atnic.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!--\ntitle: 'Serverless Framework Python Flask API backed by DynamoDB on AWS'\ndescription: 'This template demonstrates how to develop and deploy a simple Python Flask API service backed by DynamoDB running on AWS Lambda using the traditional Serverless Framework.'\nlayout: Doc\nframework: v3\nplatform: AWS\nlanguage: Python\npriority: 2\nauthorLink: 'https://github.com/serverless'\nauthorName: 'Serverless, inc.'\nauthorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200\u0026v=4'\n--\u003e\n\n# Serverless Framework Python Flask API service backed by DynamoDB on AWS\n\nThis template demonstrates how to develop and deploy a simple Python Flask API service, backed by DynamoDB, running on AWS Lambda using the traditional Serverless Framework.\n\n\n## Anatomy of the template\n\nThis template configures a single function, `api`, which is responsible for handling all incoming requests thanks to configured `httpApi` events. To learn more about `httpApi` event configuration options, please refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). As the events are configured in a way to accept all incoming requests, `Flask` framework is responsible for routing and handling requests internally. The implementation takes advantage of `serverless-wsgi`, which allows you to wrap WSGI applications such as Flask apps. To learn more about `serverless-wsgi`, please refer to corresponding [GitHub repository](https://github.com/logandk/serverless-wsgi). The template also relies on `serverless-python-requirements` plugin for packaging dependencies from `requirements.txt` file. For more details about `serverless-python-requirements` configuration, please refer to corresponding [GitHub repository](https://github.com/UnitedIncome/serverless-python-requirements).\n\nAdditionally, the template also handles provisioning of a DynamoDB database that is used for storing data about users. The Flask application exposes two endpoints, `POST /users` and `GET /user/{userId}`, which allow to create and retrieve users.\n\n## Usage\n\n### Prerequisites\n\nIn order to package your dependencies locally with `serverless-python-requirements`, you need to have `Python3.9` installed locally. You can create and activate a dedicated virtual environment with the following command:\n\n```bash\npython3.9 -m venv ./venv\nsource ./venv/bin/activate\n```\n\nAlternatively, you can also use `dockerizePip` configuration from `serverless-python-requirements`. For details on that, please refer to corresponding [GitHub repository](https://github.com/UnitedIncome/serverless-python-requirements).\n\n### Deployment\n\nThis example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.\n\nIn order to deploy with dashboard, you need to first login with:\n\n```\nserverless login\n```\n\ninstall dependencies with:\n\n```\nnpm install\n```\n\nand then perform deployment with:\n\n```\nserverless deploy\n```\n\nAfter running deploy, you should see output similar to:\n\n```bash\nDeploying aws-python-flask-dynamodb-api-project to stage dev (us-east-1)\n\n✔ Service deployed to stack aws-python-flask-dynamodb-api-project-dev (182s)\n\nendpoint: ANY - https://xxxxxxxx.execute-api.us-east-1.amazonaws.com\nfunctions:\n  api: aws-python-flask-dynamodb-api-project-dev-api (1.5 MB)\n```\n\n_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/).\n\n### Invocation\n\nAfter successful deployment, you can create a new user by calling the corresponding endpoint:\n\n```bash\ncurl --request POST 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/users' --header 'Content-Type: application/json' --data-raw '{\"name\": \"John\", \"userId\": \"someUserId\"}'\n```\n\nWhich should result in the following response:\n\n```bash\n{\"userId\":\"someUserId\",\"name\":\"John\"}\n```\n\nYou can later retrieve the user by `userId` by calling the following endpoint:\n\n```bash\ncurl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/users/someUserId\n```\n\nWhich should result in the following response:\n\n```bash\n{\"userId\":\"someUserId\",\"name\":\"John\"}\n```\n\nIf you try to retrieve user that does not exist, you should receive the following response:\n\n```bash\n{\"error\":\"Could not find user with provided \\\"userId\\\"\"}\n```\n\n### Local development\n\nThanks to capabilities of `serverless-wsgi`, it is also possible to run your application locally, however, in order to do that, you will need to first install `werkzeug`, `boto3` dependencies, as well as all other dependencies listed in `requirements.txt`. It is recommended to use a dedicated virtual environment for that purpose. You can install all needed dependencies with the following commands:\n\n```bash\npip install werkzeug boto3\npip install -r requirements.txt\n```\n\nAdditionally, you will need to emulate DynamoDB locally, which can be done by using `serverless-dynamodb-local` plugin. In order to do that, execute the following commands:\n\n```bash\nserverless plugin install -n serverless-dynamodb-local\nserverless dynamodb install\n```\n\nIt will add the plugin to `devDependencies` in `package.json` file as well as to `plugins` section in `serverless.yml`. Additionally, it will also install DynamoDB locally.\n\nYou should also add the following config to `custom` section in `serverless.yml`:\n\n\n```yml\ncustom:\n  (...)\n  dynamodb:\n    start:\n      migrate: true\n    stages:\n      - dev\n```\n\nAdditionally, we need to reconfigure DynamoDB Client to connect to our local instance of DynamoDB. We can take advantage of `IS_OFFLINE` environment variable set by `serverless-wsgi` plugin and replace:\n\n\n```python\ndynamodb_client = boto3.client('dynamodb')\n```\n\nwith\n\n```python\ndynamodb_client = boto3.client('dynamodb')\n\nif os.environ.get('IS_OFFLINE'):\n    dynamodb_client = boto3.client('dynamodb', region_name='localhost', endpoint_url='http://localhost:8000')\n```\n\nNow you can start DynamoDB local with the following command:\n\n```bash\nserverless dynamodb start\n```\n\nAt this point, you can run your application locally with the following command:\n\n```bash\nserverless wsgi serve\n```\n\nFor additional local development capabilities of `serverless-wsgi` and `serverless-dynamodb-local` plugins, please refer to corresponding GitHub repositories:\n- https://github.com/logandk/serverless-wsgi \n- https://github.com/99x/serverless-dynamodb-local\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatnic%2Fflask-base-serverless","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatnic%2Fflask-base-serverless","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatnic%2Fflask-base-serverless/lists"}