Introduction to AWS SAM CLI

Posted By :Vikrant Joshi |30th May 2021

 

The Amazon Web Services Serverless Application Model is an open-source framework that benefits you and helps in building serverless applications on AWS freely and easily.

A serverless application is an amalgamation of the following things, Lambda functions, event sources, and many other resources that work together to perform specific tasks. 

 

Be sure such serverless applications are much more than just only a Lambda function; it includes more additional resources such as APIs, databases, and event source mappings.

 

AWS SAM is used to define your serverless applications.

 

AWS SAM consists of the following components:

 

AWS SAM template specification:- 

We use this specification to define our serverless application. It delivers you a sincere and obvious syntax describing the functions, API, permissions, configurations, and events to make up a serverless application. You can use an AWS SAM template file to operate on a single, deployable, versioned unit that's your serverless application. 

 

AWS SAM command-line interface (AWS SAM CLI):- 

We use this tool to prepare serverless applications that AWS SAM templates have defined. The CLI provides you 

  1. Commands that enable you to confirm and check whether the AWS SAM template files are written according to the specification or not
  2. Invoke Lambda functions locally 
  3. Step-through debugging the Lambda functions, packaging and deploying serverless applications to the AWS Cloud, and so on. 

 

Installing the AWS SAM CLI on Linux

 

The AWS Serverless application model CLI is supported on 64-bit OS versions of recent distributions of Fedora, CentOS, Ubuntu, and Amazon Linux 2. To install the AWS SAM CLI, you first have to extract or "unzip" the downloaded package. If your OS doesn't have the built-in unzip command, users will have to use an alternate.

 

To install and configure the prerequisites to use the AWS SAM CLI on the Linux host, you will have to follow these steps:

 

   Step 1: Create an AWS account

 Proceed by creating an AWS account.

 

   Step 2: Configure IAM and AWS credentials

The IAM user, used with AWS SAM, must have all of the required permissions to make the necessary AWS service calls and manage AWS resources. The easiest way to ensure that a user has sufficient permissions is to grant the administrator privileges.

 

  Step 3: Install Docker (optional)

We use the Docker application to run containers on Linux machines. AWS SAM provides a local environment similar to AWS Lambda to use as a Docker container. You can use this container for building, testing, and debugging serverless applications.

To run functions and serverless projects locally with the AWS SAM CLI, one must have Docker installed and working. The AWS SAM CLI utilizes the DOCKER_HOST environment variable to make contact with the Docker. 

 

Docker is available on so many different operating systems, including most modern Linux distributions, for example, CentOS, Debian, and Ubuntu. 

 

Follow these steps to install the Docker, using Amazon Linux 2 :

  1.  Update all of the installed packages and package cache on your instance.

             sudo yum update -y

 

  1. Also, Install the most recent Docker Community Edition package.

            sudo amazon-Linux-extras install docker

 

  1. Start the Docker service now.

           sudo service docker start

 

  1. Add the ec2-user to the docker group to run Docker commands without using the command with Sudo. 

            sudo usermod -a -G docker ec2-user

 

Log out and Log in simultaneously to pick up the new docker group permission.

One must close your current SSH terminal window now and reconnect to your instance in a new one. 

Your new SSH session should have all the appropriate docker group permissions.

 

  1. Verify that if the ec2-user can run Docker commands without using Sudo.

             docker ps

 

Step 4: Install the AWS SAM CLI

 

 

Following are the steps mentioned to install the AWS SAM CLI:

 

  1. First, Download the AWS SAM CLI zip file to a directory of your own choice.

 

  1. Now, generate a hash value using the following command to verify the authenticity and integrity of the downloaded installer files. 

           sha256sum AWS-sam-CLI-Linux-x86_64.zip 

 

The output should exactly look like the following piece of the example given below:

          <64-character SHA256 hash value> AWS-sam-CLI-Linux-x86_64.zip

 

  1. Now, Compare the 64-character SHA256 hash value with one of your own desired AWS SAM CLI version in the release notes of AWS SAM CLI

 

4. Now, Unzip the installation files into the sam-installation/ subdirectory.

           unzip AWS-sam-CLI-Linux-x86_64.zip -d sam-installation

 

Install the AWS SAM CLI using this command.

            sudo ./sam-installation/install

 

  1. Verify the installation process.

            sam --version

 

On successful installation, it should look like the following:

 

        SAM CLI, version 1.18.0

 

You're now ready to start your development.


 


About Author

Vikrant Joshi

Vikrant is a Devops Engineer, he has hands on experience on AWS and he is adaptive and flexible.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us