Publishing to SNS

I’ve setup an SNS subscription like this in serverless.yml

  functions:
    helloWorld:
      handler: lib/handlers/hello.helloWorldHandler
      events:
        - sns: helloWorld
        - http:
            method: get
            path: hello
        - http:
            method: post
            path: hello

That creates the SNS topic and subscription seemingly fine upon deploy - and when I publish to the topic subscription via the AWS console UI it causes the lambda to run.

PROBLEM: however, when I try and publish via the NodeJS API the lambda does not respond, eg:

  async function publishSomething() {
    const sns = new aws.SNS({ region: 'us-west-2' });
    const payload = JSON.stringify({ foo: 123 });
    await sns.publish({
      Subject: 'My Subject',
      Message: payload,
      TargetArn: 'arn:aws:sns:us-west-2:255291343873:helloWorld',
    })
  }

What am I doing wrong here? Something obvious/stupid??!! The fact that it responds from the AWS Console UI suggests that it’s configured OK in the serverless.yml and that I’m somehow addressing it wrong in the publish method.

Thanks everyone!

To use await you need to call sns.publish().promise(). I would then use a try/catch block to log any error.

1 Like

Oh man! <Forehead slap!!!>

Thanks @buggy - that was a silly unforced error on my part. Cheers!