0
votes

I am beginner, I found two instructions on the internet to get data ( Contact us - Email, Phone, etc ) to AWS - Lambda then send to my email

https://aws.amazon.com/blogs/architecture/create-dynamic-contact-forms-for-s3-static-websites-using-aws-lambda-amazon-api-gateway-and-amazon-ses/

And another one to make a AWS - Lambda function to send to Dynamo to back up.

https://www.vairix.com/tech-blog/get-and-put-data-using-lambda-and-dynamodb-simple-and-clear

I put them together, but they don't connect. For example, Lamba trigger to send info from website to my email but the other function wont collect the item to send it to Dynamo

Lambda to send email:

var AWS = require('aws-sdk');
var ses = new AWS.SES();
 
var RECEIVER = '[email protected]';
var SENDER = '[email protected]';

var response = {
 "isBase64Encoded": false,
 "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'example.com'},
 "statusCode": 200,
 "body": "{\"result\": \"Success.\"}"
 };

exports.handler = function (event, context) {
    console.log('Received event:', event);
    sendEmail(event, function (err, data) {
        context.done(err, null);
    });
};
 
function sendEmail (event, done) {
    var params = {
        Destination: {
            ToAddresses: [
                RECEIVER
            ]
        },
        Message: {
            Body: {
                Text: {
                    Data: 'name: ' + event.name + '\nphone: ' + event.phone + '\nemail: ' + event.email + '\ndesc: ' + event.desc,
                    Charset: 'UTF-8'
                }
            },
            Subject: {
                Data: 'Website Referral Form: ' + event.name,
                Charset: 'UTF-8'
            }
        },
        Source: SENDER
    };
    ses.sendEmail(params, done);
}

Lambda to store information in DynamoDB:

"use strict";

AWS.config.update({ region: "us-east-1" });


exports.handler = async() => {

const documentCilent = new AWS.DynamoDB.DocumentClient({region: "us-east-1"});

const params = {
    TableName: "Users",
    
    //  This part, I need the variable connect with the top part. 
    Item:{
      id : "test",     
      name: "name",
      phone: "phone",
      desc: "desc",
      email: "email"
      
      
    }
  
};

try {
    const data = await documentCilent.put(params).promise();
    console.log(data);

} catch (err) {
    console.log(err);
}
};
1
What is the output of console.log('Received event:', event); - what do you see in CloudWatch? - Ermiya Eskandary
Also for them to be connected - you need to send the email and then store in DynamoDB in one go; the other function won't "collect it" unless you have an SNS topic, an SQS queue etc. to trigger it. - Ermiya Eskandary

1 Answers

0
votes

Lambda's are independent than each other and only fire upon receiving an Event to do so. This Event can be several things - It can be an Event sent to the lambda from an API Gateway (this is what connecting your Api to your Lambda does - gives a target for the Api Event to go to). It can be an event generated by your SDK (using your languases SDK to invoke another lambda). It can be from a DynamoDB stream or an S3 put_object event.

Pretty much any thing that happens in AWS resources outputs an event of some kind, and with the right commands/setups you can have a Lambda listening for that event.

In this situation however, as these are both very light weight, it would just be more cost effective to combine them under one lambda. API gets the information, sends it to the lambda which fires an email and adds it to the Dynamodb. Anything else in this situation is kinda overkill and would increase your costs.