首页 > 解决方案 > 无法使用 CloudFormation 让 API 代理与 SQS 一起使用

问题描述

我正在学习本教程,我可以使用控制台让这一切正常工作。我已将 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-servicesamazon-cloudformationaws-api-gatewayamazon-sqs

解决方案


我认为在我看来麻烦来自这里

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

它应该是这样的

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

推荐阅读