Search code examples
node.jsaws-lambdaaxiosamazon-ses

Do I need to import AWS SDK into lambda


Might be a dumb question, but couldn't find a clear answer on stack/aws docs. My assumption is that it should be built in to lambda.

I am running Node10.x, with Axios module, in AWS Lambda. I have a successful function which checks a DNS/EC2/Endpoint pathway and returns the proper response. I want to automate it so it checks, lets say...every 10 minutes. It will notify me in SES if there is an error, and do nothing if its a good response.

All the functionality works, except I am having trouble getting SES integrated. inside the if statement below, i have added this code, the console.log works, so its just the SES part im having issues with.

exports.handler = async (event, context) => {

let aws = require('aws-sdk');
let ses = new aws.SES({
   region: 'us-east-1'
});

let data = "document_contents=<?xml version=\"1.0\" encoding=\"UTF-8\"?><auth><user>fake</user><pass>info</pass></auth>";

var axios = require("axios");

var config = {
  headers: { 'Content-Type': 'text/xml' },
};

let res = await axios.post('https://awebsiteidontwanttogiveout.com', data, config);
let eParams;

if (res.status === 200) {
  console.log("Success");

  eParams = {
        Destination: {
            ToAddresses: ["banana@apples.com"]
        },
        Message: {
            Body: {
                Text: {
                    Data: "Test SUCCESSFUL"
                }
            },
            Subject: {Test SUCCESSFUL"
            }
        },
        Source: "banana@apples.com"
  };

  ses.sendEmail(eParams);
} 

if (res.status != 200) {
  console.log("Failure");

  eParams = {
        Destination: {
            ToAddresses: ["banana@apples.com"]
        },
        Message: {
            Body: {
                Text: {
                    Data: "Test FAIL"
                }
            },
            Subject: {
                Data: "Test FAIL"
            }
        },
        Source: "banana@apples.com"
  };

  ses.sendEmail(eParams);
}


};

I'm getting a time out after 3 seconds. I uploaded a zip file to node, with dependencies. do I need to install AWS SDK and upload that with the file? shouldn't it be built in somehow? am I missing something in my SES call?

thanks


Solution

  • There are two issues to address:

    1. sendEmail is an async function from AWS SDK, you have to use:

    await ses.sendEmail(eParams).promise()

    or else, Lambda would end execution before sendEmail method completes.

    1. Lambda's default timeout is 3 seconds. This can be increased to a max of 15 minutes in the Lambda configuration.

    enter image description here