hyphen
hyphen

Reputation: 3430

AWS SNS Notification Invalid Post Data

I'm having issues with the AWS PHP SDK and SNS Notifications in a Laravel application. I've had a similar setup working in the past and copying that setup, but it's not working. The only difference is in the old one, the version is:

"aws/aws-php-sns-message-validator": "^1.7",
"aws/aws-sdk-php": "^3.228",

and in the new one the version is:

"aws/aws-php-sns-message-validator": "^1.9",
"aws/aws-sdk-php": "^3.302",

I'm getting an error of Invalid POST data.

When I publish a message from the SNS dashboard in AWS and log it out, the only thing that comes across is the message body as raw text. There are none of the normal SNS notification data elements I'd expect to see, nor are there any MessageAttributes when I add those.

I literally can't get past the constructor, so I'll post that:

namespace App\Actions\Webhooks;

use Aws\Sns\Message;
use GuzzleHttp\Client;
use Aws\Sns\MessageValidator;
use Illuminate\Support\Facades\Log;
use App\Jobs\ProcessInboundDocumentJob;
use GuzzleHttp\Exception\GuzzleException;

class HandleDocumentUploadedWebhooks
{
    protected Message $message;
    protected MessageValidator $validator;

    public function __construct()
    {
        Log::debug(file_get_contents('php://input')); //logs only the message body as just text.
        $this->message = Message::fromRawPostData(); //fails here.
        $this->validator = new MessageValidator();
    }

...other stuff that never happens because it fails in the constructor.
}

and this is my routes file showing the POST request:

Route::post('/webhooks/document-upload', [HandleDocumentUploadedWebhooks::class, 'handle']);

I actually had this working this morning, and now it's not. The only substantial thing that changed is that I upgraded the packages.

Invalid POST data. {"exception":"[object] (RuntimeException(code: 0): Invalid POST data. at /var/www/html/vendor/aws/aws-php-sns-message-validator/src/Message.php:68) [stacktrace] #0 /var/www/html/vendor/aws/aws-php-sns-message-validator/src/Message.php(44): Aws\Sns\Message::fromJsonString() #1 /var/www/html/app/Actions/Webhooks/HandleDocumentUploadedWebhooks.php(20): Aws\Sns\Message::fromRawPostData()

UPDATE:

Apparently I had enabled the Raw Message Delivery option, which I thought I had to have checked in order to get the MessageAttributes, but that may have been before I updated. I unchecked that and now it's working as expected.

UPDATE 2

Ok, with the Raw Message Delivery option unchecked, when I publish an SNS notification through the AWS SNS dashboard with MessageAttributes, they come across in the POST request.

However, when I create the SNS notification via my python lambda function, the MessageAttributes aren't in the notification:

 sns = boto3.client('sns')
    response = sns.publish(
        TopicArn="arn:myarn",
        Message="A document has been received and processed.",
        MessageAttributes={
            "NotificationType": {"DataType": "String","StringValue": "DocumentReceived"},
            "FileName": {"DataType": "String", "StringValue": object_key},
            "LocationId": {"DataType": "String", "StringValue": location_id},
            "EmailMessageId": {"DataType": "String", "StringValue": email_message_id},
            "ToAddress": {"DataType": "String", "StringValue": to_address},
        }
    )

UPDATE 3

It looks like I had a misconfiguration that was causing multiple SNS messages, and the latter one had my MessageAttributes.

enter image description here

Upvotes: 0

Views: 84

Answers (0)

Related Questions