【问题标题】:Working example of AWS API Gateway integration with SNS, including MessageAttributesAWS API Gateway 与 SNS 集成的工作示例,包括 MessageAttributes
【发布时间】:2020-07-02 18:53:46
【问题描述】:

我有一个有效的 API Gateway 与 SNS 集成,包括 API Gateway 的 CloudWatch 日志记录(这需要一些了解)。我现在需要的最后一部分是让 SNS 消息包含MessageAttributes。在 AWS 文档中寻找一个完整的示例似乎很困难/如果不是不可能的话……有人可以分享一个工作示例吗?

这是我目前所拥有的:

  SfdcPlatformEventsApi:
    Type: AWS::Serverless::Api
    DependsOn:
      - SfdcPlatformEventsApiLogGroup
    Properties:
      Name: !Sub '${EnvironmentName}-${ApplicationName}-api'
      StageName: !Ref EnvironmentName
      AccessLogSetting:
        DestinationArn: !GetAtt SfdcPlatformEventsApiLogGroup.Arn
        # 'requestId' is useless... but it is required for AccessLogSetting
        Format: '{
                     "request_id": "$context.requestId",
                     "api_id": "$context.apiId",
                     "domain": "$context.domainPrefix"
                     "path": "$context.path",
                     "param": "$integration.request",
                     "http_method": "$context.httpMethod",
                     "source_ip": "$context.identity.sourceIp",
                     "user-agent": "$context.identity.userAgent",
                     "api_key": "$context.identity.apiKey",
                     "status": "$context.status",
                     "responseLatency": "$context.responseLatency",
                     "requestTime": "$context.requestTime",
                     "wafResponseCode": "$context.wafResponseCode",
                     "xrayTraceId": "$context.xrayTraceId",
                     "error_message": "$context.error.message",
                     "error.validationErrorString": "$context.error.validationErrorString"
                 }'
      MethodSettings:
        - DataTraceEnabled: true
          HttpMethod: '*'
          LoggingLevel: INFO
          ResourcePath: '/*'
          MetricsEnabled: true
      # enable xRay tracing
      TracingEnabled: true
      DefinitionBody:
        swagger: 2.0
        info:
          title: !Sub '${EnvironmentName}-${ApplicationName}-api'
        paths:
          /{proxy+}:
            post:
              responses:
                '202':
                  description: 'Published'
              x-amazon-apigateway-integration:
                type: aws
                httpMethod: POST
                uri: !Sub 'arn:aws:apigateway:${AWS::Region}:sns:action/Publish'
                credentials: !GetAtt SfdcPlatformEventsApiRole.Arn
                parameters:
                  name: proxy
                  in: path
                  required: true
                  type: string
                requestParameters:
                  method.request.path.proxy: true
                  integration.request.querystring.Type: 'method.request.body.attributes.type'
                  integration.request.querystring.MessageAttributes: 'method.request.body.attributes'
                  integration.request.querystring.Message: 'method.request.body'
                  integration.request.querystring.TopicArn: !Sub "'${SfdcPlatformEventsSnsQueue}'" #It looks funky here with the double and single quotes!! But it needs it!
                responses:
                  default:
                    statusCode: 202
              security:
                - API_KEY: []
              x-amazon-apigateway-api-key-source: HEADER
              x-amazon-apigateway-gateway-responses:
                ACCESS_DENIED:
                  statusCode: 403
                  responseTemplates:
                    application/json: !Ref AccessDeniedMsg
        securityDefinitions:
          API_KEY:
            type: 'apiKey'
            name: 'x-api-key'
            in: 'header'

【问题讨论】:

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


    【解决方案1】:

    我终于找到了正确的语法来传递 MessageAttributes。这是完整的工作版本 API 定义。我希望这可以省去其他人搜索大量 AWS 文档的麻烦。

    SfdcPlatformEventsApi:
        Type: AWS::Serverless::Api
        DependsOn:
          - SfdcPlatformEventsApiLogGroup
        Properties:
          Name: !Sub '${EnvironmentName}-${ApplicationName}-api'
          StageName: !Ref EnvironmentName
          AccessLogSetting:
            DestinationArn: !GetAtt SfdcPlatformEventsApiLogGroup.Arn
            # 'requestId' is useless... but it is required for AccessLogSetting
            Format: '{
                         "request_id": "$context.requestId",
                         "api_id": "$context.apiId",
                         "domain": "$context.domainPrefix"
                         "path": "$context.path",
                         "param": "$integration.request",
                         "http_method": "$context.httpMethod",
                         "source_ip": "$context.identity.sourceIp",
                         "user-agent": "$context.identity.userAgent",
                         "api_key": "$context.identity.apiKey",
                         "status": "$context.status",
                         "responseLatency": "$context.responseLatency",
                         "requestTime": "$context.requestTime",
                         "wafResponseCode": "$context.wafResponseCode",
                         "xrayTraceId": "$context.xrayTraceId",
                         "error_message": "$context.error.message",
                         "error.validationErrorString": "$context.error.validationErrorString"
                     }'
          MethodSettings:
            - DataTraceEnabled: true
              HttpMethod: '*'
              LoggingLevel: INFO
              ResourcePath: '/*'
              MetricsEnabled: true
          # enable xRay tracing
          TracingEnabled: true
          DefinitionBody:
            swagger: 2.0
            info:
              title: !Sub '${EnvironmentName}-${ApplicationName}-api'
            paths:
              /{proxy+}:
                post:
                  responses:
                    '202':
                      description: 'Published'
                  x-amazon-apigateway-integration:
                    type: aws
                    httpMethod: POST
                    uri: !Sub 'arn:aws:apigateway:${AWS::Region}:sns:action/Publish'
                    credentials: !GetAtt SfdcPlatformEventsApiRole.Arn
                    parameters:
                      name: proxy
                      in: path
                      required: true
                      type: string
                    requestParameters:
                      method.request.path.proxy: true
                      integration.request.querystring.MessageAttributes.entry.1.Name: "'sfdcObject'"
                      integration.request.querystring.MessageAttributes.entry.1.Value.StringValue: "method.request.body.sfdcObject" # The value must be top level inside body: 'method.request.body.attributes.sfdcObject' does not work
                      integration.request.querystring.MessageAttributes.entry.1.Value.DataType: "'String'"
                      integration.request.querystring.Message: 'method.request.body'
                      integration.request.querystring.TopicArn: !Sub "'${SfdcPlatformEventsSnsQueue}'"
                    responses:
                      default:
                        statusCode: 202
                  security:
                    - API_KEY: []
                  x-amazon-apigateway-api-key-source: HEADER
                  x-amazon-apigateway-gateway-responses:
                    ACCESS_DENIED:
                      statusCode: 403
                      responseTemplates:
                        application/json: !Ref AccessDeniedMsg
            securityDefinitions:
              API_KEY:
                type: 'apiKey'
                name: 'x-api-key'
                in: 'header'
    

    【讨论】:

      猜你喜欢
      • 2016-03-16
      • 2018-01-29
      • 2019-06-22
      • 2020-11-20
      • 1970-01-01
      • 2020-04-30
      • 2020-06-14
      • 1970-01-01
      • 2021-06-10
      相关资源
      最近更新 更多