What is AWS Serverless Application Model?

Before discussing what is AWS SAM, we first need to understand what “serverless technology” means. As you know, serverless technology is increasingly popular these days. Despite being called “server-less”, these technologies are still on servers or containers. In addition to this, they do not need to be managed. Management of these resources is the responsibility of the cloud provider. We focus solely on our product, writing new codes, and running them successfully. One of the most popular serverless technologies is AWS Lambda; an event-driven compute service that lets us run our code without provisioning or managing servers using the “pay as you go” principle. If you wanted to test or build your applications using serverless technology, there are several AWS services such as AWS FargateAmazon API GatewayAmazon EventBridge.

For building serverless applications, AWS has released a dedicated service called AWS Serverless Application Model (SAM). It’s an open-source framework that helps us to build a new serverless application using just a few lines of configuration and code. Let’s look at AWS SAM in detail!

AWS SAM consists of two parts: AWS SAM Template and CLI (Command Line Interface). The AWS SAM template helps us to create the resources and permissions in the application using Infrastructure as Code (IaC). The CLI also provides several useful commands including launching, building, and deploying applications. AWS SAM also supports seven different resource and property types, with more being supported in the future. You can view the full list here.

Advantages of using AWS SAM

  • There is no server management.
  • Flexible scaling: You can run code for virtually any application or backend service with zero administration.
  • High availability: AWS SAM templates enable developers to deploy serverless applications across multiple regions with high availability.
  • No idle capacity: You only pay for what you use. With serverless, there is no need to pre- or over-provision capacity for things such as computation and storage.

AWS SAM templates enable developers to quickly deploy serverless applications. It automates the testing of serverless applications. AWS SAM templates are easy to use and provide a uniform structure for developing serverless applications. AWS SAM templates enable developers to reduce serverless application development and deployment costs.

Demo with AWS SAM


Example Scenario: We have a customer’s admin who wants to be notified whenever a user uploads a photo to a specific AWS S3 bucket. They also want to save the photo details and user data to the database. What is the simplest and most effective way we can accomplish this?

The solution is simple: We will build a serverless app that consists of S3-triggered Lambda and SNS Topic with AWS SAM. When an object is put in the AWS S3 bucket, the AWS Lambda function will be triggered and automatically writes the name of the object to the AWS DynamoDB table and then notify the customer admin via email.

Step 1: Initialize A Serverless App

  • sam init: With this command, you’ll be able to initialize a new serverless app. When you run the command, AWS will first ask you a few questions about your application. If you choose AWS Quick Start Templates, AWS will provide templates that are built and managed by AWS. However, if you choose “Custom Template”, you can provide your own template. For the purpose of this demo, we will select the first choice and update the default template. There will also be a few other questions for you to answer concerning runtime and package type.

When the initialization was done, you should see something like the below.

Step 2: Update the template

  • Transform: If we look at the template line by line, we should see the “Transform” tag at the beginning. Since AWS SAM is built on top of AWS CloudFormation, this tag indicates to CloudFormation that it is a SAM template, and it is necessary to convert or in other words ‘transform’ the template to build a stack.

After that, we need to create an S3 bucket that automatically triggers the AWS Lambda function whenever a photo is uploaded to the bucket.

TriggeredLambda Function is our Lambda function.

Environment variables are used to get the table and topic name index.js.

The events section is where we define our triggers. This section states that there is an S3 bucket referred to as “S3SourceBucket” which will trigger the Lambda when a photo is uploaded.

Lambda permission is defined to allow S3 Bucket to invoke the function when adding an S3 event.

SampleTable is the DynamoDB table, and the photo name is the partition key.

Step 3: Create the application code

You should now go to the project directory and create a file using the .js extension. This file consists of the application code below.

const tableName = process.env.SAMPLE_TABLE; 
const params = 
TableName: tableName, 
Item: { 
       objectname: event.Records[0].s3.object.key 
} };
//Write the DynamoDB table await dynamodb.put(params).promise(); 
const sns_params = { 
        Message: `Object is added`, 
        Subject: 'Bucket Notification', 
        TopicArn: process.env.SNS_TOPIC } // Send to SNS 
const result = await sns.publish(sns_params).promise() 

Step 4: Build the application with SAM CLI

  • sam build: You need to run this command to make installation dependencies and packaging according to the runtime of the relevant project.

Step 5: Deploy the application

  • sam deploy — guided: You’ll now need to run this command in order to successfully deploy your application. When you deploy your serverless application with SAM for the first time, you should use the “sam deploy — guided” command to configure. It will package and upload the application artifacts to the S3 bucket, then deploys the application using AWS CloudFormation. When you run the command, you will be asked a few questions. These configurations are saved in samconfig.toml file. To save time, you can use the “sam deploy” command in other deployments to prevent having to set up the same configurations every time.

You will receive a confirmation email to subscribe to the SNS topic when the deployment is done successfully. As a quick reminder, you’ll need to confirm your AWS SNS subscription before testing.

Step 6: Test the application

To test the application, you need to upload a file to the S3 bucket from AWS Console or CLI. Then immediately check your email. You should have received an email that looks like the one below:

You should also make sure to check the DynamoDB table whether the photo name is written.


Today, we’ve discussed serverless technology and AWS SAM. We’ve explained in detail why AWS SAM is important, as well as several of its advantages. Also, designed an example scenario using AWS SAM to help you learn more about how to use it. Additionally, we’re sharing the demo source code with you free of charge to practice yourself. If you want to try it, you can access it from here. We hope you enjoy it! Stay with the cloud!

We hope you found our blog post useful. Don’t forget to check out our DevSecOps services!

If you want to read more on this topic, feel free to check out the PurpleBox Blog Section.