创建 AWS Lambda 函数 - AWS SDK for JavaScript

帮助我们改进AWS SDK for JavaScript版本 3 (V3) 文档,方法是使用反馈链接,或者在上创建议题或拉取请求GitHub.

这些区域有:AWS SDK for JavaScriptV3 API 参考指南详细描述了所有的 API 操作AWS SDK for JavaScript版本 3 (V3)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

创建 AWS Lambda 函数

配置开发工具包

首先导入必需AWS SDK for JavaScript(v3) 模块和命令:DynamoDBClient和 DynamoDBScanCommand, 和SNSClient和 Amazon SNSPublishCommand命令。Replace领域使用AWS区域。然后计算今天的日期并将其分配给参数。然后创建参数ScanCommand. 替换TABLE_NAME与您在中创建的表的名称一致。创建AWS资源 这个例子的部分。

下面的代码段演示了此步骤。(有关完整示例,请参阅捆绑 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以及所需AWS SDK for JavaScript这个例子的模块变成一个名为index.js.

  1. 如果您尚不了解,请按照先决条件项对于这个例子来安装 webpack。

    注意

    有关webpack,请参阅使用 webpack 捆绑应用程序.

  2. 在命令行中运行以下命令以捆绑JavaScript对于这个例子到一个名为的文件<index.js>

    webpack mylamdbafunction.js --mode development --target node --devtool false --output-library-target umd -o index.js
    重要

    请注意,输出被命名为index.js. 这是因为 Lambda 函数必须有index.js处理程序来工作。

  3. 压缩捆绑的输出文件,index.js,转入名为的 ZIP 文件my-lambda-function.zip.

  4. 上传mylambdafunction.zip转到您在中创建的 Amazon S3 存储桶创建AWS资源 本教程的主题。

以下是完整的浏览器脚本代码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); } };