Search code examples
javascriptnode.jsamazon-web-servicesexpressamazon-ses

Conflict of configuration of dynamodb and ses in express while importing


what i am trying to do: Run ses code in app.post route.So i have prepared a configuration file for ses in a separate and then importing in app.js.The same [procedure is being followed for dynamo db configuration also, and this is raising a conflict which throws an 404 error.

I think the issue is related to aws config globally. according to this https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/global-config-object.html But i am not able to figure out.

my code for ses works perfectly well when i am trying to run as node sendEmail.js. the error is because of similar configuration import of dynamo db. i am running my ses code for post route, tried inserting there also but somehow it is reading dynamo db configuration of endpoint.

sendEmail.js ( commented part is only for testing, i am passing params in app.js)

let sendEmail = (params) => {
 let params1=params
var AWS2 = require("aws-sdk");
require('dotenv').config()
let awsConfig2 = {
    "region": process.env.DBREGION,
   // "endpoint": process.env.DBENDPOINT,
    "accessKeyId": process.env.DBACCESSKEYID, "secretAccessKey": process.env.DBSECRETACCESSKEY
};
AWS2.config.update(awsConfig2);
console.log('added email config')


  
  // Create the promise and SES service object
  var sendPromise = new AWS2.SES({apiVersion: '2010-12-01'}).sendEmail(params1).promise();
  
  // Handle promise's fulfilled/rejected states
  sendPromise.then(
    function(data) {
      console.log(data.MessageId);
    }).catch(
      function(err) {
      console.error(err, err.stack);
    });
}
// var params = {
//     Destination: { /* required */
      
//       ToAddresses: [
//         'xxxxxxx@gmail.com',
//         /* more items */
//       ]
//     },
//     Message: { /* required */
//       Body: { /* required */
//         Html: {
//          Charset: "UTF-8",
//          Data: "Got an Order"
//         },
//         Text: {
//          Charset: "UTF-8",
//          Data: "Order Request"
//         }
//        },
//        Subject: {
//         Charset: 'UTF-8',
//         Data: 'Check odaijini for new emails'
//        }
//       },
//     Source: 'xxxxxxxxx@gmail.com', /* required */
    
//   };
//sendEmail(params)
module.exports=sendEmail;

dynamo db config

var AWS = require("aws-sdk");
require('dotenv').config()
let awsConfig = {
    "region": process.env.DBREGION,
    "endpoint": process.env.DBENDPOINT,
    "accessKeyId": process.env.DBACCESSKEYID, "secretAccessKey": process.env.DBSECRETACCESSKEY
};
AWS.config.update(awsConfig);
console.log('added')

module.exports = new AWS.DynamoDB.DocumentClient();

NOte if i comment this endpoint line in config then ses code also works. But dynamo needs endpoint, how should i work upon it.

error

message: null,
  code: 404,
  time: 2021-05-21T10:04:54.588Z,
  requestId: 'AT2LSMTUS78PRDAB23CRQ1JCRNVV4KQNSO5AEMVJF66Q9ASUAAJG',
  statusCode: 404,
  retryable: false,
  retryDelay: 21.438085761293955
} 404: null

Solution

  • IT was a config error because between dynamo db and see because if aws sdk global configuration

    ALl i had to remove endpoint from global configuration and use it specifically for dynamodb service.

    var AWS = require("aws-sdk");
    require('dotenv').config()
    let awsConfig = {
        "region": process.env.DBREGION,
        "accessKeyId": process.env.DBACCESSKEYID, "secretAccessKey": process.env.DBSECRETACCESSKEY
    };
    AWS.config.update(awsConfig);
    
    console.log('added')
    
    module.exports = new AWS.DynamoDB.DocumentClient({"endpoint": 'http://dynamodb.ap-southeast-1.amazonaws.com'}