Description
Configure stage access logs to trace request IDs and response status. Execution logs have a separate setting and help diagnose errors while API Gateway processes requests.
Potential impact
Logging only execution errors leaves gaps in the request history, including successful responses. That makes unusual calls and traffic changes harder to investigate.
Remediation
- Set a destination ARN and a format with a request ID in the stage’s
access_log_settings. Configure delivery permissions and retention, and keep sensitive request data out of logs. - For execution logging, set
aws_api_gateway_method_settings.settings.logging_levelon REST APIs ordefault_route_settings.logging_levelon WebSocket APIs. HTTP APIs have no equivalent execution log option. - Send a test request and look for its ID and status in the destination log group.
Examples
The excerpts require an existing REST API, deployment, log group, and CloudWatch log delivery permissions.
Before
hcl
resource "aws_api_gateway_stage" "example" {
stage_name = "dev"
rest_api_id = aws_api_gateway_rest_api.example.id
deployment_id = aws_api_gateway_deployment.example.id
}
resource "aws_api_gateway_method_settings" "example" {
rest_api_id = aws_api_gateway_rest_api.example.id
stage_name = aws_api_gateway_stage.example.stage_name
method_path = "*/*"
settings {
logging_level = "ERROR"
}
}
After
hcl
resource "aws_api_gateway_stage" "example" {
stage_name = "dev"
rest_api_id = aws_api_gateway_rest_api.example.id
deployment_id = aws_api_gateway_deployment.example.id
access_log_settings {
destination_arn = aws_cloudwatch_log_group.example.arn
format = jsonencode({ requestId = "$context.requestId", status = "$context.status" })
}
}
resource "aws_api_gateway_method_settings" "example" {
rest_api_id = aws_api_gateway_rest_api.example.id
stage_name = aws_api_gateway_stage.example.stage_name
method_path = "*/*"
settings {
metrics_enabled = true
logging_level = "ERROR"
}
}
Both examples set execution logging to ERROR for */*. The revision also enables access logs with request IDs and status, and enables method metrics.