- Install serverless on your machine
sudo npm i serverless -g
- Configure AWS credentials in your system using AWS CLI refer documentation to install and configure AWS CLI.
- To create serverless project use command
serverless
select required template and start with the project - Edit your yaml file according to your configuration need
- Deploy the function -
serverless deploy -v
To deploy only specific function -serverless deploy -f <fun_name>
To deploy specific stage -sls deploy --stage dev
- Invoke http end point api using API Gateway.
1. Install plugin serverless-offline
as dev dependency
2. Add serverless-offline
plugin in the plugins section of the serverless.yaml
file
3. Run the command sls offline start --httpPort 5000
- Initiate serverless project with starter template
- Install
typescript
,node
andserverless-plugin-typescript
as dev dependecies in your project - Add
serverless-plugin-typescript
plugin in the plugins section on the yaml file - Organize your folder structure according to you need.
- Install
@types/aws-lambda
dev dependencies to work with types in your code. refer code block in the repo to see more - Then normal deployment and invocation of the api end point, typescript plugin will handle all the conversion related to javascript to typescript.
Serverless layer helps to easily share code and dependencies among the multiple lambda functions, so we can generate very lean code artifact. we can include upto 5 layers in the lambda functions, see example 04-lambda-layer-js
folder to know more about the layers
- creating layer
- folder structure
- how to test layer locally using env variables
using serverless-dotenv-plugin
we can add environment variable easily in serverless functions, add this package as a dependecy in serverless plugins.
package automatically detects the variables .env file and load them into the serverless functions we can access those variable as ${env:ENV_NAME}
in yaml file
-
Create an API gateway manually in the console.
-
copy
restApiId
andrestApiRootResourceId
-
Configure api gateway in
serverless.yaml
fileapiGateway: restApiId: aabdcyfq1f restApiRootResourceId: v3q8b97do8
-
Then adding all those events under functions will create and api under the given gateway ID
-
See example to know more (04-lambda-layer-js)
If we want to create separate environment for dev and prod use the different stage in serverless.yaml
file, it will create separate lambda function, API gateway and also the cloud formation stack.
But creating multiple stack for dev and prod is not great solution to isolate work environment and also difficult to manage the severless resources in separate stack.
Note: Please refer 05-lambda-env-deploy
to know more about the separate deployment and also to know about the serverless variable.
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: "20201221"
region: "us-east-1"
stage: ${self:custom.stage}
variables:
self: To refer variable value in serverless.yaml
file manifest
opt: To refer the variable pass through the CLI.