SAM CLI - Serverless Application Model , is a command line tool provided by AWS to easily create and manage serverless Applications.
Serverless Framework – Serverless Framework is also a tool similar to SAM but it is not limited to AWS, Serverless Framework can be used to easily create and manage serverless applications from various providers like AWS, Azure, GCP & more.
I will be listing the main differences I found between these two when used with AWS, which may help you to choose one of these.
Installation
SAM
You need to follow Operating System specific installation steps as there are multiple dependencies for SAM which are installed differently on different Operating systems. Below are some general steps to be followed irrespective of Operating System.
- Create an AWS account.
- Configure IAM permissions.
- Install Docker. Note: Docker is only a prerequisite for testing your application locally.
- Install Homebrew.
- Install the AWS SAM CLI.
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html
Serverless
For Serverless you need to follow Operating
System specific instructions only for installing Node.js and if you have Node.js
installed on your machine already, then using single command below your
Serverless
installation will be ready
‘npm install -g serverless
’.
- Install Node.js
- Install Serverless (
npm install -g serverless
)
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html
Resource Definition
SAM
SAM being an AWS specific framework, it supports managing most of the AWS resources out of the box and you can expect it to be always in sync with AWS service updates.
- Resources are defined in a file called template.yaml
- More verbose hence produces more lines of code compared to serverless
- Resources are created and managed by a cloudformation stack
Eg: Defining an API Gateway endpoint in SAM
Events:
GetApiEndpoint:
Type: Api
Properties:
Path: /items
Method: GET
Serverless
Serverless being a generic framework for multiple serverless platforms , it may not support all AWS services and it will always take some time to get the latest updates with respect to changes in latest AWS services.
- Resources are defined in a file called serverless.yml
- More readable and less code compared to SAM
- Resources are created and managed by a cloudformation stack
Eg: Defining an API Gateway endpoint in Serverless
events:
-http:
path: /items
method: GET
Local Development
SAM
Can simulate API Gateway and Lambda locally without any extra dependencies and these two services are the most common requirements to run a serverless application in AWS.
- Builtin support for basic use cases.
- We can run local-dynamodb using docker and connect to SAM
- We can test lambda functions individually using
‘
invoke
’ command, which is very useful for local development
sam local invoke [OPTIONS] [FUNCTION_IDENTIFIER]
(https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-invoke.html) - We can start local server using command –
sam local start-api
(https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-start-api.html)
Serverless
Needs extra plugins to be installed for most of the basic requirements like local development server, creating nested stacks, etc.
- Need to install a plugin serverless-offline to emulated lambda and API Gateway on your machine
- ‘
sls-offline
’ is the command used to start local development server
(https://www.npmjs.com/package/serverless-offline) - We can invoke functions locally similar to SAM using
‘
invoke local
’ command
serverless invoke local --function functionName
(https://www.serverless.com/framework/docs/providers/aws/cli-reference/invoke-local/)
Deployment
SAM
On initial versions of SAM we had to run 2 different commands to build, upload and deploy and it did not support features like one step deployment and automatic deployment bucket creation which was supported in serverless framework at the same time. Latest version of SAM supports single command deployment similar to Serverless.
- Deployment steps for versions before 0.33.1
-
sam package --s3-bucket my-regional-bucket --output-template-file out.yaml
-
sam deploy --template-file out.yaml --capabilities CAPABILITY_IAM --stack-name MyStackName
-
- Deployment after version 0.33.1
Serverless
We can deploy using single command ‘sls’ deploy and S3 bucket creation is taken care automatically, we can also deploy or update single function in a stack individually which is not supported by SAM.
- Deploy entire stack
sls deploy
- Deploy single function
sls deploy function --function myFunction
(https://www.serverless.com/framework/docs/providers/aws/guide/deploying/)
Conclusion
Serverless and SAM both have pros and cons based on various scenarios in which they are used , so based on these observations , it is better to go with SAM if you are a developer who will be working with only AWS cloud. It is better to choose Serverless framework if you are a full time devops engineer or cloud developer who will be managing and working with different serverless providers at the same time because you can use a single framework and make use of vast community and documentation.