amazon-web-services - 如何在我的模板中为 API 事件添加“/”?
问题描述
这是我的 lambda 资源:
Resources:
myFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
Description: Enter description of what this specific Lambda does
CodeUri: hello_world/build/
Handler: app.lambda_handler
Runtime: python2.7
Events:
testMethod:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: testMethod/
Method: POST
/:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /
Method: ANY
testMethod和“/”对应swagger文档中的路径。但似乎 cloudformation 让我使用“/”作为 api 事件:
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [myFunction/] is invalid. Logical ids must be alphanumeric.
这是我的 API 网关资源:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
DefinitionBody:
swagger: "2.0"
info:
version: "2018-09-12T06:21:35Z"
title: 'sdfsdfsd'
schemes:
- "https"
paths:
/:
x-amazon-apigateway-any-method:
produces:
- "application/json"
responses:
'200':
description: "200 response"
schema:
$ref: "#/definitions/Empty"
security:
- sigv4: []
x-amazon-apigateway-integration:
type: "aws_proxy"
httpMethod: "POST"
passthroughBehavior: "when_no_match"
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${myFunction}/invocations
responses:
default:
statusCode: "200"
contentHandling: "CONVERT_TO_TEXT"
/testMethod:
post:
produces:
- "application/json"
responses:
'200':
description: "200 response"
schema:
$ref: "#/definitions/Empty"
security:
- sigv4: []
x-amazon-apigateway-integration:
type: "aws_proxy"
httpMethod: "POST"
passthroughBehavior: "when_no_match"
uri:
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${myFunction}/invocations
responses:
default:
statusCode: "200"
contentHandling: "CONVERT_TO_TEXT"
securityDefinitions:
sigv4:
type: "apiKey"
name: "Authorization"
in: "header"
x-amazon-apigateway-authtype: "awsSigv4"
definitions:
Empty:
type: "object"
title: "Empty Schema"
解决方案
你是对的:你不能命名一个事件“/”,但你仍然可以把它作为你的路径。
尝试将您的“/”事件重命名为仅使用字母数字字符的其他名称。这不会改变事件的行为,并且会满足 CloudFormation 的约束。例子:
Events:
apiEvent:
Type: Api
Properties:
RestApiId: !Ref ApiGatewayApi
Path: /
Method: ANY
推荐阅读
- html - 如何限制 MaterializeCSS 库中每行的图像数量
- javascript - 对象作为 React 子级无效(找到:[object Promise])。但我没有回报承诺?
- json - @rollup/plugin-typescript 找不到 JSON 文件
- css - 如何在 React Native 中设置卡片样式?
- javascript - PixiJS renderTexture 抗锯齿
- mailkit - Mimekit / MailKit BSMTP 文件
- python - TypeError:'class Meta' 的属性无效:约束
- javascript - 从具有范围的类中传递函数
- java - 获取 Firebase 电话身份验证 OTP 时出错
- javascript - Expo TaskManager - 如何将更改的状态传递给外部函数?