# Serverless Framework

## Introduction

In this series, we’ll focus on **Serverless Framework**, one of the fastest ways to build and deploy serverless applications.

If you’ve already worked with **Terraform**, you know how great it is for infrastructure automation. So why another tool?  
Because **Serverless Framework** is built specifically for deploying **serverless apps** — it abstracts away the heavy lifting of infrastructure management and lets you deploy complex setups in just a few lines of configuration.

Just like Terraform, Serverless is **cloud-agnostic** — it supports **AWS**, **GCP**, and **Azure** out of the box.

👉 [Official website](https://www.serverless.com/)

---

## Setting Up

Refer to the [official setup guide](https://www.serverless.com/framework/docs/getting-started) for installation instructions.

If you’re on Windows, consider using **WSL (Windows Subsystem for Linux)** — it makes development smoother and closer to a native Linux experience, especially when working with AWS or Python.

Once Serverless is installed, it can use your **existing AWS CLI credentials** automatically.  
If you already have AWS CLI configured with a default profile, you don’t need extra setup.

However, if you haven’t configured AWS yet, here’s how:

1. Go to **IAM → Users** in AWS Console
    
2. Create a new user named `serverless`
    
3. Attach the **AdministratorFullAccess** policy (for simplicity — use least privilege in production)
    
4. Create an **Access Key** and **Secret Key**
    
5. Configure credentials locally:
    

```bash
serverless --version
serverless config credentials --provider aws --key <yourKey> --secret <yourSecret>
```

---

## Test Setup

To confirm everything works, let’s deploy a simple Lambda function.

### Create a Test Project

```bash
serverless create --template aws-python --path new_project --name testFunc
```

This will generate a project structure like:

```plaintext
new_project/
├── handler.py
└── serverless.yml
```

### Minimal `serverless.yml`

Remove comments to simplify it:

```yaml
service: testFunc
frameworkVersion: '3'

provider:
  name: aws
  runtime: python3.8

functions:
  hello:
    handler: handler.hello
```

### Deploy

Now deploy the function:

```bash
sls deploy
```

Once deployed, you’ll see a confirmation similar to this:

```plaintext
Deploying testFunc to AWS...
Service Information
stage: dev
region: us-east-1
stack: testFunc-dev
```

To remove everything later:

```bash
sls remove
```

🎉 **Congratulations!** You just deployed your first serverless function.

---

## Invoking the Function

Testing locally or through the CLI is straightforward:

```bash
# Invoke and view output
sls invoke -f hello --log

# Stream recent logs
sls logs -f hello --tail
```

### Sample Output

```bash
$ sls invoke -f hello --log
{
  "statusCode": 200,
  "body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
--------------------------------------------------------------------
START
END Duration: 1.33 ms (init: 130.95 ms) Memory Used: 38 MB
```

---

## Using External Libraries

Most real-world Lambdas depend on external libraries (like `pandas` or `requests`).  
Serverless makes it easy to package and deploy them in two ways:

### Option 1 — Include Dependencies as a Zip

Bundle dependencies directly into your deployment zip.

### Option 2 — Use Lambda Layers

Upload dependencies as a **Lambda Layer** to share across multiple functions.  
Learn more: [AWS Blog – Lambda Layers](https://aws.amazon.com/blogs/compute/using-lambda-layers-to-simplify-your-development-process/)

---

### Using the `serverless-python-requirements` Plugin

Install the plugin:

```bash
sls plugin install -n serverless-python-requirements
```

Then update `serverless.yml`:

#### Using Zip Packaging

```yaml
plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: true
    zip: true
```

For zipped dependencies, include this at the top of your Python code:

```python
import unzip_requirements
```

#### Using Lambda Layers

```yaml
plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: true
    layer:
      name: PandasLayer
```

Define dependencies in `requirements.txt`, for example:

```plaintext
pandas==1.0.1
```

Serverless will automatically build and deploy them as a Lambda Layer.

---

## Summary

The **Serverless Framework** offers a fast, cloud-agnostic way to create and manage serverless applications.  
It simplifies deployment, testing, and packaging, letting developers focus on code — not infrastructure.

👉 Check out the GitHub repository for examples:  
[github.com/k-pulkit/source-demo-serverless-framework](https://github.com/k-pulkit/source-demo-serverless-framework/tree/main)
