AWS::ApiGateway::Method
The AWS::ApiGateway::Method resource creates Amazon API Gateway (API Gateway) methods that define the parameters
and body that clients must send in their requests.
Syntax
To declare this entity in your AWS CloudFormation template, use the following syntax:
JSON
{ "Type" : "AWS::ApiGateway::Method", "Properties" : { "ApiKeyRequired" :Boolean, "AuthorizationScopes" : [], "AuthorizationType" :String, ...String, "AuthorizerId" :String, "HttpMethod" :String, "Integration" :Integration, "MethodResponses" : [MethodResponse, ...], "OperationName" :String, "RequestModels" : {String:String, ...}, "RequestParameters" : {String:Boolean, ...}, "RequestValidatorId" :String, "ResourceId" :String, "RestApiId" :String} }
YAML
Type: AWS::ApiGateway::Method Properties: ApiKeyRequired:BooleanAuthorizationScopes: -AuthorizationType:StringStringAuthorizerId:StringHttpMethod:StringIntegration:IntegrationMethodResponses: -MethodResponseOperationName:StringRequestModels:String:StringRequestParameters:String:BooleanRequestValidatorId:StringResourceId:StringRestApiId:String
Properties
ApiKeyRequired-
Indicates whether the method requires clients to submit a valid API key.
Required: No
Type: Boolean
Update requires: No interruption
AuthorizationType-
The method's authorization type. For valid values, see Method in the API Gateway API Reference.
Note
If you specify the
AuthorizerIdproperty, specifyCUSTOMfor this property.Required: Yes.
Type: String
Update requires: No interruption
AuthorizationScopes-
A list of authorization scopes configured on the method. The scopes are used with a
COGNITO_USER_POOLSauthorizer to authorize the method invocation. The authorization works by matching the method scopes against the scopes parsed from the access token in the incoming request. The method invocation is authorized if any method scopes matches a claimed scope in the access token. Otherwise, the invocation is not authorized. When the method scope is configured, the client must provide an access token instead of an identity token for authorization purposes.Required: No
Type: List of String values
Update requires: No interruption
AuthorizerId-
The identifier of the authorizer to use on this method. If you specify this property, specify
CUSTOMfor theAuthorizationTypeproperty.Required: No
Type: String
Update requires: No interruption
HttpMethod-
The HTTP method that clients use to call this method.
Required: Yes
Type: String
Update requires: No interruption
Integration-
The backend system that the method calls when it receives a request.
Required: No
Type: Amazon API Gateway Method Integration
Update requires: No interruption
MethodResponses-
The responses that can be sent to the client who calls the method.
Required: No
Type: List of Amazon API Gateway Method MethodResponse property types.
Update requires: No interruption
OperationName-
A friendly operation name for the method. For example, you can assign the
OperationNameof ListPets for theGET /petsmethod.Required: No
Type: String
Update requires: No interruption
RequestModels-
The resources that are used for the response's content type. Specify response models as key-value pairs (string-to-string mapping), with a content type as the key and a
Modelresource name as the value.Required: No
Type: Mapping of key-value pairs
Update requires: No interruption
RequestParameters-
The request parameters that API Gateway accepts. Specify request parameters as key-value pairs (string-to-Boolean mapping), with a source as the key and a Boolean as the value. The Boolean specifies whether a parameter is required. A source must match the format
method.request., where thelocation.namelocationisquerystring,path, orheader, andnameis a valid, unique parameter name.Required: No
Type: Mapping of key-value pairs
Update requires: No interruption
RequestValidatorId-
The ID of the associated request validator.
Required: No
Type: String
Update requires: No interruption
ResourceId-
The ID of an API Gateway resource. For root resource methods, specify the RestApi root resource ID, such as
{ "Fn::GetAtt": ["MyRestApi", "RootResourceId"] }.Required: Yes
Type: String
Update requires: No interruption
RestApiId-
The ID of the RestApi resource in which API Gateway creates the method.
Required: Yes
Type: String
Update requires: No interruption
Return Value
Ref
When the logical ID of this resource is provided to the Ref intrinsic function, Ref returns the method ID, such as mysta-metho-01234b567890example.
For more information about using the Ref function, see Ref.
Examples
Mock Method
The following example creates a mock GET method for the MyApi API.
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: Fn::GetAtt: - "MyApi" - "RootResourceId" HttpMethod: "GET" AuthorizationType: "NONE" Integration: Type: "MOCK"
Lambda Proxy
The following example creates a proxy resource to enable clients to call a Lambda
function with a single integration setup on a catch-all ANY method. The
Uri property specifies the Lambda function. For more information about Lambda
proxy integration and a sample Lambda function, see Create an API with Lambda Proxy Integration through a Proxy Resource in the
API Gateway Developer Guide.
Note
Use the AWS::Lambda::Permission resource to grant API Gateway permission to invoke your Lambda function.
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
Associated Request Validator
The following example creates a REST API, method, and request validator, and associates the request validator with the method. It also lets you specify how to convert the request payload.
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
See Also
-
Method in the Amazon API Gateway REST API Reference
