A serverless application can be built using services provided by AWS such as AWS Serverless Application Model (SAM). AWS provides AWS SAM CLI for developing applications based on SAM. It facilitates an execution environment similar to Lambda for building, testing and debugging applications provided by SAM templates. Also, using AWS SAM CLI, we can deploy the SAM application to AWS.
What will we cover?
In this guide we will explore various features of AWS SAM CLI and see how to install it on Amazon Linux.
What is AWS SAM?
AWS SAM is used for developing serverless applications on AWS. Both SAM and SAM CLI are under an open-source license. SAM defines the resources used in serverless applications. It has short syntaxes to denote APIs, functions, databases, and event source mappings. The SAM syntax is transformed and expanded into AWS CloudFormation syntax. Applications can be modeled using YAML. In this way serverless applications can be built rapidly.
Features of AWS SAM
The AWS SAM can be integrated with other AWS services and thus has several benefits for developing serverless applications:
- Using AWS SAM, organization of corresponding components and resources is simplified. One can use it to distribute configuration among resources and all corresponding resources can be deployed collectively.
- While deploying, the SAM syntaxes are expanded into native AWS Cloudformation syntaxes. This provides us with the option of reliable deployment with CloudFormation.
- We can develop serverless applications in many runtime languages. With the SAM CLI, we can locally build; pilot run; and fix the serverless applications. Also, the SAM CLI can be integrated with AWS provided toolkits.
- AWS SAM CLI can be installed on many major Linux based distros, Mac and Windows.
Installing the AWS SAM CLI
Using the AWS SAM CLI tool, we can create; build; test; and debug serverless applications defined by SAM templates. For using SAM CLI, we need to install and set a few things. The AWS SAM CLI is available for 64-bit processor architecture of recent versions of CentOS, Fedora, Ubuntu, and Amazon Linux 2. In order to have a working SAM CLI on Amazon Linux system follow the steps below:
1. Download the SAM CLI package from here and extract this package to the sam-installation directory:
unzip aws-sam-cli-linux-x86_64.zip -d sam-installation
2. Use the IAM user credentials to configure AWS CLI on your local computer. To simplify the permission section, attach the ‘AdministratorAccess’ policy to this IAM user.
3. To test your serverless applications locally, install docker on your system. To install Docker on Amazon Linux, run the following commands:
sudo amazon-linux-extras install docker
4. To start the docker service, run the command below:
5. By adding the ec2-user to the docker group, we can avoid typing ‘sudo’ every time:
Now simply login and logout to set the permissions for docker group.
6. Install SAM CLI on Amazon Linux by running the command:
7. After the installation, check the installed version:
Use the same way to update existing installation but just add the ‘–update’ argument to Step 6:
Uninstalling the SAM CLI
Uninstalling the SAM CLI on linux requires 2-3 steps.
Step 1. First locate the symlink for SAM CLI using the which command:
Step 2. Using the ‘ls’ command, locate the directory the above symlink is pointing to:
Step 3. Remove the symlink and the installation directory using the commands:
sudo rm -rf /usr/local/aws-sam-cli
Important SAM CLI Commands
- sam-init: SAM CLI tool can initialize serverless applications using the AWS infrastructure templates. This is done using the ‘sam-init’ command.
- sam-build: In the same way, use the ‘sam-build’ command for compiling, building and packaging Lambda functions with the given runtime.
- sam-local: With Docker containers we can locally test a SAM application by running the ‘sam local’ commands.
- sam sync: The ‘sam sync’ command provides a feature to synchronize and verify the modifications done in the cloud environment.
- sam-deploy: Finally, deploy your SAM application by running the ‘sam deploy’ command.
In this guide we have learned about AWS SAM, how to install and uninstall AWS SAM CLI. We also tried building and deploying a simple ‘Hello World’ application with SAM CLI. Enhancing SAM CLI documentation was also discussed.