5

I'm having a weird issue with Amazon AWS SNS : When creating subscriptions and topics using aws-php-sdk (3.112.7), there is always a "ghost" or "invisible" subscription.

AWS Console screenshot

As you can see, this subscription exists in "subscriptions" tab. However, when I click on the topic link (here cav_56826), I can't see any subscription.

AWS Topic screenshot

Do you guys already had a similar issue ? How can this happen ?

Here is a my simplified code :

  try
    {
        $arn = "arn:aws:sns:eu-west-1:XXXXXXXXXXXXXXXXX:app/APNS_VOIP_SANDBOX/ios_cav";
        $topics = array("allUsers", "cav_56826");
        $topicsToSubcribe = array();

        foreach ($topics as $topic)
        {
            $res = $this->snsClient->createTopic(['Name' => $topic]);
            if ($res->get('@metadata')['statusCode'] == 200)
            {
                array_push($topicsToSubcribe, $res->get('TopicArn'));
            }
            else
            {
                throw new Exception("An error occured during Amazon SNS createTopic", $res->get('@metadata')['statusCode']);
            }
        }

        $SNSEndPointData = $this->snsClient->createPlatformEndpoint([
            'PlatformApplicationArn' => $arn,
            'Token'                  => $token
        ]);

        foreach ($topicsToSubcribe as $topic)
        {
            $this->snsClient->subscribe([
                'Protocol' => "application",
                'Endpoint' => $SNSEndPointData->get('EndpointArn'),
                'TopicArn' => $topic
            ]);
        }
    }
    catch (\Exception $e)
    {
       // Logs some errors
    }
André DS
  • 1,823
  • 1
  • 14
  • 24

1 Answers1

2

Using PHP 5.6.40 and AWS SDK PHP 3.122.0 (found here) and the below code after doing some changes, I can see the expected/right behavior is happening.

<?php

require '/usr/src/myapp/aws.phar';
$sdk = new Aws\Sdk([
    'region'   => 'us-east-1',
    'version'  => 'latest',
]);

$snsClient = $sdk->createSns();
$token = "XX:YY";

try
    {
        $arn = "arn:aws:sns:us-east-1:360479286475:app/GCM/test-stackoverflow";
        $topics = array("allUsers", "cav_56826");
        $topicsToSubcribe = array();

        foreach ($topics as $topic)
        {
            $res = $snsClient->createTopic(['Name' => $topic]);
            if ($res->get('@metadata')['statusCode'] == 200)
            {
                array_push($topicsToSubcribe, $res->get('TopicArn'));
            }
            else
            {
                throw new Exception("An error occured during Amazon SNS createTopic", $res->get('@metadata')['statusCode']);
            }
        }

        $SNSEndPointData = $snsClient->createPlatformEndpoint([
            'PlatformApplicationArn' => $arn,
            'Token'                  => $token
        ]);

        foreach ($topicsToSubcribe as $topic)
        {
            $snsClient->subscribe([
                'Protocol' => "application",
                'Endpoint' => $SNSEndPointData->get('EndpointArn'),
                'TopicArn' => $topic
            ]);
        }
    }
    catch (\Exception $e)
    {
       // Logs some errors
    } 

?>

all subscriptions: all subscriptions

"allUsers" subscriptions: enter image description here

Hope this helps.

mostafazh
  • 4,144
  • 1
  • 20
  • 26
  • Hello, thanks for your answer. You have no problem ? Even if you quickly delete your topic and run code again ? – André DS Jan 13 '20 at 08:08