AWS::ApiGateway::Method - AWS CloudFormation
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

AWS::ApiGateway::Method

AWS::ApiGateway::Method 资源创建 API Gateway 方法,这些方法定义客户端必须在其请求中发送的参数和正文。

语法

要在 AWS CloudFormation 模板中声明此实体,请使用以下语法:

JSON

{ "Type" : "AWS::ApiGateway::Method", "Properties" : { "ApiKeyRequired" : Boolean, "AuthorizationScopes" : [ String, ... ], "AuthorizationType" : String, "AuthorizerId" : String, "HttpMethod" : String, "Integration" : Integration, "MethodResponses" : [ MethodResponse, ... ], "OperationName" : String, "RequestModels" : {Key : Value, ...}, "RequestParameters" : {Key : Value, ...}, "RequestValidatorId" : String, "ResourceId" : String, "RestApiId" : String } }

YAML

Type: AWS::ApiGateway::Method Properties: ApiKeyRequired: Boolean AuthorizationScopes: - String AuthorizationType: String AuthorizerId: String HttpMethod: String Integration: Integration MethodResponses: - MethodResponse OperationName: String RequestModels: Key : Value RequestParameters: Key : Value RequestValidatorId: String ResourceId: String RestApiId: String

属性

ApiKeyRequired

指示方法是否要求客户端提交有效的 API 密钥。

必需:否

类型:布尔值

Update requires: No interruption

AuthorizationScopes

在方法上配置的授权范围列表。这些范围与 COGNITO_USER_POOLS 授权方一起使用以对方法调用进行授权。授权的工作方式是,将方法范围与从传入请求中的访问令牌解析的范围进行匹配。如果任何方法范围与访问令牌中声明的范围匹配,则会对方法调用进行授权。否则,不会对调用进行授权。在配置方法范围时,客户端必须提供访问令牌以进行授权,而不是提供身份令牌。

必需:否

类型:字符串列表

Update requires: No interruption

AuthorizationType

方法的授权类型。此参数为必需参数。有关有效值,请参阅 API Gateway API 参考中的 Method

注意

如果您指定 AuthorizerId 属性,请为此属性指定 CUSTOMCOGNITO_USER_POOLS

必需:否

类型:字符串

Update requires: No interruption

AuthorizerId

要对此方法使用的授权方的标识符。如果您指定此属性,请为 AuthorizationType 属性指定 CUSTOMCOGNITO_USER_POOLS

必需:否

类型:字符串

Update requires: No interruption

HttpMethod

客户端用于调用此方法的 HTTP 方法。

必需:是

类型:字符串

Update requires: No interruption

Integration

该方法在收到请求时调用的后端系统。

必需:否

类型Integration

Update requires: No interruption

MethodResponses

可发送到调用方法的客户端的响应。

必需:否

类型MethodResponse 的列表

Update requires: No interruption

OperationName

方法的易记操作名称。例如,您可以为 GET /pets 方法的 ListPets 分配 OperationName

必需:否

类型:字符串

Update requires: No interruption

RequestModels

用于请求的内容类型的资源。将请求模型指定为键/值对(字符串到字符串映射),其中内容类型作为键,Model 资源名称作为值。

必需:否

类型:字符串的映射

Update requires: No interruption

RequestParameters

API Gateway 接受的请求参数。将请求参数指定为键/值对 (字符串-布尔值映射),其中源作为键,布尔值作为值。布尔值指定是否需要参数。源必须与 method.request.location.name 格式匹配,其中 location 为查询字符串、路径或标头,而 name 为有效的唯一参数名称。

必需:否

类型:布尔值的映射

Update requires: No interruption

RequestValidatorId

关联的请求验证程序的 ID。

必需:否

类型:字符串

Update requires: No interruption

ResourceId

API Gateway 资源的 ID。对于根资源方法,请指定 RestApi 根资源 ID,例如 { "Fn::GetAtt": ["MyRestApi", "RootResourceId"] }

必需:是

类型:字符串

Update requires: No interruption

RestApiId

API Gateway 从中创建方法的 RestApi 资源的 ID。

必需:是

类型:字符串

Update requires: No interruption

返回值

Ref

在将此资源的逻辑 ID 传递给内部 Ref 函数时,Ref 返回 方法 ID,例如 mysta-metho-01234b567890example

For more information about using the Ref function, see Ref.

示例

模拟方法

下面的示例为 MyApi API 创建模拟 GET 方法。

JSON

{ "MockMethod": { "Type": "AWS::ApiGateway::Method", "Properties": { "RestApiId": { "Ref": "MyApi" }, "ResourceId": { "Fn::GetAtt": [ "MyApi", "RootResourceId" ] }, "HttpMethod": "GET", "AuthorizationType": "NONE", "Integration": { "Type": "MOCK" } } } }

