Serverless Framework is a tool designed to streamline the development and deployment of serverless applications, including functions and infrastructure, by abstracting away the need to manage servers.
We define desired infrastructure in serverless yaml files and then deploy it by executing:
sls deploy
This command parses serverless yaml file into larger AWS CloudFormation template which automatically gets filled with values from the yaml.
Serverless Yaml Configuration File
serverless yaml file defines a serverless service. It is a good idea to break up the serverless project into multiple services, each of which is defined by its own serverless yaml file. We don't want to have everything in one big infrastructure stack.
Example:
- database e.g. DynamoDB
- Rest API e.g. which handles the submitted web form and stores data in DynamoDB
- front-end website which e.g. stores React app website in s3 bucket
Services can be deployed in multiple regions. (Multi-region architecture is supported)
serverless.yml example:
service: my-service
frameworkVersion: "3"
useDotenv: true
plugins:
- serverless-plugin-log-subscription
- serverless-dotenv-plugin
provider:
name: aws
runtime: nodejs14.x
region: eu-east-1
memorySize: 512
timeout: 900
deploymentBucket:
name: my-serverless-deployments
vpc:
securityGroupIds:
- "sg-0123cf34f6c6354cb"
subnetIds:
- "subnet-01a23493f9e755207"
- "subnet-02b234dbd7d66d33c"
- "subnet-03c234712e99ae1fb"
iam:
role:
statements:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource: arn:aws:lambda:eu-east-1:123456789099:function:my-database
package:
patterns:
- "out/**"
- "utils.js"
- "aws-sdk"
functions:
my-function:
handler: lambda.handler
events:
- schedule:
name: "my-service-${opt:stage, self:provider.stage}"
description: "Periodically run my-service lambdas"
rate: rate(4 hours)
inputTransformer:
inputTemplate: '{"Records":[{"EventSource":"aws:rate","EventVersion":"1.0","EventSubscriptionArn":"arn:aws:sns:eu-east-1:{{accountId}}:ExampleTopic","Sns":{"Type":"Notification","MessageId":"95df01b4-1234-5678-9903-4c221d41eb5e","TopicArn":"arn:aws:sns:eu-east-1:123456789012:ExampleTopic","Subject":"example subject","Message":"example message","Timestamp":"1970-01-01T00:00:00.000Z","SignatureVersion":"1","Signature":"EXAMPLE","SigningCertUrl":"EXAMPLE","UnsubscribeUrl":"EXAMPLE","MessageAttributes":{"type":{"Type":"String","Value":"populate_unsyncronised"},"count":{"Type":"Number","Value":"400"}}}}]}'
- sns:
arn: arn:aws:sns:us-east-2:123456789099:trigger-my-service
- http:
custom:
dotenv:
dotenvParser: env.loader.js
logSubscription:
enabled: true
destinationArn: ${env:KINESIS_SUBSCRIPTION_STREAM}
roleArn: ${env:KINESIS_SUBSCRIPTION_ROLE}
- provider
- vpc
- securityGroupIds
- subnetIds - typically a list of private subnets with NAT gateway.
- functions - defines the AWS Lambda functions that are deployed as part of this Serverless service. This is where we define the AWS Lambda functions that our Serverless service will deploy.
- <function_name> (e.g., my-function) Each function entry under functions specifies:
- handler - tells Serverless which file and exported function to execute as the Lambda entry point (e.g., lambda.handler)
- events - a list of events that trigger this function, such as:
- schedule: for periodic invocation (cron-like jobs)
- sns: for invocation via an AWS SNS topic
- http