问题
Right now my requirement is, whenever I get data through API, I have to save it into 2-3 different places (for example, into my own DB, into some BI service and also sometimes into a logging DB).
I don't know if it's possible to bind a single resource and single method into multiple lambda functions or so. So, my alternate approach was, as I already know how to trigger multiple lambda functions by subscribing to SNS topic, I thought maybe if I can somehow publish to SNS topic from the API Gateway, the rest will be easy. My current thinking is something below:
But the problem is, I am not able to publish to SNS topic from the API Gateway. I am getting errors like TopicArn or TargetArn Reason: no value for required parameter
.
My approach is, create a normal SNS topic. Then, create a special role policy like below:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "StmtXXXXXXXXXXX",
"Effect": "Allow",
"Action": [
"sns:Publish",
"sns:Subscribe",
"sns:Unsubscribe"
],
"Resource": [
"SNS-TOPIC-ARN"
]
}
]
}
Then creating a API with POST/GET method (I tried both) and added SNS topic as AWS Service Proxy and the Role as Execution role.
回答1:
You have to pass in TopicArn or TargetArn to SNS via AWS API Gateway. There are different ways to achieve that:
1.
You can create a method request parameter, then create an integration request query string parameter named TopicArn/TargetArn
and map the method request parameter to it.
2.
You can create an integration request query string parameter named TopicArn/TargetArn
and set your Arn as a static value.
Here is the step by step instruction provided by AWS API Gateway.
回答2:
The following resource(s) go some way to detailing direct API-Gateway-to-SNS integration, without a Lambda definition:
Article: Async API with API Gateway and SNS
Code/template example: Profit4Cloud(NL) API-to-SNS Example code @Bitbucket
Basically, the a x-amazon-apigateway-integration
Swagger/OpenAPI 'extension' is configured within the gateway's API description. Notice that the 'ExampleTopic' SNS artifact is referenced within the gateway extension (see integration.request.querystring.TopicArn
).
AWSTemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Resources:
ExampleTopic:
Type: "AWS::SNS::Topic"
Properties:
TopicName: !Sub "${AWS::StackName}-example-topic"
ExampleAPI:
Type: "AWS::Serverless::Api"
Properties:
StageName: "prod"
DefinitionBody:
swagger: "2.0"
info:
title: !Sub "${AWS::StackName}-api"
paths:
/example-path:
post:
responses:
"202":
description: Accepted
x-amazon-apigateway-integration:
type: "aws"
httpMethod: "POST"
uri: !Sub "arn:aws:apigateway:${AWS::Region}:sns:action/Publish"
credentials: !GetAtt ExampleTopicAPIRole.Arn
requestParameters:
integration.request.querystring.Message: "method.request.body"
integration.request.querystring.TopicArn: !Sub "'${ExampleTopic}'"
responses:
default:
statusCode: 202
ExampleTopicAPIRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "apigateway.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: !Sub "${AWS::StackName}-example-topic-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action: "sns:Publish"
Effect: "Allow"
Resource: !Ref ExampleTopic
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
ExampleLambda:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs6.10
CodeUri: ./app
Events:
SNSMessage:
Type: SNS
Properties:
Topic: !Ref ExampleTopic
来源:https://stackoverflow.com/questions/38229941/using-api-gateway-to-publish-sns-topics-multiple-lambda-function-with-api-gate