Create the Lambda functions - AWS SDK for JavaScript

The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (V3).

Create the Lambda functions

This topic is part of a tutorial that demonstrates to to invoke Lambda functions using AWS Step Functions. To start at the beginning of the tutorial, see Creating AWS serverless workflows using AWS SDK for JavaScript.

Use the Lambda runtime API to create the Lambda functions. In this example, there are three workflow steps that each correspond to each Lambda function.

Create these Lambda function, as described in the following sections:

  • getId Lambda function - Used as the first step in the workflow that processes the ticket ID value.

  • addItem Lambda class - Used as the second step in the workflow that assigns the ticket to an employee and stores the data in a DynamoDB database.

  • sendemail Lambda class - Used as the third step in the workflow that use the Amazon SES to send an email message to the employee to notify them about the ticket.

getId Lambda function

Create a Lambda function that returns the ticket ID value that is passed to the second step in the workflow.

exports.handler = async (event) => { // Create a support case using the input as the case ID, then return a confirmation message try{ const myCaseID = event.inputCaseID; var myMessage = "Case " + myCaseID + ": opened..."; var result = { Case: myCaseID, Message: myMessage }; } catch(err){ console.log('Error', err); } };

Enter the following in the command line to use webpack to bundle the file into a file named index.js.

webpack getid.js --mode development --target node --devtool false --output-library-target umd -o index.js

Then compress index.js into a ZIP file name getid.js.zip. Upload the ZIP file to the Amazon S3 bucket you created in the the topic of this example.

This code example is available here on GitHub.

addItem Lambda class

Create a Lambda function that selects an employee to assign the ticket, then stores the ticket data in a DynamoDB table named Case.

"use strict"; // Load the required clients and commands. const { PutItemCommand } = require ( "@aws-sdk/client-dynamodb" ); const { dynamoClient } = require ( "../libs/dynamoClient" ); exports.handler = async (event) => { try { // Helper function to send message using Amazon SNS. const val = event; //PersistCase adds an item to a DynamoDB table const tmp = Math.random() <= 0.5 ? 1 : 2; console.log(tmp); if (tmp == 1) { const params = { TableName: "Case", Item: { id: { N: val.Case }, empEmail: { S: "brmur@amazon.com" }, name: { S: "Tom Blue" }, }, }; console.log("adding item for tom"); try { const data = await dynamoClient.send(new PutItemCommand(params)); console.log(data); } catch (err) { console.error(err); } var result = { Email: params.Item.empEmail }; return result; } else { const params = { TableName: "Case", Item: { id: { N: val.Case }, empEmail: { S: "RECEIVER_EMAIL_ADDRESS" }, // Valid Amazon Simple Notification Services (Amazon SNS) email address. name: { S: "Sarah White" }, }, }; console.log("adding item for sarah"); try { const data = await dynamoClient.send(new PutItemCommand(params)); console.log(data); } catch (err) { console.error(err); } return params.Item.empEmail; var result = { Email: params.Item.empEmail }; } } catch (err) { console.log("Error", err); } };

Enter the following in the command line to use webpack to bundle the file into a file named index.js.

webpack additem.js --mode development --target node --devtool false --output-library-target umd -o index.js

Then compress index.js into a ZIP file name additem.js.zip. Upload the ZIP file to the Amazon S3 bucket you created in the the topic of this example.

This code example is available here on GitHub.

sendemail Lambda class

Create a Lambda function that sends an email to notify them about the new ticket. The email address that is passed from the second step is used.

// Load the required clients and commands. const { SendEmailCommand } = require ( "@aws-sdk/client-ses" ); const { sesClient } = require ( "../libs/sesClient" ); exports.handler = async (event) => { // Enter a sender email address. This address must be verified. const senderEmail = "SENDER_EMAIL" const sender = "Sender Name <" + senderEmail + ">"; // AWS Step Functions passes the employee's email to the event. // This address must be verified. const recepient = event.S; // The subject line for the email. const subject = "New case"; // The email body for recipients with non-HTML email clients. const body_text = "Hello,\r\n" + "Please check the database for new ticket assigned to you."; // The HTML body of the email. const body_html = `<html><head></head><body><h1>Hello!</h1><p>Please check the database for new ticket assigned to you.</p></body></html>`; // The character encoding for the email. const charset = "UTF-8"; var params = { Source: sender, Destination: { ToAddresses: [recepient], }, Message: { Subject: { Data: subject, Charset: charset, }, Body: { Text: { Data: body_text, Charset: charset, }, Html: { Data: body_html, Charset: charset, }, }, }, }; try { const data = await sesClient.send(new SendEmailCommand(params)); console.log(data); } catch (err) { console.error(err); } };

Enter the following in the command line to use webpack to bundle the file into a file named index.js.

webpack sendemail.js --mode development --target node --devtool false --output-library-target umd -o index.js

Then compress index.js into a ZIP file name sendemail.js.zip. Upload the ZIP file to the Amazon S3 bucket you created in the the topic of this example.

This code example is available here on GitHub.

Deploy the Lambda functions

To deploy the getid Lambda function:

  1. Open the Lambda console at Amazon Web Services Console.

  2. Choose Create Function.

  3. Choose Author from scratch.

  4. In the Basic information section, enter getid as the name.

  5. In the Runtime, choose Node.js 14x.

  6. Choose Use an existing role, and then choose lambda-support (the IAM role that you created in the ).

  7. Choose Create function.

  8. choose Upload from - Amazon S3 location.

  9. Choose Upload, choose Upload from - Amazon S3 location, and enter the Amazon S3 link URL.

  10. Choose Save.

  11. Repeat this procedure for the additem.js.zip and sendemail.js.zip to new Lambda functions. When you finish, you will have three Lambda functions that you can reference in the Amazon States Language document.