# Serverless Framework

## <mark style="color:red;">Introduction</mark>

**Serverless Framework** allows developers to deploy auto-scaling, pay-per-execution, event-driven functions.&#x20;

#### <mark style="color:purple;">The advantages include:</mark>

* No server management
* Cost-effectiveness
* Scalability
* Developer productivity

**FastAPI** is a modern web framework for building APIs with Python, known for its fast performance and built-in support for data validation and interactive documentation.

## <mark style="color:red;">Setting Up Serverless Framework</mark>

#### <mark style="color:purple;">Installation:</mark>

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

#### <mark style="color:purple;">Authentication:</mark>

For AWS, set up the AWS CLI and configure your credentials:

```bash
aws configure
```

#### <mark style="color:purple;">For Azure, set up the Azure CLI and log in:</mark>

```bash
az login
```

## <mark style="color:red;">Deploying FastAPI on AWS Lambda</mark>

#### <mark style="color:purple;">Create a New Project:</mark>

```bash
serverless create --template aws-python3 --path my-fastapi-app
cd my-fastapi-app
```

### <mark style="color:yellow;">FastAPI Setup</mark>

#### <mark style="color:purple;">Install FastAPI and Uvicorn:</mark>

```bash
pip install fastapi uvicorn
```

#### <mark style="color:purple;">Your application (</mark><mark style="color:purple;">`app.py`</mark><mark style="color:purple;">):</mark>

```python
from fastapi import FastAPI

app = FastAPI()

@app.get("/")

def read_root():
    return {"Hello": "World"}
```

#### <mark style="color:purple;">Serverless Configuration (</mark><mark style="color:purple;">`serverless.yml`</mark><mark style="color:purple;">):</mark>

```yaml
service: my-fastapi-app

provider:
  name: aws
  runtime: python3.8

functions:
  api:
    handler: handler.app
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'
```

#### <mark style="color:purple;">Deploy:</mark>

```bash
serverless deploy
```

## <mark style="color:red;">Deploying on Azure and Other Platforms</mark>

While AWS is widely used, Serverless Framework also supports Azure, Google Cloud, and more.

### <mark style="color:yellow;">Azure</mark>

#### <mark style="color:purple;">Update your</mark> <mark style="color:purple;"></mark><mark style="color:purple;">`serverless.yml`</mark><mark style="color:purple;">:</mark>

```yaml
provider:
  name: azure
  location: West US
  runtime: python3.6

functions:
  hello:
    handler: handler.hello
    events:
      - http: true
        x-azure-settings:
          authLevel: anonymous
```

## <mark style="color:red;">Features, Tips, and Tricks</mark>

1. <mark style="color:purple;">**Local Development**</mark><mark style="color:purple;">:</mark> Use `serverless-offline` plugin for simulating AWS Lambda & API Gateway locally.
2. <mark style="color:purple;">**Environment Variables**</mark><mark style="color:purple;">:</mark> Manage with the `serverless-dotenv-plugin`.
3. <mark style="color:purple;">**Middleware**</mark><mark style="color:purple;">:</mark> The `serverless-middleware` plugin allows you to add middlewares before your lambda function is executed.
4. <mark style="color:purple;">**Optimizing Dependencies**</mark><mark style="color:purple;">:</mark> Use `serverless-python-requirements` to automatically package only the required dependencies.
5. <mark style="color:purple;">**Warm-Up**</mark><mark style="color:purple;">:</mark> AWS Lambdas can sometimes have cold starts. Use `serverless-plugin-warmup` to mitigate this.
6. <mark style="color:purple;">**Fine-tune your IAM roles**</mark> for your functions in `serverless.yml` to adhere to the principle of least privilege.
7. <mark style="color:purple;">**Automate Documentation**</mark><mark style="color:purple;">:</mark> FastAPI provides automatic interactive API documentation using Swagger.

## <mark style="color:red;">Extended Management Commands</mark>

#### <mark style="color:purple;">**Deploy to a Specific Stage**</mark><mark style="color:purple;">:</mark>&#x20;

By default, the deployment goes to the `dev` stage.

```bash
serverless deploy --stage production
```

#### <mark style="color:purple;">**Remove a Specific Stage**</mark><mark style="color:purple;">:</mark>

```bash
serverless remove --stage <stage-name>
```

#### <mark style="color:purple;">**Specifying a Region**</mark><mark style="color:purple;">:</mark>

```bash
serverless deploy --region us-west-1
```

#### <mark style="color:purple;">**Function Deployment**</mark><mark style="color:purple;">:</mark>&#x20;

Instead of deploying the entire stack, deploy a single function.

```bash
serverless deploy function --function <function-name>
```

#### <mark style="color:purple;">**View Function Configuration**</mark><mark style="color:purple;">:</mark>

```bash
serverless info --function <function-name>
```

#### <mark style="color:purple;">**Invoke a Function Remotely**</mark><mark style="color:purple;">:</mark>

```bash
serverless invoke --function <function-name> --log
```

#### <mark style="color:purple;">**Print Out the Serverless Configuration**</mark><mark style="color:purple;">:</mark>

This prints out the configuration that will be sent to the provider.

```bash
serverless print
```

#### <mark style="color:purple;">**Serverless Dashboard**</mark><mark style="color:purple;">:</mark>&#x20;

After setting up an account on Serverless Dashboard, you can monitor, troubleshoot, and test your serverless application.

```bash
serverless dashboard
```

#### <mark style="color:purple;">**Serverless Plugin Management**</mark><mark style="color:purple;">:</mark>&#x20;

Install and manage plugins directly from the CLI.

```bash
serverless plugin install --name <plugin-name>
serverless plugin uninstall --name <plugin-name>
```

#### <mark style="color:purple;">**Config Credentials**</mark><mark style="color:purple;">:</mark>&#x20;

Set up the Serverless Framework with your account credentials, useful if you didn’t do it during the setup.

```bash
serverless config credentials --provider <provider-name> --key <key> --secret <s
```
