[英]AWS SAM Template - Local Testing
我有一个AWS SAM模板,我正在尝试在本地进行测试然后进行部署。 运行本地测试(sam local start-api),但未验证有效负载。 这意味着我有一个RequestValidator,但是它没有验证任何东西。
然后,我尝试将YAML文件部署到AWS在那里进行测试,并且收到一条错误消息:
“无法创建变更集:服务员ChangeSetCreateComplete失败:服务员遇到终端故障状态:状态:失败。原因:转换AWS :: Serverless-2016-10-31失败,原因:无效的Serverless Application Specification文档。发现的错误数:1。标识为[BoilerPlateFunction]的资源无效。标识为[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网关抛出一个错误,但事实并非如此,它会让它通过。
多谢你们!
该函数已在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.