
Are you looking for a powerful yet easy-to-use serverless computing platform? If yes, then AWS SAM (Serverless Application Model) is the perfect solution for you. AWS SAM provides developers with an intuitive and easy-to-use platform for creating, deploying, and managing serverless applications.
In this blog, we'll explore the benefits of using AWS SAM and how it can help you unlock the power of serverless computing.
What is AWS SAM?
AWS SAM is a framework for creating serverless applications on AWS. It allows developers to define their application's resources using a simple template file, such as functions and databases. The template file can then deploy the application on AWS with a few clicks.
Benefits of Using AWS SAM
- Simplified Deployment
- Better Resource Management
- Improved Monitoring and Debugging
- Cost Optimization
- Increased Productivity
Overall, AWS SAM is a powerful tool for unlocking the full potential of serverless computing. Now I think we have seen Serverless Application Model's theoretical concept. Let's go through some code and dive into it.
Setup SAM on Desktop
To set up SAM on your desktop you must have AWS CLI configured. If not I would suggest going through this documentation. Once you are done with that, install a compatible SAM version for your machine from AWS documentation.
Create Your First SAM Project
Note: Either you can use the SAM init and follow the terminal option to create your project or you can follow along with me where I will demonstrate building a production-ready SAM project from scratch.
Following is a high-level view of the serverless application that we are going to build.

Now follow the below steps to build the serverless application and unlock the power of serverless computing.
Step 1: Create a New Directory
Create a new directory for your application and navigate to it in the terminal or using your favorite code editor like VS Code.
Step 2: Create the Lambda Function
Create a folder ServerlessApiHandler where our lambda function code will lie. Inside this folder, create a file main.py (or index.js for Node.js) and write down the below code in the file. You can modify your application code as per your need.
exports.lambdaEventHandler = async function(event) {
const user = {
"name": "kodeweich",
"email": "kodeweicher@gmail.com"
}
return {
statusCode: 200,
body: JSON.stringify({ user: user })
};
};
Step 3: Create a SAM Template File
Now create a SAM template file, which is the heart of SAM. Name it template.yaml, which defines the resources that are going to deploy on your AWS server.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Serverless Application Model Article
Parameters:
ServerStage:
Type: String
Default: "test"
AllowedValues:
- "test" # for development environment
- "v1" # for production environment
Globals:
Function:
Timeout: 5
Resources:
ServerlessApplicationApi:
Type: AWS::Serverless::Api
Properties:
Name: "ServerlessApplicationApi"
StageName: !Ref ServerStage
Description: 'Serverless Application Api'
EndpointConfiguration:
Type: REGIONAL
Cors:
AllowMethods: "'GET, POST, OPTIONS, DELETE, PUT'"
AllowOrigin: "'*'"
AllowHeaders: "'Content-type, Authorization, X-APP-TOKEN'"
# AWS IAM Role to allow lambda to PUT its logs on CLOUDWATCH.
ServerlessApiHandlerExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: ServerlessApiHandlerExecutionRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: LambdaExecutionandLogPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: logs:CreateLogGroup
Resource: "*"
- Effect: Allow
Action:
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
# A Lambda handler that will be invoked using the API URL
ServerlessApiHandler:
Type: AWS::Serverless::Function
Properties:
FunctionName: ServerlessApiHandler
CodeUri: ServerlessApiHandler/
Handler: index.lambdaEventHandler
Runtime: nodejs18.x
Role: !GetAtt ServerlessApiHandlerExecutionRole.Arn
Events:
ApiEvent:
Type: Api
Properties:
RestApiId: !Ref ServerlessApplicationApi
Path: /user
Method: GET
Outputs:
ServerlessApplicationApi:
Description: "API Gateway endpoint URL for the Serverless Application"
Value: !Sub "https://${ServerlessApplicationApi}.execute-api.${AWS::Region}.amazonaws.com/${ServerStage}"
ServerlessApiHandlerFunctionArn:
Description: "Serverless Api Handler Lambda Function ARN"
Value: !GetAtt ServerlessApiHandler.Arn
Template File Breakdown
The SAM template file is structured as follows:
Deployment and Testing
Once you have created the template and Lambda function, you can deploy your serverless application using the SAM CLI:
sam build
sam deploy --guided
The sam build command packages your application, and sam deploy handles the deployment to AWS. The --guided flag will ask you for deployment parameters interactively.
After deployment, you can test your API endpoint using curl or Postman by accessing the URL provided in the outputs.
Conclusion
AWS SAM simplifies serverless development by providing a clean, concise way to define and deploy serverless applications. The combination of Lambda, API Gateway, IAM roles, and CloudWatch logging creates a complete serverless application architecture. By following this guide, you've learned how to set up a production-ready serverless application using AWS SAM.