Build HelloWorld API using AWS SAM
Tags: serverless, aws, lambd, python
Published: 2022-11-29 16:29:35

## Serverless Application Model (AWS SAM):
The AWS Serverless Application Model (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.

### Benefits:

- Single-deployment configuration

- Extension of AWS CloudFormation

- Built-in best practices

- Local debugging and testing

- Deep integration with development tools

### Prerequisites:

- Install the AWS CLI - [Click here](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)

- Install the AWS SAM CLI - [Click here](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)

## Architecture Overview:

![Architecture Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd6oxc2x5xycetwmjjop.png)

## Download stage:

`sam init`

![Choose AWS Quick Start Templates](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90fghjbxz7jxidp32ojw.png)

Choose **AWS Quick Start Templates**

![Choose Hello World Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gisvsajwhrv4k2ht6bdl.png)

Choose **Hello World Example**

![Give y](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fobraiola2qqhdok6m0s.png)

Give **y** - we are going with python and zip package

![Give y](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/glhto7396ug7q8dgxwip.png)

Give **y** - This will enable X-Ray tracing for function

![Give project name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55t7z08rfe0c5emg7bv0.png)

Give project name as **firstserverlessapp**

This command creates a directory with the name that you provided as the project name. The contents of the project directory are similar to the following:

`firstserverlessapp/
   ├── README.md
   ├── events/
   │   └── event.json
   ├── hello_world/
   │   ├── __init__.py
   │   ├── app.py            #Contains your AWS Lambda handler logic.
   │   └── requirements.txt  #Contains any Python dependencies the application requires, used for sam build
   ├── template.yaml         #Contains the AWS SAM template defining your application's AWS resources.
   └── tests/
       └── unit/
           ├── __init__.py
           └── test_handler.py`

There are three especially important files:

- **template.yaml**: Contains the AWS SAM template that defines your application's AWS resources.

- **hello_world/app.py**: Contains your actual Lambda handler logic.

- **hello_world/requirements.txt**: Contains any Python dependencies that the application requires, and is used for sam build.

## Build stage:

`sam build`

![SAM Build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m62tcn5l8jeosou6lc75.png)

You can see the following top-level tree under **.aws-sam**:

![AWS SAM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/saczz6uolziavv9amoec.png)

## Deploy stage:

`sam deploy --guided`

![Deploy SAM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylz6c7aiezoaol8sflwz.png)

`curl https://<restapiid>.execute-api.us-east-1.amazonaws.com/Prod/hello/`

`{"message": "hello world"}`

## Test locally:

`sam local start-api`

![Sam local](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3w5knvjp3ln8lyeoudtd.png)

`curl http://127.0.0.1:3000/hello`

## Invoke Lambda function:

`sam local invoke "HelloWorldFunction" -e events/event.json`

![Invoke lambda](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kepdppgrgxrhnhrm459i.png)

## Clean up:

`aws cloudformation delete-stack --stack-name **sam-app** --region **region**`

Replace sam-app with stack name and region with your aws region