# Serverless

To be as portable as possible, even with severless, we use [serverless framework](https://serverless.com/framework/docs/). It allows us to deploy our functions across multiple clouds like AWS, GCP, Azure and IBM OpenWisk.&#x20;

## Setup  & Installation

Make sure you have your [Developer Environment Setup](https://den.gitbook.io/developerplaybook/development-environment)&#x20;

```bash
npm install serverless -g
```

In your repo create your **serverless template for Python**

```
mkdir notes-app-api
cd notes-app-api
sls create --template aws-python3
```

### Compile non-pure Python modules (e.g. C?)

To compile non-pure Python modules, install [Docker](https://docs.docker.com/engine/installation/), the [Lambda Docker Image](https://github.com/lambci/docker-lambda) and [serverless-python-requirements](https://serverless.com/blog/serverless-python-packaging/) . Enable **dockerizePip** in **serverless.yml** and `serverless deploy` again.

```bash
npm init
npm install --save serverless-python-requirements
```

To configure our `serverless.yml` file to use the plugin, we'll add the following lines in our `serverless.yml`:

{% code title="serverless.yml" %}

```yaml
plugins: 
    - serverless-python-requirements 
custom: 
    pythonRequirements: 
        dockerizePip: non-linux
```

{% endcode %}

## Implementation

### Create virtual env local, activate and deactivate

```
#http://sourabhbajaj.com/mac-setup/Python/virtualenv.html
virtualenv venv
source venv/bin/activate
deactivate
```

### Install python dependency for this function:

```python
pip install boto3 google-api-python-client requests
```

### Store a reference to my dependencies:

```python
pip freeze > requirements.txt
```

Optional: Re-install the dependencies from the `requirements.txt`:

```python
pip install -r requirements.txt
```

### Implement the function

1. Define the function in the `serverless.yml` including the events (e.g. http) that trigger the function and the handler
2. Implement the unit test: what is the best serverless unit test framework for python?
3. Implement the handler function in `handler.py`
4. Test the function locally

```
sls invoke local --function create_api --path tests/create-api-event.json
sls invoke local -f hello
```

Run the unit tests

```python
python -m unittest discover -s tests
```

### Protect Endpoint with API Key

{% embed url="<https://serverless.com/framework/docs/providers/aws/events/apigateway/#setting-api-keys-for-your-rest-api>" %}

### Examples

{% embed url="<https://github.com/serverless/examples>" %}

## Deployment

### Deploy a project

```
sls deploy
```

![](https://412027953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBulIh9Wa6TPIk4F7x5%2F-LBulwFWKqpOjjszN_cy%2F-LBulzbrnvI-Xqd7EulG%2Fdeployment.png?generation=1525699593318959\&alt=media)

The deployment takes very long. I have to see how to optimize this.

#### Test the deployed function

You can use the endpoint endpoint and use Postman to make a "real world" request: ![](https://412027953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBulIh9Wa6TPIk4F7x5%2F-LBulwFWKqpOjjszN_cy%2F-LBulzgujdt9nz2xh8Ab%2Ftest-with-postman.png?generation=1525699591890683\&alt=media)

Or you can use the CLI and use:

```
sls invoke -f create_api --path tests/create-api.json
```

![](https://412027953-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LBulIh9Wa6TPIk4F7x5%2F-LBulwFWKqpOjjszN_cy%2F-LBulzki4rmBGEIKpCL0%2Ftest-with-cli-at-runtime.png?generation=1525699592472213\&alt=media)

### Deploy single function

```
sls deploy function --function hello
```

## Sources:

* <https://github.com/serverless/examples/tree/master/aws-python-pynamodb-s3-sigurl>&#x20;
* <https://github.com/AnomalyInnovations/serverless-python-starter>
* <https://serverlesscode.com/post/python-3-on-serverless-framework/>

##
