Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda SNS trigger Event Type?

I've been doing serverless dev for a while, mainly AWS, and just starting to get into SNS pub/sub design patters. I'm using typescript and the type support is fantastic, however I can't find the correct type for the event that comes in to a Lambda from SNS or an S3 trigger. These would make the seams between functions very safe indeed. For the moment I'm defining my own interfaces, this will get tiresome though. e.g.

interface IAWSSNSTriggerPayload {
  Records: [
    {
      EventSource: string,
      EventVersion: string,
      EventSubscriptionArn: string,
      Sns: {
        Type: string,
        MessageId: string,
        TopicArn: string,
        Subject: string,
        Message: string,
        Timestamp: string,
        SignatureVersion: string,
        Signature: string,
        SigningCertUrl: string,
        UnsubscribeUrl: string,
        MessageAttributes: {},
      }
    }
  ]
}

export const handler = (event: IAWSSNSTriggerPayload, context, cb) => {
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless Webpack (Typescript) v1.0! Your function executed successfully! And from the SNS',
      input: event,
    }),
  };
  console.log('SNS Received from Lambda Publisher!!!')
  console.log(JSON.stringify(event.Records))
  cb(null, response);
}

If anyone can point me in the right direction I'd be much obliged.

Cheers.

like image 765
theSiberman Avatar asked Dec 23 '22 12:12

theSiberman


1 Answers

You can use SNSEvent from @types/aws-lambda.

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/56c1ea26b59ed0e4634b1ba27096ab3b90371875/types/aws-lambda/index.d.ts#L211

Please do remember that the typings are not maintained by AWS themselves so there may be some cases where it is not up to date.

like image 199
Noel Llevares Avatar answered Jan 05 '23 16:01

Noel Llevares