现在我的要求是,每当我通过API获取数据时,我必须将其保存到2-3个不同的位置(例如,保存到我自己的数据库中,保存到一些BI服务中,有时还保存到日志数据库中)。
我不知道是否可以将单个资源和单个方法绑定到多个lambda函数中。所以,我的另一种方法是,因为我已经知道如何通过订阅SNS topic来触发多个lambda函数,我想如果我可以从API Gateway以某种方式发布到SNS topic,剩下的事情就会很容易。我现在的想法是:

但问题是,我无法从API网关发布到SNS主题。我收到了像TopicArn or TargetArn Reason: no value for required parameter这样的错误。
我的方法是,创建一个普通的SNS主题。然后,创建一个特殊角色策略,如下所示:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "StmtXXXXXXXXXXX",
"Effect": "Allow",
"Action": [
"sns:Publish",
"sns:Subscribe",
"sns:Unsubscribe"
],
"Resource": [
"SNS-TOPIC-ARN"
]
}
]
}然后创建一个带有POST/GET方法的API (我都尝试过了),并添加了SNS主题作为AWS服务代理和角色作为执行角色。
发布于 2018-10-15 22:21:14
以下资源有助于详细说明API-Gateway- to -SNS的直接集成,而无需Lambda定义:
文章:Async API with API Gateway and SNS
代码/模板示例:Profit4Cloud(NL) API-to-SNS Example code @Bitbucket
基本上,API Swagger/OpenAPI‘扩展’是在网关的x-amazon-apigateway-integration描述中配置的。注意,在网关扩展中引用了'ExampleTopic‘SNS工件(请参阅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发布于 2016-07-07 03:54:27
您必须通过AWS API Gateway向SNS传入TopicArn或TargetArn。有不同的方法可以实现这一点:
TopicArn/TargetArn的集成请求查询字符串参数,并将该方法请求参数映射到该参数。TopicArn/TargetArn的集成请求查询字符串参数,并将Arn设置为静态值。这是AWS API Gateway提供的step by step instruction。
https://stackoverflow.com/questions/38229941
复制相似问题