How to deploy AWS Serverless Application in Cloud9

jeptoo

Ivy Jeptoo

Posted on September 12, 2023

How to deploy AWS Serverless Application in Cloud9

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
Enter fullscreen mode Exit fullscreen mode

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.

  • Opt out of monitoring with Amazon CloudWatch Application Insights. Learn about Amazon CloudWatch Application.

  • Name your application

Use the image below for reference

screenshot1

cont:

screenshot2

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
Enter fullscreen mode Exit fullscreen mode
  • After building AWS SAM CLI creates a .aws-samdirectory,it organizes your function dependencies, project code and files there.
.aws-sam
├── build
│   ├── HelloWorldFunction
│   │   ├── __init__.py
│   │   ├── app.py
│   │   └── requirements.txt
│   └── template.yaml
└── build.toml
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode
  • Be sure to configure the deployment steps here is an example of how to

deploy

  • Confirm the deployment changes
    deployment

  • 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

Testing

  • Ensure that you are in the correct directory before running the testing command. Run the command to test the function
sam local invoke
Enter fullscreen mode Exit fullscreen mode

The expected output is as below:

SAM

  • Since sam local invoke does not test API we are going to use the command below.
sam local start-api
Enter fullscreen mode Exit fullscreen mode

api test

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
Enter fullscreen mode Exit fullscreen mode

The payload is successful as below:

curl api

Image description

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

💖 💪 🙅 🚩
jeptoo
Ivy Jeptoo

Posted on September 12, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related