In this article, we will learn the concept of the AWS SAM CLI. This is a part of the three-article series “Develop and Deploy Serverless Applications with AWS SAM CLI”. SAM, abbreviated for Serverless Application Model is a framework provided by Amazon Web Services, which can be leveraged to build applications on the local machine and deploy those to the AWS Lambdas directly.
Previously, before the introduction of the AWS SAM CLI, developers used to write, test, and deploy codes directly on the AWS Lambda using the online editor. This was done by using the AWS Management Console to create a Lambda Function. And when the function is created, you can add your code in there using the online code-editor as provided by AWS Lambda.
Figure 1 – Writing code within the inline Code Editor (AWS Lambda)
Although, this seems to be quite a handy way of writing and testing your code without having to deploy, and it is fine if you have really simple functions that don’t include much complex logic in it or you are working on it individually. However, the idea of having your code inside an online code editor sounds quite intimidating to me. You do not have complete control over your code in this process. If by any chance, someone deletes your function accidentally, then you have to rewrite the code again from scratch, and that is something we, as developers, should really avoid.
Instead, in this era of Infrastructure as Code, where most of the things can be implemented by writing code, we should think of a more robust idea where the code can be version controlled and easily managed. There are a lot of tools out there in the market which you can use to write and develop your function, and once it is ready for deployment, you can deploy it to the online service using the native process as provided by AWS Lambda.
Understanding Serverless Functions and Applications in AWS
Serverless Functions or Lambda Functions in AWS is a piece of code that can be written in any of the supported languages like C #, Python, NodeJS, Ruby, etc. This code can be deployed on AWS and executed based on a trigger event. There are many supported events in AWS like the API Gateway, which can be used to trigger a Lambda Function. Apart from this, there are also other events like S3 events, SNS Topics, SQS Queues, etc. All these events can be used to trigger a Lambda Function to execute.
Serverless Applications, on the other hand, is a combination of similar services which work towards fulfilling a specific goal. Such an application might consist of one or more than one Lambda Functions, in combination with other AWS services like the DynamoDB tables or SNS Topics.
What is AWS SAM – Serverless Application Model
SAM is an open-source framework developed by Amazon, which helps us define the serverless resources like Lambda Functions, API Gateways along with a few others. The SAM provides access to the API using a CLI, known as the SAM CLI. With the help of this utility, you can easily define your resources and get those deployed to any environment in the AWS without doing much manual work. The SAM is based on the previously built Cloud Formation Templates. If you already have some experience with deploying serverless resources with Cloud Formation Stack, then using the AWS SAM CLI will be quite easy for you.
Now that we have some idea about AWS SAM, let us go ahead and try to see the underlying workflow.
Figure 2 – AWS SAM Workflow
If you see the figure above, you can see that there are two primary sections – one being the “Local App Repo” and the second is the “AWS Cloud”. In the local repo, there is the root directory “my-sls-app-1”, which resembles a serverless application. Inside that application, there is another directory “lambda-1”, which is the directory for the lambda function. Under the lambda function directory, I have my “main.py” file, in which I can write my code for the lambda function. An important point to note here is that under the root application directory, there can be more than one lambda function and other AWS resources defined. However, for the sake of simplicity, I would like to limit it to one lambda function only.
The next item you can see is the “sam-template.yaml”, which is the template that will be used the SAM framework to deploy the application. I will explain it later in the series. This template is somewhat similar to the Cloud Formation template, with some minor modifications in it. If you are familiar with the Cloud Formation templates, then you can work with this one easily. Once you have developed your code, you can run the “sam-package” command, which will use this template as an input to the SAM framework, and then convert it to a Cloud Formation Template and store it with the name “sam-deploy.yaml”. In addition to generating the deploy template, the SAM framework also creates a zip of the lambda function and uploads it to a pre-defined bucket in S3. The URL for this uploaded file will be present in the deploy template for reference.
Once the deploy template is ready, you are now good to deploy the resources into AWS Cloud. In order to do that, you need to run the “sam-deploy” command, which will take the deploy template as an input and create a Cloud Formation Stack based on the resources like AWS Lambda Functions, API Gateways, etc. that were specified on the SAM template file. This deployment can not only be used to deploy Lambda Functions but also other services like API Gateway, SNS Topics, SQS Queues, Dynamo DB tables, etc. This is all about creating and deploying your serverless application.
Benefits of using AWS SAM
AWS SAM is able to integrate with other AWS Services and thus offers us many benefits that we can leverage.
- Single Deployment Configuration – By using AWS SAM, you can easily manage all your necessary resources at one single place that belongs to the same stack
- Extension of AWS Cloud Formation – Since the SAM is built as an extension of the Cloud Formation already available, you can get reliability on the deployment capabilities of the same. With this, you are also able to use all of the resources that are available in Cloud Formation in the SAM YAML template
- Local Debugging and Testing – With the help of AWS SAM CLI, you can now execute and test your serverless applications on your local. The CLI provides a Lambda like execution environment on your local by mounting a docker image and runs the code. This helps you to easily step through the code and find out issues that might not be detected while running the code on the lambda environment from the console directly
Installing the SAM CLI
So far, we have learned reasonably about the AWS SAM. Let us now go ahead and learn how we can install the same on our local machine. Assuming that you have python already installed on your machine, you can just install the CLI by running the following command.
pip install aws–sam–cli
Figure 3 – Installing the SAM using PIP
I have already installed the same on my machine, so I get the message as “Requirement already satisfied”. However, if you are installing it for the first time, it will download the SAM package and all the necessary dependencies as required.
In this article, we have seen what the AWS Serverless Application Model (SAM) is about and how it can be used to develop serverless applications. Further, we have also learned about its workflow and how to package and deploy serverless applications. In the next articles in this series, I will demonstrate the debugging and testing of a serverless application locally and how we can package and deploy the same using the SAM CLI.