AWS Lambda Accessing DynamoDB Causes Internal Server Error with Debugging Difficulties
在這次的學習中,我深入探討了如何使用 AWS Lambda 存取 DynamoDB。過程中遇到了一個問題:當查詢結果超過 1 MB 時,會直接回傳 Internal Server Error,而這種情況在使用 try-catch 捕獲時,無法正常處理。
以下是我在實作中使用的基本代碼範例:
try {
const data = await ddb.query(params).promise();
} catch (e) {
return e.message;
}
如上所示,無法捕獲到因為查詢結果超過大小限制而產生的錯誤。這讓我感到困惑,於是我開始在網路上查找解決方案。
DynamoDB 查詢結果大小限制
經過一些研究,我發現 DynamoDB 的查詢結果最大為 1 MB。當查詢結果超過此限制時,需要使用分頁來獲取額外的數據。這可以通過 LastEvaluatedKey 來實現分頁查詢。
解決方案
為了解決這個問題,我在查詢時使用了 timestamp 作為過濾條件,這樣就成功回傳了所需的資料。這裡有一個要注意的細節:如果是在 JavaScript 中撰寫 Lambda 函數,記得要把 timestamp 除以 1000,因為 JavaScript 中的時間戳是以毫秒為單位。
總結
這次的經歷讓我對 AWS Lambda 和 DynamoDB 的整合有了更深入的了解,特別是在處理大型查詢結果時的最佳實踐。希望這些筆記能幫助到未來的自己或其他學習者!
— — — — — — — — — — — — -
In this learning experience, I delved into how to access DynamoDB using AWS Lambda. I encountered a significant issue: when the query result exceeds 1 MB, it returns an Internal Server Error, and this situation cannot be caught using a try-catch
block.
Here’s a basic example of the code I used in my implementation:
try {
const data = await ddb.query(params).promise();
} catch (e) {
return e.message;
}
As shown above, it fails to catch errors caused by exceeding the size limit of the query results. This left me confused, so I began searching online for solutions.
DynamoDB Query Result Size Limit
After some research, I discovered that the maximum size for a DynamoDB query result is 1 MB. When the query result exceeds this limit, pagination is required to retrieve additional data. This can be accomplished using the LastEvaluatedKey
for pagination queries.
Solution
To solve this issue, I implemented a timestamp
as a filter in my query, which successfully returned the desired data. One important detail to note is that when writing Lambda functions in JavaScript, you need to divide the timestamp
by 1000, as JavaScript timestamps are in milliseconds.
Conclusion
This experience deepened my understanding of integrating AWS Lambda with DynamoDB, especially regarding best practices for handling large query results. I hope these notes will assist my future self or other learners!