|
| 1 | +<!-- |
| 2 | +title: 'Serverless Framework Python Flask API on AWS' |
| 3 | +description: 'This template demonstrates how to develop and deploy a simple Python Flask API running on AWS Lambda using the traditional Serverless Framework.' |
| 4 | +layout: Doc |
| 5 | +framework: v2 |
| 6 | +platform: AWS |
| 7 | +language: Python |
| 8 | +authorLink: 'https://github.com/serverless' |
| 9 | +authorName: 'Serverless, inc.' |
| 10 | +authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4' |
| 11 | +--> |
| 12 | + |
| 13 | +# Serverless Framework Python Flask API on AWS |
| 14 | + |
| 15 | +This template demonstrates how to develop and deploy a simple Python Flask API service running on AWS Lambda using the traditional Serverless Framework. |
| 16 | + |
| 17 | + |
| 18 | +## Anatomy of the template |
| 19 | + |
| 20 | +This template configures a single function, `api`, which is responsible for handling all incoming requests thanks to configured `http` events. To learn more about `http` event configuration options, please refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). 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). Additionally, the template 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). |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Prerequisites |
| 25 | + |
| 26 | +In order to package your dependencies locally with `serverless-python-requirements`, you need to have `Python3.8` installed locally. You can create and activate a dedicated virtual environment with the following command: |
| 27 | + |
| 28 | +```bash |
| 29 | +python3.8 -m venv ./venv |
| 30 | +source ./venv/bin/activate |
| 31 | +``` |
| 32 | + |
| 33 | +Alternatively, 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). |
| 34 | + |
| 35 | +### Deployment |
| 36 | + |
| 37 | +This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc. |
| 38 | + |
| 39 | +In order to deploy with dashboard, you need to first login with: |
| 40 | + |
| 41 | +``` |
| 42 | +serverless login |
| 43 | +``` |
| 44 | + |
| 45 | +install dependencies with: |
| 46 | + |
| 47 | +``` |
| 48 | +npm install |
| 49 | +``` |
| 50 | + |
| 51 | +and then perform deployment with: |
| 52 | + |
| 53 | +``` |
| 54 | +serverless deploy |
| 55 | +``` |
| 56 | + |
| 57 | +After running deploy, you should see output similar to: |
| 58 | + |
| 59 | +```bash |
| 60 | +erverless: Using Python specified in "runtime": python3.8 |
| 61 | +Serverless: Packaging Python WSGI handler... |
| 62 | +Serverless: Generated requirements from /home/xxx/xxx/xxx/examples/aws-python-flask-api/requirements.txt in /home/xxx/xxx/xxx/examples/aws-python-flask-api/.serverless/requirements.txt... |
| 63 | +Serverless: Using static cache of requirements found at /home/xxx/.cache/serverless-python-requirements/62f10436f9a1bb8040df30ef2db5736c8015b18256bf0b6f1b0cbb2640030244_slspyc ... |
| 64 | +Serverless: Packaging service... |
| 65 | +Serverless: Excluding development dependencies... |
| 66 | +Serverless: Injecting required Python packages to package... |
| 67 | +Serverless: Creating Stack... |
| 68 | +Serverless: Checking Stack create progress... |
| 69 | +........ |
| 70 | +Serverless: Stack create finished... |
| 71 | +Serverless: Uploading CloudFormation file to S3... |
| 72 | +Serverless: Uploading artifacts... |
| 73 | +Serverless: Uploading service aws-python-flask-api.zip file to S3 (1.3 MB)... |
| 74 | +Serverless: Validating template... |
| 75 | +Serverless: Updating Stack... |
| 76 | +Serverless: Checking Stack update progress... |
| 77 | +................................. |
| 78 | +Serverless: Stack update finished... |
| 79 | +Service Information |
| 80 | +service: aws-python-flask-api |
| 81 | +stage: dev |
| 82 | +region: us-east-1 |
| 83 | +stack: aws-python-flask-api-dev |
| 84 | +resources: 12 |
| 85 | +api keys: |
| 86 | + None |
| 87 | +endpoints: |
| 88 | + ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/ |
| 89 | + ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/{proxy+} |
| 90 | +functions: |
| 91 | + api: aws-python-flask-api-dev-api |
| 92 | +layers: |
| 93 | + None |
| 94 | +``` |
| 95 | + |
| 96 | +_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 [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). |
| 97 | + |
| 98 | +### Invocation |
| 99 | + |
| 100 | +After successful deployment, you can call the created application via HTTP: |
| 101 | + |
| 102 | +```bash |
| 103 | +curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/ |
| 104 | +``` |
| 105 | + |
| 106 | +Which should result in the following response: |
| 107 | + |
| 108 | +``` |
| 109 | +{"message":"Hello from root!"} |
| 110 | +``` |
| 111 | + |
| 112 | +Calling the `/hello` path with: |
| 113 | + |
| 114 | +```bash |
| 115 | +curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/hello |
| 116 | +``` |
| 117 | + |
| 118 | +Should result in the following response: |
| 119 | + |
| 120 | +```bash |
| 121 | +{"message":"Hello from path!"} |
| 122 | +``` |
| 123 | + |
| 124 | +If you try to invoke a path or method that does not have a configured handler, e.g. with: |
| 125 | + |
| 126 | +```bash |
| 127 | +curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/nonexistent |
| 128 | +``` |
| 129 | + |
| 130 | +You should receive the following response: |
| 131 | + |
| 132 | +```bash |
| 133 | +{"error":"Not Found!"} |
| 134 | +``` |
| 135 | + |
| 136 | +### Local development |
| 137 | + |
| 138 | +Thanks 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` dependency, 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: |
| 139 | + |
| 140 | +```bash |
| 141 | +pip install werkzeug |
| 142 | +pip install -r requirements.txt |
| 143 | +``` |
| 144 | + |
| 145 | +At this point, you can run your application locally with the following command: |
| 146 | + |
| 147 | +```bash |
| 148 | +serverless wsgi serve |
| 149 | +``` |
| 150 | + |
| 151 | +For additional local development capabilities of `serverless-wsgi` plugin, please refer to corresponding [GitHub repository](https://github.com/logandk/serverless-wsgi). |
0 commit comments