AWS API Gateway Method
This page shows how to write Terraform and CloudFormation for API Gateway Method and write them securely.
aws_api_gateway_method (Terraform)
The Method in API Gateway can be configured in Terraform with the resource name aws_api_gateway_method. The following sections describe 4 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_api_gateway_method" "api_method_post_streams" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.api_resource_streams.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_method" "denied" {
http_method = "ANY"
authorization = "NONE"
}
resource "aws_api_gateway_method" "denied_2" {
resource "aws_api_gateway_method" "good_example" {
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
resource_id = aws_api_gateway_resource.MyDemoResource.id
http_method = "GET"
authorization = "AWS_IAM"
}
resource "aws_api_gateway_method" "poc_person_post_method" {
rest_api_id = aws_api_gateway_rest_api.poc_rest_api.id
resource_id = aws_api_gateway_resource.poc_person_resource.id
http_method = "POST"
authorization = "NONE"
api_key_required = false
Security Best Practices for aws_api_gateway_method
There is 1 setting in aws_api_gateway_method that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure that your API Gateway method blocks unwanted access
It is better that the API Gateway method does not allow public access.
Parameters
-
api_key_requiredoptional - bool -
authorizationrequired - string -
authorization_scopesoptional - set of string -
authorizer_idoptional - string -
http_methodrequired - string -
idoptional computed - string -
operation_nameoptional - string -
request_modelsoptional - map from string to string -
request_parametersoptional - map from string to bool -
request_validator_idoptional - string -
resource_idrequired - string -
rest_api_idrequired - string
Explanation in Terraform Registry
Provides a HTTP Method for an API Gateway Resource.
Tips: Best Practices for The Other AWS API Gateway Resources
In addition to the aws_api_gateway_method_settings, AWS API Gateway has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
aws_api_gateway_method_settings
Ensure that API Gateway stage-level cache is encrypted
It is better to enable the stage-level cache encryption which reduces the risk of data leakage.
aws_api_gateway_domain_name
Ensure to use modern TLS protocols
It is better to adopt TLS v1.2+.
aws_api_gateway_stage
Ensure to enable access logging of your API Gateway stage (v1)
It is better to enable the access logging of your API Gateway stage (v1).
AWS::ApiGateway::Method (CloudFormation)
The Method in ApiGateway can be configured in CloudFormation with the resource name AWS::ApiGateway::Method. The following sections describe how to use the resource and its parameters.
Example Usage from GitHub
An example could not be found in GitHub.
Parameters
-
ApiKeyRequiredoptional - Boolean -
AuthorizationScopesoptional - List -
AuthorizationTypeoptional - String -
AuthorizerIdoptional - String -
HttpMethodrequired - String -
Integrationoptional - Integration -
MethodResponsesoptional - List of MethodResponse -
OperationNameoptional - String -
RequestModelsoptional - Map -
RequestParametersoptional - Map -
RequestValidatorIdoptional - String -
ResourceIdrequired - String -
RestApiIdrequired - String
Explanation in CloudFormation Registry
The
AWS::ApiGateway::Methodresource creates API Gateway methods that define the parameters and body that clients must send in their requests.
Frequently asked questions
What is AWS API Gateway Method?
AWS API Gateway Method is a resource for API Gateway of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS API Gateway Method?
For Terraform, the trackit/aws-workflow-live-streaming, snyk-labs/infrastructure-as-code-goof and shisho-security/shisho-security source code examples are useful. See the Terraform Example section for further details.