5

我从事无服务器开发已经有一段时间了,主要是 AWS,并且刚刚开始涉足 SNS 发布/订阅设计模式。我正在使用 typescript 并且类型支持很棒,但是我找不到从 SNS 或 S3 触发器进入 Lambda 的事件的正确类型。这些将使函数之间的接缝确实非常安全。目前我正在定义自己的接口,但这会让人厌烦。例如

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);
}

如果有人能指出我正确的方向,我将不胜感激。

干杯。

4

1 回答 1

11

您可以使用SNSEvent来自@types/aws-lambda.

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

请记住,这些类型不是由 AWS 自己维护的,因此在某些情况下它可能不是最新的。

于 2017-12-05T01:25:11.853 回答