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:
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.
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.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.
It will be very easy to use AWS CLI to access AWS services if you setup AWS credentials.
Choose the user you have created, and then create access key
If you don’t have SAM CLI installed into your local machine, please follow this link Installing the AWS SAM CLI
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.
Using command
sam init --name assistant --runtime python3.9
to create a project named assistant using Python3.9.Using command
sam build
to build the projectHere is the structure of this project.
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 namedlambda_handler
, which will be called when receiving a request.events
: This folder only contains a file nameevent.json
, which can help build a request forlambda_handler
, you can update the parameters if needstests
: Running the test case forlambda_handler
helps to check the logic right or not.
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. 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.
Here are the resources about this project.
Here is the resource in Lambda
Here is the monitor for this project.
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
, andsam deply
. We just need to developlambda_handler
to handle our business logic inapp.py
file. - We can easily manage and monitor our resources using
AWS CloudFormation
andAWS CloudWatch
- You can see the source code in GitHub.