The Amazon SDK for JavaScript V3 API
Reference Guide
Creating the Amazon Lambda function
Configuring the SDK
First import the required Amazon SDK for JavaScript (v3) modules and commands:
DynamoDBClient
and the DynamoDB ScanCommand
, and SNSClient
and the Amazon SNS PublishCommand
command. Replace REGION
with the Amazon Region. Then calculate today's date and assign it to a parameter.
Then create the parameters for the ScanCommand
.Replace TABLE_NAME
with the name of the table you created in the
Create the Amazon resources section of this example.
The following code snippet shows this step. (See Bundling the Lambda function for the full example.)
"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", };
Scanning the DynamoDB table
First create an async/await function called sendText
to publish a text message using the Amazon SNS PublishCommand
. Then, add a try
block
pattern that scans the DynamoDB table for employees with their work anniversary today, and then calls the sendText
function to send these employees a text message. If an error
occurs the catch
block is called.
The following code snippet shows this step. (See Bundling the Lambda function for the full example.)
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); } };
Bundling the Lambda function
This topic describes how to bundle the mylambdafunction.js
and the required Amazon SDK for JavaScript modules for this example into a
bundled file called index.js
.
If you haven't already, follow the Prerequisite tasks for this example to install webpack.
Note
For information aboutwebpack, see Bundling applications with webpack.
Run the the following in the command line to bundle the JavaScript for this example into a file called
<index.js>
:webpack mylamdbafunction.js --mode development --target node --devtool false --output-library-target umd -o index.js
Important
Notice the output is named
index.js
. This is because Lambda functions must have anindex.js
handler to work.Compress the bundled output file,
index.js
, into a ZIP file namedmy-lambda-function.zip
.Upload
mylambdafunction.zip
to the Amazon S3 bucket you created in the Create the Amazon resources topic of this tutorial.
Here is the complete browser script code for 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); } };