首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用API Gateway发布SNS主题/使用API Gateway实现多个lambda函数

使用API Gateway发布SNS主题/使用API Gateway实现多个lambda函数
EN

Stack Overflow用户
提问于 2016-07-07 01:18:32
回答 2查看 4.9K关注 0票数 5

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

我不知道是否可以将单个资源和单个方法绑定到多个lambda函数中。所以,我的另一种方法是,因为我已经知道如何通过订阅SNS topic来触发多个lambda函数,我想如果我可以从API Gateway以某种方式发布到SNS topic,剩下的事情就会很容易。我现在的想法是:

但问题是,我无法从API网关发布到SNS主题。我收到了像TopicArn or TargetArn Reason: no value for required parameter这样的错误。

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

代码语言:javascript
复制
{
    "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服务代理和角色作为执行角色。

EN

回答 2

Stack Overflow用户

发布于 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)。

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2016-07-07 03:54:27

您必须通过AWS API Gateway向SNS传入TopicArn或TargetArn。有不同的方法可以实现这一点:

  1. 您可以创建一个方法请求参数,然后创建一个名为TopicArn/TargetArn的集成请求查询字符串参数,并将该方法请求参数映射到该参数。

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

这是AWS API Gateway提供的step by step instruction

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38229941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档