Review access logging on an API Gateway deployment stage

Configure access logging on the stage that serves an API Gateway deployment.

Description

aws_api_gateway_deployment captures a REST API configuration. Configure request access logs on the aws_api_gateway_stage that serves that deployment. The deployment description does not record requests.

Potential impact

Without access records from the serving stage, it is harder to trace failures or unusual calls to a particular deployment.

Remediation

Check that the stage’s deployment_id points to the intended deployment. In the stage template, set access_log_settings to the destination log group ARN and a format containing a request ID. Configure delivery permissions and retention. After applying the change, send a test request and look for it in that group.

Examples

The excerpts use a separate stage. Configure the REST API, methods, integrations, deployment dependencies, and log group separately.

Before

hcl
resource "aws_api_gateway_deployment" "example" {
  rest_api_id = aws_api_gateway_rest_api.example.id
}

resource "aws_api_gateway_stage" "example" {
  deployment_id = aws_api_gateway_deployment.example.id
  rest_api_id   = aws_api_gateway_rest_api.example.id
  stage_name    = "example"
}

After

hcl
resource "aws_api_gateway_deployment" "example" {
  rest_api_id = aws_api_gateway_rest_api.example.id
}

resource "aws_api_gateway_stage" "example" {
  deployment_id = aws_api_gateway_deployment.example.id
  rest_api_id   = aws_api_gateway_rest_api.example.id
  stage_name    = "example"

  access_log_settings {
    destination_arn = aws_cloudwatch_log_group.example.arn
    format = jsonencode({ requestId = "$context.requestId", status = "$context.status" })
  }
}

Both examples link the stage to the same deployment. The added stage settings select request IDs and response status for the access log. Check any other logging separately.

References