AWS API-Gateway communicating to SNS

后端 未结 7 762
暗喜
暗喜 2020-12-29 06:13

I am building an API which will be serviced by Lambda functions but I need these to be asynchronous so rather than connecting the API-Gateway directly to the Lambda function

相关标签:
7条回答
  • 2020-12-29 06:47

    I did eventually get this to work after working with AWS support. Here's my solution:

    • First of all even though you're sending a POST you will not be able to send a JSON message in the body of the message as you might expect
    • Instead you must URL Encode the JSON and pass it as query parameter
    • Also remember that the JSON you send should start with a root object of default which in SNS-world means the "default channel"
    • Then, eventually Lambda picks up the SNS event you must also abstract away a lot of the noise to get at your JSON message. For this I created the following function that I use within my Lambda function:

    /**
     * When this is run in AWS it is run "through" a SNS
     * event wconfig.ich adds a lot of clutter to the event data,
     * this tests for SNS data and normalizes when necessary
     */
    function abstractSNS(e) {
      if (e.Records) {
        return JSON.parse(decodeURIComponent(e.Records[0].Sns.Message)).default;
      } else {
        return e;
      }
    }
    
    /**
     * HANDLER
     * This is the entry point for the lambda function
     */
    exports.handler = function handler(event, context) {
      parent.event = abstractSNS(event);

    0 讨论(0)
提交回复
热议问题