How to deploy AWS Serverless Application in Cloud9
Published: 2023-09-12 09:46:39

Howdy! 

In this tutorial we are going to initialize, build and deploy a simple Hello world application using AWS Serverless Application. 
The Hello World application contains resources which includes:
 - **AWS Lambda** – Function that processes the HTTP API GET request and returns a hello world message.
 - **AWS Identity and Access Management (IAM) role** - Provisions permissions for the services to securely interact.
- **Provisions permissions for the services to securely interact** - API endpoint that you will use to invoke your function.

## Prerequisites:
- Before installing AWS SAM CLI ensure that
   - Create an AWS account, AWS Identity, IAM credials and access key pair.
- Install AWS CLI
- Use AWS CLI to configure AWS credentials

## Initialize Hello World Application
- We are going to use the AWS SAM CLI to create the application on our Cloud9.

i). Run the command below on your desired directory

```
sam init
```

ii). The prompts will guide you through the initializing a new application.
 - Select AWS Quick Start Templates to choose a starting template.

 - Choose the Hello World Example template and download it.

 - Use the Python3.8 runtime and image package type.

 - Opt out of AWS X-Ray tracing. See What is AWS X-Ray? in the [AWS X-Ray Developer Guide](https://docs.aws.amazon.com/xray/latest/devguide/aws-xray.html).

 - Opt out of monitoring with Amazon CloudWatch Application Insights. Learn about [Amazon CloudWatch Application.](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html)

 - Name your application 

Use the image below for reference

![screenshot1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fks113vxetadt5guh30h.png)

cont:


![screenshot2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqiews0h41pussybpbbd.png)

iii). The AWS SAM CLI then downloads your the template then creates the application directory.

iv). Navigate to the newly created directory. Now, let's breakdown the files:
- **hello_world/app.py** – Contains your Lambda function code.
- **hello_world/requirements.txt** – Contains any Python dependencies that your Lambda function requires.
- **samconfig.toml** – Configuration file for your application that stores default parameters used by the AWS SAM CLI.
- **template.yaml** – The AWS SAM template that contains your application infrastructure code.

## Build the Application

- Building the app will dockerize the lambda function in cloud9 environment. Use the command:

```
sam build
```
- After building AWS SAM CLI creates a `.aws-sam `directory,it organizes your function dependencies, project code and files there.
  

```
.aws-sam
├── build
│   ├── HelloWorldFunction
│   │   ├── __init__.py
│   │   ├── app.py
│   │   └── requirements.txt
│   └── template.yaml
└── build.toml
```
this is what each file does:
- **build/HelloWorldFunction** – Contains your Lambda function code and dependencies. The AWS SAM CLI creates a directory for each function in your application.

- **build/template.yaml** – Contains a copy of your AWS SAM template that is referenced by AWS CloudFormation at deployment.

- **build.toml** – Configuration file that stores default parameter values referenced by the AWS SAM CLI when building and deploying your application.


![build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5siirb8am2jy932d52um.png)

## Deploy the application to an ECR image repository
- Before building ensure that you have configured your AWS credentials.
The application files will be uploaded to S3 and the SAM template is transformed into a CloudFormation. The template is then uploaded the template to the AWS CloudFormation service to provision your AWS resources.

- To deploy, run the command below:

```
sam deploy --guided
```
- Be sure to configure the deployment steps here is an example of how to

![deploy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qykbl9yb2nm5am6dxwc.png)

- Confirm the deployment changes
![deployment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9r88fgmaf4u27eqgblw4.png)

- After successful deployment an API gateway endpoint URL that you can `curl` or paste in a browser to see the function output. 

_Alternatively_

- You can use the `sam list endpoints-output json` command to get information
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3zgk0z1zgu8810q7vuo.png)

## Testing
- Ensure that you are in the correct directory before running the testing command.
Run the command to test the function

```
sam local invoke
```
The expected output is as below:

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

- Since `sam local invoke` does not test API we are going to use the command below.

```
sam local start-api
```

![api test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qpzkstgsvibavbcx0uon.png)

To check on what we are calling, you can open a new terminal the run curl and the endpoint.

```
curl http://127.0.0.1:3000/hello
```
The payload is successful as below:

![curl api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdxjwf99woyxb3yz1h29.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zf6vax3l2f4c6r1mtz58.png)

This is the basis of getting started with SAM!
Happy Coding!