Gianluca Fontana
Gianluca Fontana

Reputation: 51

Lambda used SNS to send SMS but stopped working

I was just having fun in sending me SMS from a Lambda called via API Gateway (REST API). No problem at all until it suddendly stopped sending SMS. I'm actually using a free tier level on my AWS account, but in no way I have reached the maximum sendable SMS limit.

Permissions and roles have not been modified from yesterday when text messages still were arriving to my verified mobile number.

As you can see, inside my code, I used a "test2" variable to return the result for SMS sending but it's barely useless.

Is there any way to better undestand the error behind this problem? And why am I getting it all of a sudden?

My lambda code:

const AWS = require("aws-sdk");
const dynamo = new AWS.DynamoDB.DocumentClient();
const SNS = new AWS.SNS();
const tableName = "Testing";  
const smsMessage = {
                PhoneNumber: '+39#mynumber#',
                Message: 'Someone called your api!'
             };
const httpMethodConverter = 'http-method';
const pathRequestConverter = "resource-path";
const headers = {
  "Access-Control-Allow-Origin":"https://#myaccount#.github.io",
  "Content-Type": "application/json",
  "Access-Control-Allow-Methods": "GET, POST, PATCH, PUT, DELETE, OPTIONS",
  "Access-Control-Allow-Headers": "Origin, Content-Type, X-Auth-Token"
};

let body;
let statusCode = 200;
let test2;

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

  try {
    let test = JSON.stringify(event);
    let callContext = event.context;
    let httpMethod = callContext[httpMethodConverter];
    let path = callContext[pathRequestConverter];
    let methodAndPath = httpMethod + " "+ path;
    
    test2= await SNS.publish(smsMessage).promise();

    switch (methodAndPath) {

      case "DELETE /items/{id}":
        await dynamo
          .delete({
            TableName: tableName,
            Key: {
              Id: parseInt(JSON.parse(test).params.path.id)
            }
          })
          .promise();
        body = `Deleted item ${JSON.parse(test).params.path.id}`;
        break;
        
      case "GET /items/{id}":
        body = await dynamo
          .get({
            TableName: tableName,
            Key: {
              Id: parseInt(JSON.parse(test).params.path.id)
            }
          })
          .promise();
        break;
        
      case "GET /items":
        body = await dynamo.scan({ TableName: tableName }).promise();
        break;
        
      case "POST /items":
        var bodyParser = "body-json";
        var bodyPassed = JSON.parse(test)[bodyParser];

        await dynamo
          .put({
            TableName: tableName,
            Item: {
              Id: parseInt(bodyPassed.Id),
              Type: bodyPassed.Type
            }
          })
          .promise();

        body = "Put item "+ bodyPassed.Type+ " id: " + parseInt(bodyPassed.Id);
        break;
        
      default:
        throw new Error(`Unsupported route: "${event.context}"`);
    }
  } catch (err) {
    statusCode = 400;
    body = err.message;
  } finally {
    body = JSON.stringify(body);
  }

  return {
    statusCode,
    body,
    headers,
    test2
  };
};

This is the answer for my SNS.publish(message).promise

"test2": {
    "ResponseMetadata": {
      "RequestId": "b2fbd9fc-4cc0-54e8-a660-82#########"
    },
    "MessageId": "c94393bf-cbb3-5cb9-8155-c#########"
  }

Upvotes: 1

Views: 351

Answers (1)

bjrnt
bjrnt

Reputation: 2822

If you enable delivery status logging through e.g. https://docs.aws.amazon.com/sns/latest/dg/sms_stats_cloudwatch.html, you should receive an error message detailing why the message could not be delivered. Most likely, you've hit the default monthly $1 spend limit quota.

Upvotes: 2

Related Questions