且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用API​​ Gateway通过API Gateway发布SNS主题/多个Lambda函数

更新时间:2023-01-18 11:34:42

以下资源详细说明了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