How to Deploy a Sample "Hello World" App using AWS SAM - Serverless

How to Deploy a Sample "Hello World" App using AWS SAM - Serverless

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. 279-3.png 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)

0.png

//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

1.png

Install docker

sudo amazon-linux-extras install docker -y

3.png 2.png

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

wget https://github.com/aws/aws-sam-cli/releases/latest/download/aws-sam-cli-linux-x86_64.zip

5.5.png 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

6.png

# Install the AWS SAM CLI  
sudo ./sam-installation/install
# Verify the installation by checking the version  
/usr/local/bin/sam --version

7.png

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.

To get your access key ID and secret access key
8.png

Task 5: Download the sample SAM Application

To download the sample SAM application, run the below command:

/usr/local/bin/sam init

10.png

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

10.0.png

Enter N in enabling X-Ray tracing on the function in your application 11.11png.png
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.

12.a.png

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

13.6.png 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

14.6.png

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

14.6.6.png

Open the CloudFormation console page.

You will find a stack is getting created. Wait for it to complete and refresh the page.
15.6.png 16.6.png

Switch to the Session Manager SSH tab and enter y to deploy this changeset? [y/N].

18.png

Output :
ssm20.png

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.

19.png

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.

Happy Learning.

Like and Follow for more Azure and AWS Content.

Regards,
Jineshkumar Patel