How to create and deploy an application using AWS SAM

Posted By :Aakash Chaddha |22nd March 2021

 

It is not efficient to manage your infrastructure manually for the following reasons:

  • It is not a fast enough method for deployment. 
  • It lacks consistency due to multiple slacks in the process. 
  • For linear processes, the developer needs to wait for the previous tasks to complete. 
  • Overall It increases the management overhead.

 

That is where an open-source framework from AWS comes to the rescue. It helps you create your infrastructure as code. It allows you to duplicate, automate, and audit your infrastructure easily.

 

What is SAM?

The Amazon Web Services Serverless Application Model (SAM) allows you to configure APIs, functions, event source mappings, and databases using shorthand syntax. With only a few lines of code per resource in YAML, you can create your application. Moreover, AWS SAM CLI converts Its syntax into AWS CloudFormation syntax automatically.

 

Why SAM?

  1. Everything can be managed on one configuration file.
  2. AWS SAM provides local debugging & testing features for you. 
  3. AWS SAM CLI has an execution environment locally for AWS Lambda functions.
  4. It uses best practices for deployment.


 

Now, we will set up a simple AWS SAM project in Ubuntu.

For other platforms, please follow this link:

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html


 

Follow these steps:

1. Create an AWS account.

You need to have an AWS account to create an AWS SAM application.

2. Create IAM Users with sufficient permissions & Generate AWS credentials

IAM USER:

The account that we are using for AWS SAM must have sufficient permissions to run it.

For the simple case, we can just grant administrator privileges to your IAM user. 

 

AWS Credentials:

You need to install AWS-CLI. Follow this link to install (https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-linux.html)

 

Once installed, use this command “aws configure” to configure Access & Secret key.

aws configure
AWS Access Key ID [None]: your_access_key_id
AWS Secret Access Key [None]: your_secret_access_key
Default region name [None]:
Default output format [None]:

3. Install the AWS SAM CLI

Follow these steps:

  1. AWS SAM CLIP zip can be download from here: AWS SAM CLI zip file 
  2. Ensure the validity and legitimacy of the downloaded installer files by using the following command to generate a hash value.:

sha256sum aws-sam-cli-linux-x86_64.zip

  1. You will get something similar to this:
    5b9c61df9a92f0e9f7d335a124ae5d94cc10a11b424fa2635e0cfaeb56bdd3e3  aws-sam-cli-linux-x86_64.zip
    Compare the hash with your AWS SAM CLI version in the AWS SAM CLI release notes.
  2. Unzip the file

unzip aws-sam-cli-linux-x86_64.zip -d sam-installation

  1. Install the AWS SAM CLI from the zip.

sudo ./sam-installation/install

  1. Verify the AW SAM CLI installation.

sam --version

  1. The output will be similar to this:
    SAM CLI, version 1.18.0

 

Let’s now create an AWS SAM application.

We need to follow these steps:

1. Download a sample application

sam init

 When you enter the above command, you will be prompted with some options.

 For simplicity, we suggest that you should select these options when prompted:

  1. AWS Quick Start Templates
  2. The Zip package type, 
  3. Your preferred Runtime.
  4. The Hello World Example.

(base) $ sam init

Which template source would you like to use?

1 - AWS Quick Start Templates

2 - Custom Template Location

Choice: 1

What package type would you like to use?

1 - Zip

2 - Image

Package type: 1

 

Which runtime would you like to use?

1 - nodejs12.x

2 - python3.8

3 - ruby2.7

4 - go1.x

5 - java11

6 - dotnetcore3.1

7 - nodejs10.x

8 - python3.7

9 - python3.6

10 - python2.7

11 - ruby2.5

12 - java8.al2

13 - java8

14 - dotnetcore2.1

Runtime: 9

 

Project name [sam-app]: 

 

After providing values to all the prompts, AWS SAM will automatically build the content of the project. It will be similar to this:

sam-app/
  ??? README.md
  ??? events/
  ?   ??? event.json
  ??? hello_world/
  ?   ??? __init__.py
  ?   ??? app.py         
  ?   ??? requirements.txt
  ??? template.yaml
  ??? tests/
      ??? unit/
          ??? __init__.py
          ??? test_handler.py

 

2. Build your application

cd sam-app
sam build

 

A successful build will generate output similar to this:


Build Succeeded

 Built Artifacts  : .aws-sam/build
Built Template   : .aws-sam/build/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided

 

3. Deploy your application

sam deploy --guided

These are all the steps required to create and deploy an application using AWS SAM.

 

You can now hit this application from the generated endpoint URL.

 

For example:

curl https://<API_ID>.execute-api.us-west-2.amazonaws.com/Prod/hello-world/

 

If SAM is successfully deployed, we will get this response:

 

{"message": "hello world"}

         

 


About Author

Aakash Chaddha

He is a very ambitious, motivated, career oriented person, willing to accept challenges, energetic and result oriented, with excellent leadership abilities, and an active and hardworking person who is patient and diligent.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch

Chat With Us