AWS::ApiGateway::Method
The AWS::ApiGateway::Method
resource creates 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" :[ String, ... ]
, "AuthorizationType" :String
, "AuthorizerId" :String
, "HttpMethod" :String
, "Integration" :Integration
, "MethodResponses" :[ MethodResponse, ... ]
, "OperationName" :String
, "RequestModels" :{
, "RequestParameters" :Key
:Value
, ...}{
, "RequestValidatorId" :Key
:Value
, ...}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:RequestParameters:
Key
:Value
RequestValidatorId:
Key
:Value
String
ResourceId:String
RestApiId:String
Properties
ApiKeyRequired
-
Indicates whether the method requires clients to submit a valid API key.
Required: No
Type: Boolean
Update requires: No interruption
AuthorizationScopes
-
A list of authorization scopes configured on the method. The scopes are used with a
COGNITO_USER_POOLS
authorizer 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 match 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
Update requires: No interruption
AuthorizationType
-
The method's authorization type. This parameter is required. For valid values, see Method in the API Gateway API Reference.
Note If you specify the
AuthorizerId
property, specifyCUSTOM
orCOGNITO_USER_POOLS
for this property.Required: No
Type: String
Update requires: No interruption
AuthorizerId
-
The identifier of the authorizer to use on this method. If you specify this property, specify
CUSTOM
orCOGNITO_USER_POOLS
for theAuthorizationType
property.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: Integration
Update requires: No interruption
MethodResponses
-
The responses that can be sent to the client who calls the method.
Required: No
Type: List of MethodResponse
Update requires: No interruption
OperationName
-
A friendly operation name for the method. For example, you can assign the
OperationName
ofListPets
for theGET /pets
method.Required: No
Type: String
Update requires: No interruption
RequestModels
-
The resources that are used for the request's content type. Specify request models as key-value pairs (string-to-string mapping), with a content type as the key and a
Model
resource name as the value. To use the same model regardless of the content type, specify$default
as the key.Required: No
Type: Map of String
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.location.name
, where the location is querystring, path, or header, and name is a valid, unique parameter name.Required: No
Type: Map of Boolean
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 values
Ref
When you pass the logical ID of this resource to the intrinsic Ref
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: !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.
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:put in the Amazon API Gateway REST API Reference