Skip to content

[Bug]: SNS -> SQS Subscription wrong ARN when working with localstack- Invalid parameter: SQS endpoint ARN #179

@MichalOleszczuk

Description

@MichalOleszczuk

Bug description

autoSubscribe not working for resource with Protocol: 'sqs',

Environment

  • Windows 10
  • Node version: 16.20.0
  • NPM version: 8.19.4
  • "serverless": "3.27.0",
  • "serverless-esbuild": "1.39.0",
  • "serverless-offline": "12.0.4",
  • "serverless-offline-sns": "0.76.0",
  • "serverless-offline-sqs": "7.3.2",

Bugfix or workaround

Make autoSubscribe feature work by providing resource arn in Endpoint: subscribeEndpoint, for Protocol: 'sqs' not http url.

Details

We can make autoSubscribe work for SQS subscriptions with sns-endpoint config option by allowing to pull queue resource ARN.
Currently when code reaches this.sns.subscribe(params, (err, data) => ... then params object is already wrong. When we provide resource with Protocol: 'sqs', line Endpoint: subscribeEndpoint, should not have URL in it. Since mixing protocol different then 'http' and url provided in endpoint will result in error from AW SDK
⚠️ Invalid parameter: SQS endpoint ARN.
And this makes sense since when using Protocol: 'SQS' SDK is expecting ARN not http url.

For serverless.ts configuration:

      'serverless-offline-sns': {
        'sns-endpoint': 'http://localhost:4566',
        accountId: process.env.AWS_ACCOUNT_ID,
        debug: true,
      },
    [...]
  resources: {
    Resources: {
      SnsSynchronizeProfileEmailTopic: {
        Type: 'AWS::SNS::Topic',
        Properties: {
          TopicName: 'synchronize-profile-email-topic',
        },
      },
      SnsSynchronizeProfileEmailSubscription: {
        Type: 'AWS::SNS::Subscription',
        Properties: {
          Protocol: 'sqs',
          Endpoint: { 'Fn::GetAtt': ['SynchronizeProfileEmailQueue', 'Arn'] },
          TopicArn: { Ref: 'SnsSynchronizeProfileEmailTopic' },
          RawMessageDelivery: 'true',
        },
      },
      SynchronizeProfileEmailQueue: {
        Type: 'AWS::SQS::Queue',
        Properties: {
          QueueName: 'synchronize-profile-email',
        },
      },
    },
  },

This code fails
3
when this code works
7

And it triggers lambda configured like below in the end

  functions: {
    [config.name]: {
      handler: relativePathToHandler,
      events: [
        {
          sqs: {
            enabled: true,
            batchSize: 1,
            maximumBatchingWindow: 1,
            arn: { 'Fn::GetAtt': ['SynchronizeProfileEmailQueue', 'Arn'] },
          },
        },
      ],
    },
  },

5
script sending message to SNS
6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions