# 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656556858535/Nxu81Ls95.png align="left")
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](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html))
![0.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656622386886/1kOZSfp9o.png align="left")
```
//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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656622558465/oGd4HHLZd.png align="left")

Install docker  
```
sudo amazon-linux-extras install docker -y
```
![3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656622807668/bRgCpDRSR.png align="left")
![2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656622767728/8MxwjTYNu.png align="left")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656623425412/DKL-vi-EH.png align="left")
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656623472339/0JnaJEYM_.png align="left")  
```
# Install the AWS SAM CLI  
sudo ./sam-installation/install
# Verify the installation by checking the version  
/usr/local/bin/sam --version
``` 
![7.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656624125231/pf71WbcfW.png align="left")

### 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](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-appendix-sign-up.html)  
![8.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656624824174/xTIOIUy22.png align="left")  

### Task 5: Download the sample SAM Application
To download the sample SAM application, run the below command:
```
/usr/local/bin/sam init
```
![10.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656626245442/Cjd7C1BJK.png align="left")  

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656626224562/bEZHjKwub.png align="left")

Enter N in enabling X-Ray tracing on the function in your application
![11.11png.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656625355553/Inm1OHZey.png align="left")    
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656625449210/qwbZR6o8y.png align="left")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656625675853/gHGFppCHu.png align="left")
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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656626332592/qYtD5vRcH.png align="left")  

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656625944789/Xyb7BnY0z.png align="left")

Open the [CloudFormation](https://us-east-1.console.aws.amazon.com/cloudformation/home?region=us-east-1#) console page.

You will find a stack is getting created. Wait for it to complete and refresh the page.  
![15.6.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656626531080/PZI7d40UX.png align="left")
![16.6.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656626565249/LKrvZHv7k.png align="left")  

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

![18.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656628276314/sk7ToWrdE.png align="left")

Output :  
![ssm20.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656626880211/_AiEaGm__.png align="left")

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](https://cdn.hashnode.com/res/hashnode/image/upload/v1656626942438/WPjn9zOoH.png align="left")

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](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html)

Thank you for reading and following along with the Blog.

Happy Learning.

Like and Follow for more Azure and AWS Content.

Regards,  
Jineshkumar Patel



