API网关SAM指定HTTP GET:500内部服务器错误

时间:2018-10-19 18:37:00

标签: amazon-web-services aws-api-gateway sam

当我尝试使用SAM定义的GET端点时,总是得到500 Internal Server Error

我能够定义一个有效的POST请求。 对于GET请求,它向我显示: Lambda invocation failed with status: 403 Execution failed due to configuration error:

我认为在定义API网关的DefinitionBody时出现了问题。

Globals:
  Api:
    Cors:
      AllowMethods: "'GET,POST,OPTIONS'"
      AllowHeaders: "'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'"
      AllowOrigin: "'*'"
Resources:
  getFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      ...
      Events:
        GetApi:
          Type: Api
          Properties:
            Path: /tips
            Method: GET
            RestApiId: !Ref MyApi
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      EndpointConfiguration: REGIONAL
      DefinitionBody:
        swagger: "2.0"
        info:
          title: "API"
        paths:
          /tips:
            get:
              x-amazon-apigateway-integration:
                uri:
                  Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${getFunction.Arn}/invocations
                responses: {}
                httpMethod: "POST"
                type: "aws_proxy"

我已经尝试过将x-amazon-apigateway-integration上的httpMethod更改为“ GET”,但这不能解决我的问题。

在AWS Lambda控制台中,我看到Lambda和Api网关已链接,但是我无法通过API网关调用Lambda。我可以在控制台中通过“测试事件”成功执行Lambda。绝对是API网关方面的东西。

有人可以验证我在做什么吗?

1 个答案:

答案 0 :(得分:0)

进一步研究之后,我发现必须实现GET路径的/tips方法,如下所示:

        get:
          responses:
            '200':
              description: 200 response
              headers:
                Content-Type:
                  type: string
          produces:
          - application/json
          x-amazon-apigateway-integration:
            uri:
              Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${getTipsFunction.Arn}/invocations"
            passthroughBehavior:  when_no_match
            responses:
              default:
                statusCode: 200
                responseParameters:
                  method.response.header.Content-Type: integration.response.header.content-type
            httpMethod: POST
            type: aws

这解决了问题。