How to fix AWS SNS API end point code that cause site not to load?

The root cause is this:

function sns_endpoint_data() {    
    // this line caused the site not to load
    $message = Message::fromRawPostData();
    /* additional code follows... */
}

add_action( 'template_redirect', 'sns_endpoint_data' );

There is no if condition checking if this is indeed the URL you desired to do this on. As a result, the Message::fromRawPostData() will load on every page that uses a template regardless of the URL.

This is because you never checked to see which page you were on, and you tried to do work in the wrong hook.

Handling the Rule

add_rewrite_tag( '%apitest%', '([^&]+)' );
add_rewrite_rule( 'test/([^&]+)/?', 'index.php?apitest=$matches[1]', 'top' );

Here we see apitest is added as a rewrite tag, but it’s never tested.

So instead, lets modify the template_redirect action to do what it’s supposed to: redirect the template, say something similar to this:

function sns_apitest_template_redirect() {    
    global $wp_query;
    if ( !empty( $wp_query->query_vars['apitest'] ) ) {
        $apitest= $wp_query->query_vars['apitest'];
        sns_handle_apitest_endpoint( $apitest );
        exit;
}
add_action( 'template_redirect', 'sns_apitest_template_redirect' );

Note that it attempts to detect the apitest tag and if it is not empty, it calls a function then exits. This way the code for your endpoints logic isn’t muddled up with template_redirect.

So now we need that function:

function sns_handle_apitest_endpoint( $apitest ) {
    $message = Message::fromRawPostData();
    // etc...
}