AWS API-Gateway与SNS通信


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 I'm using the "AWS Service Proxy" to publish SNS messages and then have the Lambda function subscribe to the relevant SNS topic so it receives delivery of the requests. Here's a picture which illustrates the flow:


I have tested both the Lambda function in isolation as well pub/sub messaging between SNS and Lambda but I am struggling with the API-Gateway to SNS handoff. Documentation is quite light but what I am assuming right now is that the following attributes must be sent in the POST request:

我已经单独测试了Lambda函数以及SNS和Lambda之间的pub / sub消息传递,但我正在努力使用API​​-Gateway进行SNS切换。文档很轻,但我现在假设的是必须在POST请求中发送以下属性:

  1. Action: the API-Gateway offers to set this in the UI and I have put in the Publish action which is the appropriate SNS action


  2. Message: the body of the POST message should be a JSON document. It would be passed by the web client and proxied through the gateway to SNS.


  3. TopicArn: indicates the SNS topic that we're publishing to. In my design this would be a static value/endpoint so I'd prefer that the web-client not have to pass this too but if it were easier to do this that would be fine too.


I have tried lots of things but am just stuck. Would love to find a good code example somewhere but any help at all would be appreciated.


Wanted to add a little more context on my current attempt:


I have tried publishing my API and using Postman to try and get a valid response. Here's the postman screens(one for header vars, one for JSON body):


This results in the following error message:


   "Error": {
     "Code": "InvalidParameter",
     "Message": "Invalid parameter: TopicArn or TargetArn Reason: no value for required parameter",
     "Type": "Sender"
  "RequestId": "b33b7700-e8a3-58f7-8ebe-39e4e62b02d0"

the error seems to indicate that the TopicArn parameter is not being sent to SNS but I have included the following in API-Gateway:


5 个解决方案



I'm from the Api Gateway team.

我来自Api Gateway团队。

I believe there are a few formats for the HTTP request to the Publish API, but here's the one I used first:


AWS Region us-west-2

AWS Region us-west-2

AWS Service sns

AWS Service sns

AWS Subdomain


HTTP method POST


Action Publish


== query strings ==


Subject 'foo'
Message 'bar'
TopicArn 'arn:aws:sns:us-west-2:xxxxxxxxxxxx:test-api'


This worked for me to publish a message.


Let me know if you have further troubles.






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
  • 首先,即使您正在发送POST,您也无法像预期的那样在邮件正文中发送JSON消息
  • Instead you must URL Encode the JSON and pass it as query parameter
  • 相反,您必须URL编码JSON并将其作为查询参数传递
  • Also remember that the JSON you send should start with a root object of default which in SNS-world means the "default channel"
  • 还要记住,您发送的JSON应该以默认的根对象开头,在SNS-world中表示“默认通道”
  • 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:
  • 然后,最终Lambda获得了SNS事件,您还必须抽出大量噪音来获取JSON消息。为此我创建了我在Lambda函数中使用的以下函数:

 * 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;

 * This is the entry point for the lambda function
exports.handler = function handler(event, context) {
  parent.event = abstractSNS(event);



You could use API Gateway to invoke your Lambda function asynchronously by configuring it as an AWS service proxy. The configuration is basically the same you see in this GitHub sample, with the exception that the uri for the Lambda invocation changes to /invoke-async/ instead of just /invoke/

您可以使用API​​网关通过将其配置为AWS服务代理来异步调用Lambda函数。配置与您在此GitHub示例中看到的基本相同,但Lambda调用的uri更改为/ invoke-async /而不是/ invoke /



I am just speculating (haven't tried this myself), but I think you are not sending the message correctly...


Based on AWS's documentation here (https://docs.aws.amazon.com/sns/latest/api/API_Publish.html), you need to POST the message in what seems to be the application/x-www-form-urlencoded encoding like this:

根据此处的AWS文档(https://docs.aws.amazon.com/sns/latest/api/API_Publish.html),您需要在似乎是application / x-www-form-urlencoded的情况下发布消息像这样的编码:

POST https://sns.us-west-2.amazonaws.com/ HTTP/1.1

That is, the message body looks the way a browser would encode form data. Your message can be JSON formatted, but still needs to be encoded as if it was a form field (an awkward analogy :)).


Also, based on the common parameters documentation (https://docs.aws.amazon.com/sns/latest/api/CommonParameters.html), you have a number of additional required fields (the usual access key, signature and so on).

此外,基于通用参数文档(https://docs.aws.amazon.com/sns/latest/api/CommonParameters.html),您还有许多其他必填字段(通常的访问密钥,签名等等) )。

You have not specified what language you are writing your API Gateway in - there might be an AWS SDK for it that you can use, instead of trying to manually compose the REST requests).

您尚未指定编写API网关的语言 - 您可以使用可以使用的AWS SDK,而不是尝试手动编写REST请求)。



I would do it like:


WebApp --> Gateway --> Lambda ( Use Boto3 to publish in SNS ) --> SNS -->Lambda

WebApp - > Gateway - > Lambda(使用Boto3在SNS中发布) - > SNS - > Lambda

I think, things will be simpler.