Una piccola funzione Lambda per inviare in modalità serverless i dati di una form (salvati su DaynamoDB):
// Import the SNS Client from AWS SDK v3
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
// Create an SNS client instance
const snsClient = new SNSClient({ region: "us-west-3" }); //update
export const handler = async (event) => {
const records = event.Records;
const messagePromises = records.map(async (record) => {
console.log('Stream record: ', JSON.stringify(record, null, 2));
if (record.eventName === 'INSERT') {
const nome = record.dynamodb.NewImage.Nome.S; //update all
const cognome = record.dynamodb.NewImage.Cognome.S;
const email = record.dynamodb.NewImage.email.S;
const tel = record.dynamodb.NewImage.Telefono.S;
const note = record.dynamodb.NewImage.Note.S;
const params = {
Subject: `A new message from ${nome} ${cognome}`,
Message: `Dati: ${email} ${tel} ${note}`,
TopicArn: 'arn:aws:sns:us-west-3:xxxxx:TableNAME' //update
};
// Create a PublishCommand instance
const command = new PublishCommand(params);
try {
// Sending the message using the send method
const data = await snsClient.send(command);
console.log("Results from sending message: ", JSON.stringify(data, null, 2));
} catch (err) {
console.error("Unable to send message. Error JSON:", JSON.stringify(err, null, 2));
}
}
});
// Wait for all the SNS messages to be sent
await Promise.all(messagePromises);
return `Successfully processed ${records.length} records.`;
};