Can't confirm SNS subscription

My SNS subscription route gets called with a response such as

{
  success: true,
  data: {
    Type: 'SubscriptionConfirmation',
    MessageId: 'b42e8d2a-6117-45d5-805a-dfdb5609090e',
    Token: 'f6ae6b03',
    TopicArn: 'arn:aws:sns:us-west-1:000000000000:create-attachment',
    Message: 'You have chosen to subscribe to the topic arn:aws:sns:us-west-1:000000000000:create-attachment.\n' +
      'To confirm the subscription, visit the SubscribeURL included in this message.',
    SubscribeURL: 'http://localhost:4566/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-1:000000000000:create-attachment&Token=f6ae6b03',
    Timestamp: '2023-04-12T18:04:59.217Z',
    SignatureVersion: '1',
    Signature: 'EXAMPLEpH+..',
    SigningCertURL: 'https://sns.us-east-1.amazonaws.com/SimpleNotificationService-0000000000000000000000.pem'
  }
}
export const route = async (req: Request, res: Response) => {
  if (req.header('x-amz-sns-message-type') === 'SubscriptionConfirmation') {
    const payload = JSON.parse(res.body);
    const uuu = payload.SubscribeURL.replace('localhost', '127.0.0.1'); //localhost doesn't work here for some reason
    try {
      const ress = await fetch(uuu);
      console.log(await ress.text());
    } catch (e) {
      console.log(e);
    }
  }
  res.status(200).end();
};

Excuse my poor naming but this returns

<?xml version='1.0' encoding='utf-8'?>
<ErrorResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"><Error><Code>InvalidParameter</Code><Message>Invalid parameter: Token</Message><Type>Sender</Type></Error><RequestId>NREQMGRCAQ1BI4857JAB5QHPRF5EJFFGR6FNEPNEGIN3XXF9XJFS</RequestId></ErrorResponse>
import { SNSClient } from '@aws-sdk/client-sns';
const REGION = 'us-west-1';
const snsClient = new SNSClient({
endpoint: 'http://127.0.0.1:4566',
region: REGION,
});

export { snsClient };
    const params = {
      Token: payload.Token,
      TopicArn: payload.TopicArn,
      AuthenticateOnUnsubscribe: 'true',
    };
    const d = await snsClient.send(new ConfirmSubscriptionCommand(params));

This works correctly but it’s not at all clear why I need to do this vs visiting the link.

Hi @bluewave41,

It seems this is a bug on our side, because of the region we would use to confirm the subscription. We have another issue here: bug: SNS Subscription Confirmation fails with: InvalidParameter: Token · Issue #8118 · localstack/localstack · GitHub

There’s a fix on progress here: fix ConfirmSubscription access to SNS store by bentsku · Pull Request #8129 · localstack/localstack · GitHub

Thanks for the question!

1 Like