帮助我们改进Amazon SDK for JavaScript版本 3 (V3) 文档,方法是使用反馈链接,或者在上创建议题或拉取请求GitHub
这些区域有:Amazon SDK for JavaScriptV3 API 参考指南详细描述了Amazon SDK for JavaScript版本 3 (V3)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
创建 Amazon Lambda 函数
配置开发工具包
在libs
目录创建一个名为的文件snsClient.js
和lambdaClient.js
,然后将下面的内容分别粘贴到它们中。
const { SNSClient } = require("@aws-sdk/client-sns"); // Set the AWS Region. const REGION = "REGION"; // e.g. "us-east-1" // Create an Amazon Simple Notification Service client object. const snsClient = new SNSClient({region:REGION}); module.exports = { snsClient };
Replace领域
使用Amazon区域。此代码可用此处GitHub
const { LambadaClient } = require( "@aws-sdk/client-lambda" ); // Set the AWS Region. const REGION = "eu-west-1"; // e.g. "us-east-1" // Create an Amazon Lambda service client object. const lambdaClient = new LambdaClient({region:REGION}); module.exports = { lambdaClient };
Replace领域
使用Amazon区域。此代码可用此处GitHub
首先导入必需的Amazon SDK for JavaScript(v3) 模块和命令。然后计算今天的日期并将其分配给参数。然后为ScanCommand
. 替换TABLE_NAME
使用您在中创建的表的名称创建Amazon资源 这个例子的部分。
下面的代码段演示了此步骤。(有关完整示例,请参阅捆绑 Lambda 函数。)
"use strict"; // Load the required clients and commands. const { ScanCommand } = require ( "@aws-sdk/client-dynamodb" ); const { PublishCommand } = require ( "@aws-sdk/client-sns" ); const {lambdaClient} = require ( "./libs/lambdaClient" ); const {snsClient} = require ( "./libs/snsClient" ); // 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 dynamoClient.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.ts
和所需Amazon SDK for JavaScript此示例的模块变成一个名为index.js
.
如果您尚不了解,请按照先决项任务对于这个例子来安装 webpack。
注意 有关的信息webpack,请参阅使用 webpack 捆绑应用程序.
在命令行中运行以下命令以捆绑JavaScript对于这个例子到一个名为的文件
<index.js>
:webpack mylambdafunction.ts --mode development --target node --devtool false --output-library-target umd -o index.js
重要 注意输出名为
index.js
. 这是因为 Lambda 函数必须有index.js
处理程序来工作。压缩捆绑的输出文件,
index.js
,转入名为的 ZIP 文件mylambdafunction.zip
.上传
mylambdafunction.zip
转到您在中创建的 Amazon S3 存储桶创建Amazon资源 本教程的主题。