YAML

MockMethod: Type: 'AWS::ApiGateway::Method' Properties: RestApiId: !Ref MyApi ResourceId: !GetAtt - MyApi - RootResourceId HttpMethod: GET AuthorizationType: NONE Integration: Type: MOCK

Lambda 代理

以下示例创建一个代理资源以使客户端能够通过单独的集成设置在“捕获全部”ANY 方法中调用 Lambda 函数。Uri 属性指定 Lambda 函数。有关 Lambda 代理集成和示例 Lambda 函数的更多信息,请参阅 API Gateway 开发人员指南中的通过代理资源用 Lambda 代理集成创建 API

注意

使用 AWS::Lambda::Permission 资源向 API Gateway 授予调用 Lambda 函数的权限。

JSON

{ "ProxyResource": { "Type": "AWS::ApiGateway::Resource", "Properties": { "RestApiId": { "Ref": "LambdaSimpleProxy" }, "ParentId": { "Fn::GetAtt": [ "LambdaSimpleProxy", "RootResourceId" ] }, "PathPart": "{proxy+}" } }, "ProxyResourceANY": { "Type": "AWS::ApiGateway::Method", "Properties": { "RestApiId": { "Ref": "LambdaSimpleProxy" }, "ResourceId": { "Ref": "ProxyResource" }, "HttpMethod": "ANY", "AuthorizationType": "NONE", "Integration": { "Type": "AWS_PROXY", "IntegrationHttpMethod": "POST", "Uri": { "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaForSimpleProxy.Arn}/invocations" } } } } }

YAML

ProxyResource: Type: 'AWS::ApiGateway::Resource' Properties: RestApiId: !Ref LambdaSimpleProxy ParentId: !GetAtt - LambdaSimpleProxy - RootResourceId PathPart: '{proxy+}' ProxyResourceANY: Type: 'AWS::ApiGateway::Method' Properties: RestApiId: !Ref LambdaSimpleProxy ResourceId: !Ref ProxyResource HttpMethod: ANY AuthorizationType: NONE Integration: Type: AWS_PROXY IntegrationHttpMethod: POST Uri: !Sub >- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaForSimpleProxy.Arn}/invocations

关联的请求验证程序

以下示例创建一个 REST API、方法和请求验证程序,并将请求验证程序与该方法相关联。它还允许您指定如何转换请求负载。

JSON

{ "Parameters": { "contentHandling": { "Type": "String" }, "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": { "RestApi": { "Type": "AWS::ApiGateway::RestApi", "Properties": { "Name": { "Ref": "restApiName" } } }, "Method": { "Type": "AWS::ApiGateway::Method", "Properties": { "HttpMethod": "POST", "ResourceId": { "Fn::GetAtt": [ "RestApi", "RootResourceId" ] }, "RestApiId": { "Ref": "RestApi" }, "AuthorizationType": "NONE", "Integration": { "Type": "MOCK", "ContentHandling": { "Ref": "contentHandling" }, "IntegrationResponses": [ { "ContentHandling": { "Ref": "contentHandling" }, "StatusCode": 400 } ] }, "RequestValidatorId": { "Ref": "RequestValidator" }, "OperationName": { "Ref": "operationName" } } }, "RequestValidator": { "Type": "AWS::ApiGateway::RequestValidator", "Properties": { "Name": { "Ref": "validatorName" }, "RestApiId": { "Ref": "RestApi" }, "ValidateRequestBody": { "Ref": "validateRequestBody" }, "ValidateRequestParameters": { "Ref": "validateRequestParameters" } } } }, "Outputs": { "RootResourceId": { "Value": { "Fn::GetAtt": [ "RestApi", "RootResourceId" ] } } } }

YAML

Parameters: contentHandling: Type: String 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: RestApi: Type: AWS::ApiGateway::RestApi Properties: Name: !Ref restApiName Method: Type: AWS::ApiGateway::Method Properties: HttpMethod: POST ResourceId: !GetAtt RestApi.RootResourceId RestApiId: !Ref RestApi AuthorizationType: NONE Integration: Type: MOCK ContentHandling: !Ref contentHandling IntegrationResponses: - ContentHandling: !Ref contentHandling StatusCode: 400 RequestValidatorId: !Ref RequestValidator OperationName: !Ref operationName RequestValidator: Type: AWS::ApiGateway::RequestValidator Properties: Name: !Ref validatorName RestApiId: !Ref RestApi ValidateRequestBody: !Ref validateRequestBody ValidateRequestParameters: !Ref validateRequestParameters Outputs: RootResourceId: Value: !GetAtt RestApi.RootResourceId

另请参阅

  • Amazon API Gateway REST API 参考中的 method:put