AWS Serverless Application Model (SAM)
AWS SAM is an open-source framework that you can use to build serverless applications on AWS.
A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. Note that a serverless application is more than just a Lambda function—it can include additional resources such as APIs, databases, and event source mappings.
AWS SAM consists of the following components:
AWS SAM template specification.
You use this specification to define your serverless application. It provides you with a simple and clean syntax to describe the functions, APIs, permissions, configurations, and events that make up a serverless application. You use an AWS SAM template file to operate on a single, deployable, versioned entity that's your serverless application.
AWS SAM CLI.
Use the AWS SAM CLI to create a serverless application that you can package and deploy in the AWS Cloud. You can run the application both in the AWS Cloud or locally on your development host. This lab walks you through the steps to create, build and deploy the sample node.js Hello World application using AWS Serverless Application Model (SAM).
Task 1: Launch Instance from AWS Instance and SSH into it. (Help)
//Switch to the root user sudo su //Update the EC2 Instance yum update -y
Task 2: Install Docker, AWS CLI, and SAM CLI
Install AWS CLI, Zip, Unzip, wget
yum install awscli git zip unzip wget -y
sudo amazon-linux-extras install docker -y
Start the docker service
sudo service docker start
Add the ec2-user to the docker group and verify
sudo usermod -a -G docker ec2-user
Verify the docker installation, and check if ec2-user user can run docker commands To download the AWS SAM CLI
Verify the integrity and authenticity of the downloaded installer file "aws-sam-cli-linux-x86_64.zip". 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 sudo ./sam-installation/install # Verify the installation by checking the version /usr/local/bin/sam --version
Task 4: Configure the AWS CLI on EC2 Instance
To configure the AWS CLI run the below command:
aws configure Enter the AWS Access key Id: Secret Access Key: Enter the (your) region : us-east-1 Default output format [None]: Press enter.
Task 5: Download the sample SAM Application
To download the sample SAM application, run the below command:
To download the AWS Quick Start Templates. Enter choice 1
To Choose an AWS Quick Start application template. Enter choice 1
To Use the most popular runtime and package type
To select runtime nodejs14.x, Select No 11
To select zip as package type, Enter choice 1
To Select your Starter template, Enter 1
Enter N in enabling X-Ray tracing on the function in your application
Enter Project name as Hello World Example
The project will be cloned from the official GitHub To see the application files, switch the directory to the Hello World Example folder, by running the below command:
cd Hello\ World\ Example/ List all the files using the ls command.
The important files are:
template.yaml: Contains the AWS SAM template defining your application's AWS resources.
hello-world/app.js: Contains AWS Lambda handler logic.
Task 6: Build and Deploy your application
To build the application, run the below command:
/usr/local/bin/sam build --use-container
It may take up to 5 minutes for the build process to complete.
Note: Make sure you are present at the same location i.e. inside the Hello World Example folder.
To deploy the application, run the below command:
/usr/local/bin/sam deploy --guided
To complete the build prompts will ask you a few inputs to enter:
Stack Name: Enter Hello-World
AWS Region: Enter us-east-1
Confirm changes before deploy [y/N]: Enter y
Allow SAM CLI IAM role creation [Y/n]: Enter y
Disable rollback [y/N]: Enter y
HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: Enter y
Save arguments to configuration file [Y/n]: Enter Y
SAM configuration file [samconfig.toml]: Enter Y
SAM configuration environment [default]: hit Enter
Open the CloudFormation console page.
You will find a stack is getting created. Wait for it to complete and refresh the page.
Switch to the Session Manager SSH tab and enter y to deploy this changeset? [y/N].
After your confirmation, the stack will create all the resources. Copy the value of the API URL and paste it to the new tab of your browser.
The sample application is now successfully deployed.
We have successfully created, build, and deployed the sample AWS SAM Application and using Serverless Application Model.
AWS Refference : Tutorial: Deploying a Hello World application
Thank you for reading and following along with the Blog.
Like and Follow for more Azure and AWS Content.