使用API​​网关使用API​​网关发布SNS主题/多个lambda函数

时间:2016-07-06 17:18:32

标签: amazon-web-services aws-lambda amazon-sns aws-api-gateway

现在我的要求是,每当我通过API获取数据时,我都必须将其保存到2-3个不同的位置(例如,将其保存到我自己的数据库中,转换为某些BI服务,有时也会保存到日志数据库中)。

我不知道是否可以将单个资源和单个方法绑定到多个lambda函数中。所以,我的替代方法是,因为我已经知道如何通过订阅SNS主题来触发多个lambda函数,我想如果我能以某种方式从API网关发布到SNS主题,其余的将很容易。我目前的想法如下:

current implementation thinking

但问题是,我无法从API网关发布到SNS主题。我收到了等错误。

我的方法是,创建一个正常的SNS主题。然后,创建一个特殊的角色策略,如下所示:

TopicArn or TargetArn Reason: no value for required parameter

然后使用POST / GET方法创建API(我尝试了两种方法)并将SNS主题添加为AWS Service Proxy和Role as Execution角色。

2 个答案:

答案 0 :(得分:2)

您必须通过AWS API Gateway将TopicArn或TargetArn传递给SNS。有不同的方式可以存档。

1。    您可以创建方法请求参数,然后创建一个名为export class HttpLoading extends Http { constructor(private _ls: LoadingService, backend: ConnectionBackend, defaultOptions: RequestOptions) { super(backend, defaultOptions); } 的集成请求查询字符串参数,并将方法请求参数映射到它。

2。    您可以创建一个名为TopicArn/TargetArn的集成请求查询字符串参数,并将您的Arn作为静态值。

以下是AWS API Gateway提供的step by step instruction

答案 1 :(得分:0)

以下资源详细说明了直接API-网关到SNS的集成,没有有Lambda定义:

文章:Async API with API Gateway and SNS

代码/模板示例:Profit4Cloud(NL) API-to-SNS Exmaple code @Bitbucket

基本上,在网关的API说明中配置了x-amazon-apigateway-integration Swagger / OpenAPI'扩展'。请注意,网关扩展中引用了“ 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