Serverless FrameworkServerless Framework The Serverless Framework is a CLI tool that allows users to build & deploy auto-scaling, pay-per-execution, event-driven functions. Write your code, configure events to trigger your functions, then deploy & run those functions to your cloud provider via the Serverless CLI. You can create and run serverless service at the different platforms. These are aws, google cloud, azure, kubeless, spotinst, fn and cloudflare. You must decide one of these first. There are many plugins and resources in the serverless framework such as websocket, graphql, appsync, cognito, dynamodb, mongodb, appsync, bucket, tensorflow etc.
- Serverless framework installed on your machine:
npm install -g serverless
- Set-up your Provider Credentials:
serverless config credentials –provider aws –key your-access-key-ıd –secret your-secret-key
sls config credentials –provider aws –key your-access-key-ıd –secret your-secret-key
- Create a new service:
serverless create –template aws-nodejs –path your-project-name
Available Templates :
- Deploy the service: After make the your changes on the service.yaml
serverless deploy | sls deploy
- Cleanup: If at any point, you no longer need your service then you can remove the service from cloud
serverless remove | sls remove1. Services : A service is like a project. It’s where you define your Lambda Functions, the events that trigger them and any infrastructure resources they require, all in a file called serverless.yml.Each service configuration is managed in the serverless.yml file. Example: Declare a Serverless service, defining functions, events, provider, plugins, database tables, cognito ect…
2. Functions : All functions inside the service are Lambda functions.All of the Lambda functions in your serverless service can be found in serverless.yml under the functions property.
The handler property points to the file and module containing the code you want to run in your function
3. Events : Events are the things that trigger your functions to run. All events in the service are anything that can trigger an Lambda function.
4. Resources : Resources are the things that your services uses in itself infrastructures. (Db, Bucket, Cognito etc…) Using the Serverless Framework, you can define the infrastructure resources you need in serverless.yml
You must set the permissions of serverless service resources.You can make ones at the iamRoleStatements under provider in the .yml file.
5. Variables : Variables allow users to dynamically replace config values in serverless.yml config.They are especially useful when providing secrets for your service to use and when you are working with multiple stages.
npm install –save <custom-serverless-plugin>
Folder Structure of Serverless ProjectOur serverless project start to grow in time. And for this we must determine the folder structure our serverless project. The folder structure of our serverless project is likes this.