Before discussing what AWS SAM is, we first need to understand what “serverless technology” means. Here are two key points about serverless technology:

  • Despite being called “server-less”, these technologies are still on servers or containers. 
  • 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. 

Now, let’s become acquainted with the AWS Serverless Application Model.

What is AWS Serverless Application Model?

One of the most popular serverless technologies is AWS Lambda. It is 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 want to test or build your applications using serverless technology, there are several AWS services such as AWS Fargate, Amazon API Gateway, and Amazon 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.

Introduction to AWS Serverless Application Model

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). AWS SAM also supports seven different resource and property types, with more being supported in the future.
  • The CLI also provides several useful commands including launching, building, and deploying applications. 

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,
  • Automate the testing of serverless applications,
  • Are easy to use,
  • Provide a uniform structure for developing serverless applications,
  • Reduce serverless application development and deployment costs.

Demo with AWS SAM

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. Whenever an object is placed in the AWS S3 bucket, the AWS Lambda function triggers and automatically writes the object’s name to the AWS DynamoDB table. Afterward, it sends a notification email to the customer admin. 

Also see: The Best User Management Service: AWS Cognito

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 deploy 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.


During this article, we covered the topic of serverless technology and AWS SAM. We provided a useful example scenario to help you gain a better understanding of how to use AWS SAM, a powerful tool for building serverless applications on AWS. 

Its simplicity, scalability, and cost-effectiveness make it an ideal choice for developers. 

Additionally, we offer a demo source code free of charge, so you can practice and experiment with it.  We hope you enjoy it and continue to explore the cloud with us.

Be sure to check out our DevSecOps services for even more amazing tools and resources. If you’re interested in learning more, visit the PurpleBox Blog section for additional information on this topic.