Skip to content

API Gateway access logs show request size as blank after enabling custom logs

0

I have enabled custom access logging for an Amazon API Gateway stage. The logs are getting generated correctly, but the request size field is coming through as blank, even though requests are successfully reaching the backend.

I’m using standard $context variables in the log format and expected to see the request payload size populated. Other fields (request ID, status code, latency, etc.) appear as expected.

Is this expected behavior in API Gateway? Are there any specific $context variables or configurations required to capture request size in access logs? Does this behavior differ between REST API vs HTTP API, or based on payload type (JSON vs binary)?

Any guidance or best practices to correctly log request size in API Gateway would be appreciated

1 Answer
1
Accepted Answer

Based on the available context variables for API Gateway access logs, there is no standard variable specifically for request payload size. The logging variables available include $context.responseLength which captures the response payload length in bytes, but there isn't a corresponding $context.requestLength or similar variable for the incoming request size.

The blank field you're seeing is expected behavior because API Gateway's access log variables don't include a built-in parameter for request payload size. The standard variables you can use focus on request metadata (like request ID, request time, route key), caller information (source IP, user agent), and response characteristics (response length, status code, latency).

For HTTP APIs specifically, the available logging variables include:

  • Request identifiers and timing information
  • Response metrics like $context.responseLength and $context.responseLatency
  • Route and stage information
  • HTTP method and status codes

However, request body size is not among the supported variables.

If capturing request size is critical for your use case, you might need to consider alternative approaches such as:

  • Implementing custom logging within your backend integration to capture and log the request size
  • Using AWS X-Ray tracing (available for REST APIs) which may provide additional request details
  • Processing CloudWatch Logs with custom metrics to derive request size information from other available data

The behavior is consistent across API types - neither REST APIs nor HTTP APIs provide a native context variable for request payload size in access logs.
Sources
Customize HTTP API access logs - Amazon API Gateway
Amazon API Gateway controls - AWS Control Tower

answered 9 months ago

AWS
EXPERT

reviewed 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.