Creating Serverless Python API

#<b>INTRODUCTION</b>
This article we are going to discuss about <b>Creating Serverless Python API</b>. We are going to separate this article into three big part. First part, we are going to build an api using <b>Chalice & Lambda</b>. Second part , we are going to build an api using <b>Zappa & Lambda</b> and third part , we going to use <b>Serverless & AWS</b>.

#<b>I. Chalice & Lambda</b>

##<b>1. AWS Setup</b>

- First of all , we need to install awscli to configure our aws credential.

```terminal
pip install awscli
```
- Configure

```terminal
aws configure

AWS Access Key ID [None]: input your access key
AWS Secret Access Key [None]: input your secret access key
Default region name [None]: input your region
Default output format [None]: json
```

- Verifies

```terminal
aws s3 ls
```

##<b>2. Chalice</b>

Now it's time for us to play with chalice , first let us install chalice.

- Install chalice

```terminal
pip install chalice
```

- Create Chalcie project

```terminal
chalice new-project chalice_project_name
cd chalice_project_name
ls
```
You will see two files which are.
1.<b>chalice_project_name.py</b> : It is a default chalice app.
2.<b>requirements</b> : just a blank text

you can open the chalice_project_name.py file to see what is going on.after that let us deploy.

- Deploy

```terminal
chalice deploy
```

it will take a while and you will seee something like this

```terminal
Creating role: chalice_project_name-dev
Creating deployment package.
Creating lambda function: chalice_project_name-dev
Initiating first time deployment.
Deploying to API Gateway stage: api
https://a23wxp5cve.execute-api.eu-west-1.amazonaws.com/api/
```
copy that URL and paste it in the browser, you will see the response of your api. Now you can modify the API to follow what you need.

##<b>3. Chalice Undeploy</b>
after you are done playing around with chalice , you can undeploy it by doing like so.

```terminal
chalice undeploy --stage-dev
```
#<b>II. Zappa & Lambda</b>

Zappa is a wonderful python package created by Miserlou.It saved us a lot of time and allows users to deploy code to Lambda with minimal configuration with just one command from the CLI. 
<b>Thing to note</b> : Zappa only work in virtual environment.

##<b>1.Setting Up</b>

Now let start install virtual env.

- Installing virtual env

```terminal
pip install pipenv
```

- Setting up virtual env

```terminal
pipenv --three
pipenv shell
```

- Now verifies you are connected to aws

```terminal
aws s3 ls
```

- Ok now let install zappa and flask

```terminal 
pipenv install zappa flask
```
##<b>2.Build API</b>
Ok, we have finished the setup. Now let us use flask to build the sample api.

```python
from flask import Flask , jsonify
app = Flask(__name__)

@app.route("/quad")
def hello():
    return jsonify({"result":"success"})

if __name__ == '__main__':
    app.run(debug=True)
```

- You can run the app.py and see the response on the localhost and now it time to deploy it with zappa.

##<b>3.Zappa Deploy</b>

```terminal
zappa init
```

this will create your zappa_settings.json file and add it to the project, now you can deploy! This is what makes zappa so nice, it takes this tiny config and then packages and deploys with one command. Ok now let deploy zappa.

```terminal
zappa deploy
```
This will give you something like this.

<img width="567" alt="Screen Shot 2018-06-13 at 7.36.40 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/0ceb1920-d84b-e115-89ae-c2214415a751.png">

copy the link and paste it in the browser, you will see the response of the API.

##<b>4.Zappa Undeploy</b>

If you are follow this tutorial just for testing purpose , after play around with it. You can delete it by below command :

```terminal
zappa undeploy
```

#<b>III. Serverless & AWS</b>
This time we going to use python flask to build a sample API and use serverless to deploy it into AWS.

##<b>1.Setting Up</b>
- first, let us create our working directories. 

```terminal
mkdir APIwithServerless
cd APIwithServerless
```

- ok now let's install the serverless python plug in.

```terminal
npm install --save serverless-wsgi
```

- then let's build serverless.

```terminal
serverless create --template aws-python3
```
there are many template, you can use serverless create --template --help to see. Since I am using python3.6 so I'm going with above template and after run the command you will get two file in the directories.
1. <b>serverless.yml</b>
2. <b>handler</b>

- Ok , now last thing we need to do is : we need to create an requirements.txt because we are using serverless-plugin and it will looking for that file to install needed library.

```terminal
mkdir requirements.txt
```

- Since we need flask to run the app , now let add Flask to the requirements.txt

```terminal
echo "Flask==0.12.2" >> requirements.txt
```

yes , that it we are done with set up.

##<b>2.Build API</b>
Now it time to build our API. create an .py file and name it <b>createAPI</b>

```python
#File name = "createAPI"

from flask import Flask , jsonify
app = Flask(__name__)

@app.route("/")
def response():
    return jsonify({"result":"success"})

if __name__ == '__main__':
    app.run(debug=True)
```

- now try running the file in our local computer by:

```terminal
export FLASK_APP=createAPI.py
flask run
```
- now you should see the response in your localhost. let deploy it into aws :).

##<b>3.API deploy With Serverless</b>
before we deploy our api we need to configure our <b>serverless.yml</b> file.

- open the file

```terminal
open serverless.yml
```

- Delete default configure and write this


```yml

service: aws-python3 # NOTE: update this with your service name

provider:
  name: aws
  runtime: python3.6

plugins:
  - serverless-wsgi

functions:
  createAPI:
    handler: wsgi.handler
    events:
      - http: ANY {proxy+}
      - http: ANY /

custom:
  wsgi:
    app: createAPI.app
```

- now save the and we finally can deploy it.

```terminal
sls deploy
```
it will take a while and you will see this.

<img width="541" alt="Screen Shot 2018-06-15 at 12.10.39 PM.png" src="https://qiita-image-store.s3.amazonaws.com/0/211896/3257d6b2-ca53-0807-30cc-b4057fceed06.png">

- copy url in the endpoins and paste it in your web browser. You will the our API response :)

##<b>4.Serverless Undeploy</b>
If you are done playing with it and you want to undeploy the project. You can do it like so.

```terminal
sls remove
```