logo
Menu
How to build a simple serverless application

How to build a simple serverless application

This post will help you know how to build a simple serverless application.

Published Jan 1, 2024
If you are interested in serverless but you don't know how to use it in AWS, if you're a newer to serverless and eager to know how to develop a serverless application quickly. This article will help you out. There are five parts in this article as below:

Prerequisites

Creating an AWS account

Open the Amazon Web Services (AWS) home page. Choose Create an AWS Account. If you have an AWS account aready, please choose Sign in to the Console.

Configuring IAM

You cannot allow to access AWS services if you don't have AWS Identity and Access Management (IAM) permissions, you can create a user and setup up IAM AdministratorAccess permission.
Configuring IAM

Installing and Configuring AWS CLI

AWS Command Line Interface (CLI) is an open source tool that enables you to interact with AWS services using commands in your command-line shell. You can refer to AWS CLI to install it.

Setup credentials

It will be very easy to use AWS CLI to access AWS services if you setup AWS credentials.
Create access key
Choose the user you have created, and then create access key
Create access key
Setup AK/SK

Installing SAM CLI

If you don’t have SAM CLI installed into your local machine, please follow this link Installing the AWS SAM CLI

Installing Python

If you want to use python as your application language, you have to install python in your local pc, you can follow this link Python.

Build

Init project

Using command sam init --name assistant --runtime python3.9 to create a project named assistant using Python3.9.

Build project

Using command sam build to build the project

Project structure

Here is the structure of this project.
Project Structure
Here are the explanation for this project structure.
  • .aws-sam : SAM will store the temporary file in this folder when building, deplying etc.
  • assistant : This folder contains the core codes, app.py includes a Lambda function named lambda_handler, which will be called when receiving a request.
  • events : This folder only contains a file name event.json, which can help build a request for lambda_handler , you can update the parameters if needs
  • tests : Running the test case for lambda_handler helps to check the logic right or not.

Test

Running command sam local start-api to test locally.
You can visit http://127.0.0.1:3000/chat to check if this project works well.
Test your project locally

Deploy

Running command sam deploy to deploy this project in your AWS account.
And then, you can visit AssistantApi's value to check if this project works well.
Deploy your project and then check it
Here are the resources about this project.
Resources of this prject in CloudFormation
Here is the resource in Lambda
Resource in Lambda
Here is the monitor for this project.
Monitor for Lambda

Conclusion

AWS SAM is really a sample tool to help us develop serverless application.
  • We have to understand a few commands, including sam init, sam build, sam local start-api , and sam deply. We just need to develop lambda_handler to handle our business logic in app.py file.
  • We can easily manage and monitor our resources using AWS CloudFormation and AWS CloudWatch
  • You can see the source code in GitHub.

Comments