smsamazon-cloudwatchlogs

Format of log for failed SNS messages in CloudWatch Loggroup


I’m building a lambda function that monitors the logs of failed delivery of SNS messages from CloudWatch loggroup. This is what I get from the log:

{
    "notification":
    {
        "messageId": "9f807381-7795-5cbd-af1a-2db23e0c112c",
        "timestamp": "2024-01-30 19:00:41.925"
    },
    "delivery": 
    {
        "phoneCarrier": "T-mobile USA Inc.",
        "mnc": 800,
        "numberOfMessageParts": 1,
        "destination": "+1xxxxxxxxxx",
        "priceInUSD": 0.00831,
        "smsType": "Transactional",
        "mcc": 310,
        "providerResponse": "Phone has blocked SMS",
        "dwellTimeMs": 0,
        "dwellTimeMsUntilDeviceAck": 1304
    },
    "status": "FAILURE"
}

My question is if this layout or format can be somehow configured to include topicARN or subscriptionARN when the message was published to a topic. I need these info so I can more conveniently identify the receiver that has his phone blocking messages; otherwise it's gonna be very tedious because it's from one of the 45 applications (so need to search it in 45 database). With topicARN or subscriptionARN I can easily identify the application that the receiver is with. Thank you so very much for your help!


Solution

  • SNS supports delivery status logging for some targets on the topic level (SQS, Firehose, Lambda, HTTP, Push). When logging is enabled for these targets, you do get the topic ARN and the subscription ARN in the logs.

    Unfortunately, it looks like the logging of SMS delivery status is not handled by the same code and topic ARN is not being logged indeed.

    To send a text message to a phone number through SNS, you can either post it to a topic to which the number is subscribed, or send it to the number directly.

    The piece of AWS infrastructure responsible for the SMS delivery logging, unfortunately, does not discern between these paths. No matter now your message is being sent, it's getting logged in a log group with the words DirectPublishToPhoneNumber as a part of its name, even if the message was originally sent by posting it to a topic. Topic ARN or subscription ARN are also not parts of these logs.

    If you absolutely need to tie your message delivery status to a topic, you'll have to roll your own indexing solution. You can do that by subscribing your topic to a Lambda or API Gateway endpoint (in addition to SMS) and record the topic ARN and the message id in a DynamoDB table or somewhere. By looking up the message id in this table, you will be able to correlate it to the topic.