我想知道在設計無服務器功能時采用什么方法,同時將設計常規服務器作為參考。
對于傳統的服務器,人們將專注于定義集合,然后可以在每個集合上運行 CRUD 操作(HTTP 動詞,例如 GET 或 POST)。例如,您將擁有一個 集合users,您可以通過 獲取所有記錄app.get('/users', ...)、通過獲取特定記錄app.get('/users/{id}', ...)或通過創建一條記錄app.post('/users', ...)。
您設計無服務器功能的方法有何不同?具體來說:
- 區分 HTTP 操作是否有意義,還是只使用 POST?我發現在客戶端定義它們很有用,以決定是否要在發生錯誤時重試(如果操作是冪等的,重試將是安全的等),但這似乎并不重要后端。
- 命名。我假設您會使用類似
getAllUsers()當使用常規服務器時定義集合users然后只使用 GET 來指定您想用它做什么的東西。 - 功能大小:如果您需要一步在后端做很多事情。你會定義一些小函式,例如
lookupUser(),endTrialForUser()(如果我們得到的用戶lookupUser()已經試用超過 7 天,則觸發)等,然后從客戶端一個接一個地運行它們(決定是否應該在客戶端上結束試用- 看起來很不安全),或者你會創建一個getUser()然后處理那里的所有邏輯嗎? - 路由。在無服務器功能中,我們真的不能做任何事情,比如
.../users/${id}/accountData. 您將如何獲取嵌套資料?你會每次都回傳一個完整的 JSON 嗎?
我一直在尋找有關此事的一些綜合文章,但沒有運氣。有什么建議?
uj5u.com熱心網友回復:
這是您提出的一個非常廣泛的問題。讓我試著逐點回答。
首先,您在這里談論的方法是Serverless API專案方法。您可以克隆他們的示例專案,以更好地了解如何構建REST用于執行CRUD操作的 api。首先安裝 SAM cli,然后運行以下命令。
$ sam init
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Cloning from https://github.com/aws/aws-sam-cli-app-templates
Choose an AWS Quick Start application template
1 - Hello World Example
2 - Multi-step workflow
3 - Serverless API
4 - Scheduled task
5 - Standalone function
6 - Data processing
7 - Infrastructure event management
8 - Machine Learning
Template: 3
Which runtime would you like to use?
1 - dotnetcore3.1
2 - nodejs14.x
3 - nodejs12.x
4 - python3.9
5 - python3.8
Runtime: 2
Based on your selections, the only Package type available is Zip.
We will proceed to selecting the Package type as Zip.
Based on your selections, the only dependency manager available is npm.
We will proceed copying the template using npm.
Project name [sam-app]: sample-app
-----------------------
Generating application:
-----------------------
Name: sample-app
Runtime: nodejs14.x
Architectures: x86_64
Dependency Manager: npm
Application Template: quick-start-web
Output Directory: .
Next steps can be found in the README file at ./sample-app/README.md
Commands you can use next
=========================
[*] Create pipeline: cd sample-app && sam pipeline init --bootstrap
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
明智地回答您的問題:
- 是的,您應該將您的
HTTP操作與其合適的HTTP verbs. 這可以在 API Gateway 中進行配置,并且可以在 Lambda 代碼中進行檢查。檢查處理程式的源代碼和template.yml您剛剛克隆的專案中的檔案SAM。
// src/handlers/get-by-id.js
if (event.httpMethod !== 'GET') {
throw new Error(`getMethod only accepts GET method, you tried: ${event.httpMethod}`);
}
# template.yml
Events:
Api:
Type: Api
Properties:
Path: /{id}
Method: GET
The naming is totally up to the developer. You can follow the same approach that you're following with your regular server project. You can define the handler with name
getAllUsersorusersand then set the path of that resource toGET /usersin theAWS API Gateway. You can choose theHTTP verbsof your desire. Check this tutorial out for better understanding.Again this up to you. You can create a single Lambda that handles all that logic or create individual Lambdas that are triggered one after another by the client based on the response from the previous API. I would say, create a single Lambda and just return the cumulative response to reduce the number of requests. But again, this totally depends on the UI integration. If your screens demand separate API calls, then please, by all means create individual lambdas.
This is not true. We can have dynamic routes specified in the API Gateway. You can easily set wildcards in your routes by using
{variableName}while setting the routes in API Gateway.GET /users/{userId}TheuserIdwill then be available at your disposal in the lambda function viaevent.pathParameters.GET /users/{userId}?a=xSimilarly, you could even pass query strings and access them viaevent.queryStringParametersin code. Have a look at working with routes.
Tutorial I would recommend for you:
Tutorial: Build a CRUD API with Lambda and DynamoDB
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443176.html
標籤:休息 http aws-lambda 无服务器
