> For the complete documentation index, see [llms.txt](https://gitbook.techbytes.app/devops-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.techbytes.app/devops-docs/tools/serverless-framework.md).

# Serverless Framework

## Serverless Framework

### About

* Serverless Framework is open-source software that builds, compiles, and packages code for serverless deployment and then deploys the package to the cloud.
* It Helps us to focus on Business Logic Rather than software and Hardware
* Developers write Lambda functions, which are triggered by the client app, and user’s requests come through the API Gateway. AWS Lambda provides functions as a service
* We usually wrap up our serverless script in `serverless.yml`

### Installation

* **Prerequisites**:
  * Nodejs
  * AWS: To run serverless commands that interface with your AWS account, you will need to setup your AWS account credentials on your machine.
  * To know more [Click\_Here](https://www.serverless.com/framework/docs/providers/aws/guide/installation/)
* In Linux Follow the Below command to install `serverless-framework`

  ```
  npm install -g serverless
  ```
* Verify Installation:

  ```
  serverless --version
  ```
* The above command Will give you result similar to those below

  ```shell
  Framework Core: 2.20.1
  Plugin: 4.4.2
  SDK: 2.3.2
  Components: 3.5.1
  ```

### Get Started with `Serverless`

An average `serverless.yml` will Look like the below

```yaml
service: slsNewService

frameworkVersion: '2'

provider:
  name: aws
  runtime: python3.8
  lambdaHashingVersion: 20201221

functions:
  helloworld:
  	handler: handler.helloworld
    events:
        - httpApi:
        path: /users/create
        method: get

   environment:
     variable: value

resources:
 Resources:
   NewResource:
     Type: AWS::S3::Bucket
     Properties:
       BucketName: my-new-bucket

```

#### Create Project

You can Get started with the project by generating Templates

**AWS-Python:**

* To generate `AWS-Python` Template for `serverless` , Use the below Command:

  ```sh
  serverless create --template aws-python3 --path MyNewService
  ```
* The Above Command will generate below mentioned two files in `MyNewService` Directory :
  * `serverless.yml`
  * `handler.py`

Similarly For JS ...

**AWS-JS:**

* To generate `AWS-NodeJS` Template for `serverless`, Use the below Command:

  ```shell
  serverless create --template aws-nodejs --path MyNewService
  ```
* The Above Command will generate below mentioned two files in `MyNewService` Directory :
  * `serverless.yml`
  * `handler.js`

To know more about other templates [Click\_Here](https://github.com/serverless/serverless/tree/master/lib/plugins/create/templates)

**Terms in-Detail:**

* `service`: It is simply the name of your project.
* `frameworkVersion`: Framework Version
* `app` & `org` for use with [serverless\_dashboard](https://www.serverless.com/dashboard/)
* `provider` : This section includes configurations related to `cloud provider` like [aws](https://aws.amazon.com/), [azure](https://azure.microsoft.com/en-in/), GCP, etc., (We Talk about AWS Here)
* You can use the `package` and `exclude` configuration for more control over the packaging process, to know more [Click\_Here](https://www.serverless.com/framework/docs/providers/aws/guide/packaging/#exclude--include)
* `functions`: Functions are AWS Lambda functions. You can find all options available on[ this Serverless documentation page](https://serverless.com/framework/docs/providers/aws/guide/functions/).
* `resources`: We can Add CloudFormation resource templates to this

#### Deploy a Service

So to deploy the service to AWS, use the below Command

```shell
serverless deploy
```

#### Remove a Service

In order to remove a service that we've deployed, we can use Below Command

```shell
serverless remove
```

#### Key\_Points

* `Permissions`: If your lambda needs to access other AWS services (S3, SQS, SNS…), you will need to add the proper permissions via the `provider`>`iamRoleStatements` , To know more [Click\_here](https://serverless.com/framework/docs/providers/aws/guide/functions#permissions)

#### References

* [Official\_Docs](https://www.serverless.com/framework/docs/)
* [Learn Serverless](https://www.serverless.com/learn/)
