本文介绍了使用API​​ Gateway通过API Gateway发布SNS主题/多个Lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

我不知道是否可以将单个资源和单个方法绑定到多个lambda函数中.因此,我的另一种方法是,因为我已经知道如何通过订阅SNS主题来触发多个lambda函数,所以我想也许我可以通过API网关以某种方式发布到SNS主题,那么其余的工作就很容易了.我目前的想法是:

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:

但是问题是,我无法从API网关发布到SNS主题.我收到类似TopicArn or TargetArn Reason: no value for required parameter的错误.

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.

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

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"
            ]
        }
    ]
}

然后使用POST/GET方法创建一个API(我都尝试过),并添加了SNS主题作为AWS Service Proxy和角色作为执行角色.

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.

推荐答案

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

The following resource(s) go some way to detailing direct API-Gateway-to-SNS integration, without a Lambda definition:

文章:带有API网关和SNS

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

基本上,在网关的API描述中配置了x-amazon-apigateway-integration Swagger/OpenAPI'扩展'.请注意,网关扩展中引用了"ExampleTopic" SNS工件(请参见integration.request.querystring.TopicArn).

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

这篇关于使用API​​ Gateway通过API Gateway发布SNS主题/多个Lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 07:26