Description
Check authentication on each protected REST method. A method using IAM authentication needs no separate authorizer. For Lambda or Cognito, the authorizer and method must belong to the same API.
Potential impact
Inconsistent settings can hide an unprotected method and expose a sensitive operation to unintended callers.
Remediation
Configure aws_api_gateway_authorizer for the target REST API. On the method, set authorization to CUSTOM for Lambda or COGNITO_USER_POOLS for Cognito and set the matching authorizer_id. For Lambda, grant the role that API Gateway assumes permission to invoke the authorizer function. Review the planned configuration and test both allowed and denied requests.
Examples
These excerpts omit the Lambda function, invocation role and method settings. The first assumes a separate API named demo exists.
Before
resource "aws_api_gateway_authorizer" "demo" {
name = "demo"
rest_api_id = aws_api_gateway_rest_api.demo.id
authorizer_uri = aws_lambda_function.authorizer.invoke_arn
authorizer_credentials = aws_iam_role.invocation_role.arn
}
resource "aws_api_gateway_rest_api" "demo2" {
name = "auth-demo"
}
After
resource "aws_api_gateway_authorizer" "demo" {
name = "demo"
rest_api_id = aws_api_gateway_rest_api.demo.id
authorizer_uri = aws_lambda_function.authorizer.invoke_arn
authorizer_credentials = aws_iam_role.invocation_role.arn
}
resource "aws_api_gateway_rest_api" "demo" {
name = "auth-demo"
}
The first authorizer references demo, while the API shown is demo2. The revision references the API shown, demo. Attach it to the protected method to enforce authentication.