I heard about the serverless framework so many times , but i didn't really know what it was all about, but last week, i decided to give it a try.
Let's discover it together
First thing : you need to setup your credentials.
If you don't know how to do it, check this link
Install serverless
npm install -g serverless
Create our service in Node JS
serverless create --template aws-nodejs --path test-service
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "/your/folder/path/test-service"
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v2.19.0
-------'
Serverless: Successfully generated boilerplate for template: "aws-nodejs"
cd test-service
Two files have been created :
handler.js serverless.yml
Open the files
Since we are using AWS as a provider, all functions inside the service are AWS Lambda functions.
All of the Lambda functions in your serverless service can be found in serverless.yml under the functions property
# serverless.yml
functions:
hello:
handler: handler.hello
You can add as many functions as you want within this property.
# serverless.yml
functions:
hello:
handler: handler.hello
description: optional description for your Lambda
functionTwo:
handler: handler.functionTwo
functionThree:
handler: handler.functionThree
The handler property points to the file and module containing the code you want to run in your function.
# handler.js
'use strict';
# hello
module.exports.hello = async (event) => {
return {
# the code you want to run
),
};
};
# functionTwo
module.exports.functionTwo = async (event) => {
return {
# the code you want to run
),
};
};
Create and deploy
Let's create and deploy two simple functions.
One function who will say "Hello", and the other one will say "Goodbye" !
# handler.js
'use strict';
module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Hello function',
}
),
};
};
module.exports.goodbye = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Goodbye function',
}
),
};
};
#serverless.yml
service: test-service
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
functions:
hello:
handler: handler.hello
events:
- httpApi:
path: /hello
method: get
goodbye:
handler: handler.goodbye
events:
- httpApi:
path: /goodbye
method: get
Now, to deploy, juste hit this command
serverless deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
...
...
Serverless: Stack update finished...
Service Information
service: test-service
stage: dev
region: us-east-1
stack: test-service-dev
resources: 17
api keys:
None
endpoints:
GET - https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/hello
GET - https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/goodbye
functions:
hello: test-service-dev-hello
goodbye: test-service-dev-goodbye
layers:
None
Now let's check these two endpoints on your browser :
Congrats !
You have now two lambdas running for you on AWS.
Delete ressources
Now , let's clean everything
serverless remove
Serverless: Getting all objects in S3 bucket...
Serverless: Removing objects in S3 bucket...
Serverless: Removing Stack...
Serverless: Checking Stack delete progress...
..................
Serverless: Stack delete finished...
Serverless: Stack delete finished...