What is serverless
Serverless allows you to build and run applications and services without having to worry about the server. Understand simply that you only need to care about writing code to solve customer problems and push the code onto a service (AWS Lambda) to run without caring about server building.
Serverless allows you to build modern applications with greater flexibility and lower total cost of ownership. Building serverless applications means that developers can focus on the core product instead of having to worry about managing and operating multiple servers or runtime, whether on cloud or on-premises.
AWS Lambda
AWS Lambda allows you to run code without providing or managing servers. Just upload your code and Lambda will take care of everything it needs to run. Lambda supports languages: Node.js, Python, Java, Ruby, C #, Go and PowerShell.
What is the AWS Serverless Application Model (AWS SAM)
AWS Serverless Application Model (AWS SAM) is an open source framework for building serverless applications, used to run on AWS ( https://github.com/awslabs/serverless-application-model )
Install AWS SAM CLI
Before installing you must set the following conditions:
- Install docker on the operating system
- AWS account (to deploy code)
Next, install Homebrew as a package manager. See details here . Run the following commands to install
1 2 | $ sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)" |
Add Homebrew to PATH on linux with the following command:
1 2 3 4 5 | $ test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv) $ test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv) $ test -r ~/.bash_profile && echo "eval $($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile $ echo "eval $($(brew --prefix)/bin/brew shellenv)" >>~/.profile |
Check if the installation was successful
1 2 | $ brew --version |
Results displayed
1 2 3 | Homebrew 2.2.5 Homebrew/linuxbrew-core (git revision 9d1f; last commit 2020-02-10) |
Finally install the SAM cli with the following command:
1 2 3 | $ brew tap aws/tap $ brew install aws-sam-cli |
Check if installed
1 2 3 | $ sam --version SAM CLI, version 0.41.0 |
Hello World Application
In this article I will use the Nodejs language to write services. To initialize the project we will run the following command:
1 2 | sam init --runtime nodejs10.x --dependency-manager npm --name lambda_skeleton |
Results after running the command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Which template source would you like to use? 1 - AWS Quick Start Templates 2 - Custom Template Location Choice: 1 Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git AWS quick start application templates: 1 - Hello World Example 2 - Quick Start: From Scratch 3 - Quick Start: Scheduled Events 4 - Quick Start: S3 5 - Quick Start: SNS 6 - Quick Start: SQS 7 - Quick Start: Web Backend Template selection: 2 ----------------------- Generating application: ----------------------- Name: lambda_skeleton Runtime: nodejs10.x Dependency Manager: npm Application Template: quick-start-from-scratch Output Directory: . Next steps can be found in the README file at ./lambda_skeleton/README.md |
Or to initialize with other options you can refer here
After the project has been created, I have modified the directory structure as follows:
1 2 3 4 | |src -|app.js -|services |
The app.js file will take responsibility for receiving requests from the API and the response for the following content
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | exports.handler = (event, context, callback) => { const message = 'Hello from Lambda!' console.log(`${message}`) callback(null, { statusCode: 200, headers: { 'Content-Type': 'text/html' }, body: message }) } |
Folder service will contain utils file to handle the business tasks of the task. For the time being I won't have any complicated functions, so I'll leave it blank
Edit template.yml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | helloFromLambdaFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./src Handler: app.handler Runtime: nodejs10.x MemorySize: 1024 Timeout: 100 Description: A Lambda function that returns a static string. Policies: - AWSLambdaBasicExecutionRole Events: Api: Type: Api Properties: Path: /hello Method: get |
I will explain a little bit about conifg above
- CodeUri: used to specify the folder containing source code
- Handler: function will contain the code to start when starting running
- Runtime: runtime environment
- MemorySize: ram capacity when running docker
- Timeout: the maximum time to run the program
- Events: declares api endpoints
Run the following command to start API Gateway Locally
1 2 | sam local start-api |
Results after starting api:
1 2 3 4 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template 2020-03-02 15:01:12 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit) Invoking app.handler (nodejs10.x) |
Check if the API has a response:
Conclude
You can refer to your project on github: https://github.com/duongpham910/lambda_skewelry