AWS CloudFormation
User Guide (API 版本 2010-05-15)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

AWS::ApiGateway::Method

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

语法

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

JSON

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

YAML

Type: "AWS::ApiGateway::Method" Properties: ApiKeyRequired: Boolean AuthorizationType: String AuthorizerId: String HttpMethod: String Integration: Integration MethodResponses: - MethodResponse OperationName: String RequestModels: 字符串字符串 RequestParameters: 字符串布尔值 RequestValidatorId: String ResourceId: String RestApiId: String

属性

ApiKeyRequired

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

Required: No

Type: Boolean

更新要求无需中断

AuthorizationType

方法的授权类型。

Required: Yes. 如果您指定 AuthorizerId 属性,请为此属性指定 CUSTOM

Type: String

更新要求无需中断

AuthorizerId

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

Required: No

Type: String

更新要求无需中断

HttpMethod

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

Required: Yes

Type: String

更新要求无需中断

Integration

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

Required: No

Type: Amazon API Gateway 方法集成

更新要求无需中断

MethodResponses

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

Required: No

Type: Amazon API Gateway Method MethodResponse 属性类型的列表。

更新要求无需中断

OperationName

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

必需:否

类型:字符串

更新要求无需中断

RequestModels

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

Required: No

类型:键值对的映射

更新要求无需中断

RequestParameters

API 网关 接受的请求参数。将请求参数指定为键/值对 (字符串-布尔值映射),其中源作为键,布尔值作为值。布尔值指定是否需要参数。源必须匹配 method.request.location.name 格式,其中 locationquerystringpathheadername 为有效的唯一参数名称。

Required: No

类型:键值对的映射

更新要求无需中断

RequestValidatorId

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

必需:否

类型:字符串

更新要求无需中断

ResourceId

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

Required: Yes

Type: String

更新要求无需中断

RestApiId

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

Required: Yes

Type: String

更新要求无需中断

返回值

Ref

如果向 Ref 内部函数提供此资源的逻辑 ID,Ref 将返回方法 ID,例如 mysta-metho-01234b567890example

有关使用 Ref 功能的更多信息,请参阅参考

示例

模拟方法

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

JSON

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

YAML

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

Lambda 代理

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

注意

使用 AWS::Lambda::Permission 资源授予 API 网关 权限,以调用 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