AWS lambda sending sns message to phone number

222 views Asked by At

I have a lambda function running node.js The function makes database calls and sends a text message via sns. It has the following structure

enter image description here

the functions -> index.js file looks like this

async function sendTextMessage(message, phoneNumber) {
  try {
    console.log("hitSendFunction");
    const sns = new AWS.SNS();

    const params = {
      Message: message,
      MessageStructure: "string",
      PhoneNumber: phoneNumber
    };
    //remember phone number must have +1 before it.

    return await sns.publish(params).promise();
  } catch (error) {
    console.log(error);
  }
}
module.exports = {
  sendTextMessage
};

that function is than called in the main index.js file:

const database = require("./db");
const { sendTextMessage } = require("./functions");
const AWS = require("aws-sdk");
AWS.config.region = "us-east-1";
exports.handler = async function (event, context) {
  try {
    console.log("hit");
    const result = await database.query("call getTicker()");
    const data = result[0][0];
    const currentProjectEndDate = new Date(
      data.currentProjectEndDate
    ).getTime();
    const now = new Date().getTime();
    console.log("data.currentProjectEndDate", data.currentProjectEndDate);
    const runningHot =
      data.jobsInQueue > 0 && currentProjectEndDate <= now && data.textSent < 1;
    if (runningHot) {
      const numbers = ["+1435994****"];
      for (let i = 0; i < numbers.length; i++) {
        let number = numbers[i];
        console.log("number", number);
        let messageResult = await sendTextMessage(
          "The CE Bot is running hot from lambda",
          number
        );
        console.log("messageResult", messageResult);
      }
      await database.query("call insertIntoTextSent()");
      console.log("yes");
    }
  } catch (error) {
    console.log("this was the error", error);
  }

The database calls work correctly but the textMessage function just hangs and times out. The lambda function has the following permissions attached to it:

enter image description here

Finally even though I do not think it is needed as the db code is working here is what the db -> index.js file looks like:

const mysql = require("mysql");
const util = require("util");

const awsConfig = {
  host: process.env.RDS_HOST,
  user: process.env.RDS_USER,
  password: process.env.RDS_PASSWORD,
  database: process.env.RDS_DATABASE
};

const connection = mysql.createConnection(awsConfig);

connection.query = util.promisify(connection.query.bind(connection));
connection.end = util.promisify(connection.end.bind(connection));

module.exports = connection;

I am not quite sure where I am going wrong here. Can someone point me in the right direction?

1

There are 1 answers

1
codernoob8 On BEST ANSWER

You can just send it via sms in sns just type in the phone number