【问题标题】:Can't get API proxy to work with SQS using CloudFormation无法使用 CloudFormation 让 API 代理与 SQS 一起使用
【发布时间】:2021-03-23 05:14:37
【问题描述】:

我正在关注this tutorial,我可以使用控制台让这一切正常工作。我已将 SQS 队列和 API 创建转换为 CloudFormation,但无法使 API 方法集成正常工作。我已经使用Former2将工作方法转换为CloudFormation(如下所示),但我继续收到此错误:

Invalid mapping expression specified: Validation Result: 
warnings : [],
errors : [Invalid mapping expression specified: application/x-www-form-urlencoded] 
(Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; 
Request ID: 1e40fc86-af05-4698-a91a-c1fa8930ee10; Proxy: null)

我的 POST 方法的 CloudFormation 模板如下:

  ApiPostMsgMethod:
    DependsOn: 'SqsQueue'
    Type: AWS::ApiGateway::Method
    Properties: 
      ApiKeyRequired: false
      AuthorizationType: NONE
      HttpMethod: POST
      Integration:
          Credentials: !GetAtt ApiSqsSendMsgRole.Arn
          IntegrationHttpMethod: POST
          IntegrationResponses:
            - StatusCode: '200'
          PassthroughBehavior: NEVER
          RequestParameters: 
            integration.request.header.Content-Type: 'application/x-www-form-urlencoded'
          RequestTemplates: 
            application/json : 'Action=SendMessage&MessageBody=$input.body'
          TimeoutInMillis: 1200
          Type: AWS
          Uri: !Join
            - ""
            - - !Sub "arn:aws:apigateway:${AWS::Region}:sqs:path/${AWS::AccountId}/"
              - !Sub ${EnvironmentName}-queue
      MethodResponses:
          - StatusCode: 200
      OperationName: PostSqsItem
      ResourceId: !Ref SqsResource
      RestApiId: !Ref SqsRestApi

Former2 从有效的 POST 方法返回以下模板:

    httpMethod: POST
    authorizationType: NONE
    apiKeyRequired: false
    requestParameters: <empty object>
    methodResponses:
        200:
            statusCode: 200
            responseModels:
                application/json: Empty
    methodIntegration:
        type: AWS
        httpMethod: POST
        uri: arn:aws:apigateway:###:sqs:path/###/###
        credentials: arn:aws:iam::###:role/###
        requestParameters:
            integration.request.header.Content-Type: 'application/x-www-form-urlencoded'
        requestTemplates:
            application/json: Action=SendMessage&MessageBody=$input.body
        passthroughBehavior: NEVER
        timeoutInMillis: 29000
        cacheNamespace: ###
        integrationResponses:
            200:
                statusCode: 200
                responseTemplates:
                    
    restApiId: ###
    resourceId: ###

我的引用和替换似乎正常工作,但我仍然收到此集成错误。我错过了什么吗?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation aws-api-gateway amazon-sqs


    【解决方案1】:

    我认为麻烦似乎来自这里

    integration.request.header.Content-Type: 'application/x-www-form-urlencoded'
    

    应该是这样的

    integration.request.header.Content-Type: "'application/x-www-form-urlencoded'"
    

    【讨论】:

    • 是的,做到了。微妙,我没有在文档中看到这一点。
    猜你喜欢
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2014-10-31
    相关资源
    最近更新 更多