Review API Gateway stage logging

Configure stage logs so API requests and errors can be investigated.

Description

Use stage access logs to trace request IDs and response status. REST and WebSocket APIs also support execution logs for investigating processing errors. HTTP APIs do not provide that execution log setting.

Potential impact

Without stage request records, investigations may depend on other sources such as backend logs. Logging sensitive payloads or granting broad read access can expose request data.

Remediation

Set the log group ARN and a format containing $context.requestId in the WebSocket stage’s AccessLogSettings. Configure delivery permissions, retention, and read access. Send a test request and check the log. Configure execution log levels and alerts separately if needed.

Examples

MyApi and MyDeployment reference an existing API and deployment. Prepare the log group and replace the sample ARN.

Before

yaml
Resources:
  WebSocketStage:
    Type: AWS::ApiGatewayV2::Stage
    Properties:
      StageName: Prod
      ApiId: !Ref MyApi
      DeploymentId: !Ref MyDeployment

The Prod stage has no access log destination.

After

yaml
Resources:
  WebSocketStage:
    Type: AWS::ApiGatewayV2::Stage
    Properties:
      StageName: Prod
      ApiId: !Ref MyApi
      DeploymentId: !Ref MyDeployment
      AccessLogSettings:
        DestinationArn: arn:aws:logs:us-east-1:123456789012:log-group:api-gateway-prod
        Format: '{"requestId":"$context.requestId","ip":"$context.identity.sourceIp","routeKey":"$context.routeKey","status":"$context.status"}'

The format selects the request ID, source IP, route key, and response status.

References