【问题标题】:AWS SAM Template - Local TestingAWS SAM 模板 - 本地测试
【发布时间】:2019-04-24 14:31:21
【问题描述】:

我有一个 AWS SAM 模板,我尝试在本地进行测试然后部署。本地测试运行(sam local start-api),但未验证有效负载。这意味着我有一个 RequestValidator,但它不验证任何东西。

然后,我尝试将 YAML 文件部署到 AWS 以在那里进行测试,我收到一条错误消息:

"创建变更集失败:Waiter ChangeSetCreateComplete 失败:服务员遇到终端故障状态状态:FAILED。 原因:转换 AWS::Serverless-2016-10-31 失败:无效 无服务器应用程序规范文档。发现的错误数: 1. id为[BoilerPlateFunction]的资源无效。 ID 为 [ApiEvent] 的事件无效。 Api 事件的 RestApiId 属性必须引用 同一模板中的有效资源。”

这是我的 yaml 文件,所以首先我希望能够使 RequestValidator 在我的本地工作,一旦完成,就知道我做错了什么以及为什么我无法部署:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-app

  Sample SAM Template for sam-app

Globals:
  Function:
    Timeout: 20
Parameters:
  operationName:
    Type: String
    Default: testoperationName
  restApiName:
    Type: String
    Default: testrestApiName
  validatorName:
    Type: String
    Default: testvalidatorName
  validateRequestBody:
    Type: String
    Default: testvalidateRequestBody
  validateRequestParameters:
    Type: String
    Default: true
Resources:
  BoilerPlateApi:
    Type: AWS::ApiGateway::Api
    Properties:
      Name: !Ref restApiName
  BoilerPlateFunctionMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      HttpMethod: ANY
      RestApiId: !Ref BoilerPlateApi
      RequestValidatorId: !Ref RequestValidator
      RequestParameters:
        method.request.querystring.test: true
  RequestValidator:
    Type: AWS::ApiGateway::RequestValidator
    Properties:
      Name: !Ref validatorName
      RestApiId: !Ref BoilerPlateApi
      ValidateRequestParameters: !Ref validateRequestParameters
  BoilerPlateFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: boilerplate/apiName
      Handler: index.handler
      Runtime: nodejs8.10
      Events:
        ApiEvent:
          Type: Api
          Properties:
            RestApiId: !Ref BoilerPlateApi
            Path: /hello
            Method: GET

同样,使用 sam local start-api 运行,我可以点击端点并执行 Lambda。但是如果我没有在查询字符串中包含“test”参数,我希望 API 网关会抛出错误,但它没有,它让它通过。

谢谢大家!

【问题讨论】:

    标签: amazon-web-services aws-serverless


    【解决方案1】:

    该函数已在 API 网关之前创建。 您可以在方法之前使用 DependsOn 参数来创建 API

    因此,只需将 BoilerPlateFunction 资源中的以下内容更改为:

      BoilerPlateFunction:
        DependsOn:
          - BoilerPlateApi
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: boilerplate/apiName
          Handler: index.handler
          Runtime: nodejs8.10
          Events:
            ApiEvent:
              Type: Api
              Properties:
                RestApiId: !Ref BoilerPlateApi
                Path: /hello
                Method: GET
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2018-04-15
      • 1970-01-01
      • 2020-02-07
      • 2020-11-11
      • 1970-01-01
      • 2018-06-14
      相关资源
      最近更新 更多