Amazon SDK for JavaScriptV3 API 参考指南详细描述了Amazon SDK for JavaScript版本 3 (V3) 的所有 API 操作。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
创建 Amazon Lambda 函数
配置开发工具包
首先导入必需Amazon SDK for JavaScript(v3) 模块和命令:DynamoDBClient
和 DynamoDBScanCommand
, 和SNSClient
和 Amazon SNSPublishCommand
命令。Replace领域
使用Amazon区域。然后计算今天的日期并将其分配给参数。然后创建参数ScanCommand
. 替换TABLE_NAME
与您在中创建的表的名称一致。创建Amazon资源 这个例子的部分。
下面的代码段演示了此步骤。(有关完整示例,请参阅捆绑 Lambda 函数。)
"use strict"; // Load the required clients and commands. const { DynamoDBClient, ScanCommand } = require("@aws-sdk/client-dynamodb"); const { SNSClient, PublishCommand } = require("@aws-sdk/client-sns"); //Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Get today's date. const today = new Date(); const dd = String(today.getDate()).padStart(2, "0"); const mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0! const yyyy = today.getFullYear(); const date = yyyy + "-" + mm + "-" + dd; // Set the parameters for the ScanCommand method. const params = { // Specify which items in the results are returned. FilterExpression: "startDate = :topic", // Define the expression attribute value, which are substitutes for the values you want to compare. ExpressionAttributeValues: { ":topic": { S: date }, }, // Set the projection expression, which the the attributes that you want. ProjectionExpression: "firstName, phone", TableName: "TABLE_NAME", };
扫描 DynamoDB 表
首先创建一个名为的 async/await 函数sendText
使用 Amazon SNS 发布短信PublishCommand
. 然后,添加try
区块模式,该模式可以扫描 DynamoDB 表以查找员工今天的工作周年纪念日,然后调用sendText
函数向这些员工发送短信。如果出现错误catch
块被称为。
下面的代码段演示了此步骤。(有关完整示例,请参阅捆绑 Lambda 函数。)
exports.handler = async (event, context, callback) => { // Helper function to send message using Amazon SNS. async function sendText(textParams) { try { const data = await snsclient.send(new PublishCommand(textParams)); console.log("Message sent"); } catch (err) { console.log("Error, message not sent ", err); } } try { // Scan the table to check identify employees with work anniversary today. const data = await dbclient.send(new ScanCommand(params)); data.Items.forEach(function (element, index, array) { const textParams = { PhoneNumber: element.phone.N, Message: "Hi " + element.firstName.S + "; congratulations on your work anniversary!", }; // Send message using Amazon SNS. sendText(textParams); }); } catch (err) { console.log("Error, could not scan table ", err); } };
捆绑 Lambda 函数
本主题介绍如何将mylambdafunction.js
以及所需Amazon SDK for JavaScript这个例子的模块变成一个名为index.js
.
如果您尚不了解,请按照先决条件项对于这个例子来安装 webpack。
注意
有关webpack,请参阅使用 webpack 捆绑应用程序.
在命令行中运行以下命令以捆绑JavaScript对于这个例子到一个名为的文件
<index.js>
:webpack mylamdbafunction.js --mode development --target node --devtool false --output-library-target umd -o index.js
重要
请注意,输出被命名为
index.js
. 这是因为 Lambda 函数必须有index.js
处理程序来工作。压缩捆绑的输出文件,
index.js
,转入名为的 ZIP 文件my-lambda-function.zip
.上传
mylambdafunction.zip
转到您在中创建的 Amazon S3 存储桶创建Amazon资源 本教程的主题。
以下是完整的浏览器脚本代码mylambdafunction.js
.
"use strict"; // Load the required clients and commands. const { DynamoDBClient, ScanCommand } = require("@aws-sdk/client-dynamodb"); const { SNSClient, PublishCommand } = require("@aws-sdk/client-sns"); //Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Get today's date. const today = new Date(); const dd = String(today.getDate()).padStart(2, "0"); const mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0! const yyyy = today.getFullYear(); const date = yyyy + "-" + mm + "-" + dd; // Set the parameters for the ScanCommand method. const params = { // Specify which items in the results are returned. FilterExpression: "startDate = :topic", // Define the expression attribute value, which are substitutes for the values you want to compare. ExpressionAttributeValues: { ":topic": { S: date }, }, // Set the projection expression, which the the attributes that you want. ProjectionExpression: "firstName, phone", TableName: "TABLE_NAME", }; // Create the client service objects. const dbclient = new DynamoDBClient({ region: REGION }); const snsclient = new SNSClient({ region: REGION }); exports.handler = async (event, context, callback) => { // Helper function to send message using Amazon SNS. async function sendText(textParams) { try { const data = await snsclient.send(new PublishCommand(textParams)); console.log("Message sent"); } catch (err) { console.log("Error, message not sent ", err); } } try { // Scan the table to check identify employees with work anniversary today. const data = await dbclient.send(new ScanCommand(params)); data.Items.forEach(function (element, index, array) { const textParams = { PhoneNumber: element.phone.N, Message: "Hi " + element.firstName.S + "; congratulations on your work anniversary!", }; // Send message using Amazon SNS. sendText(textParams); }); } catch (err) { console.log("Error, could not scan table ", err); } };