Review CloudFront request logging

Collect CloudFront request logs to investigate failures and abnormal traffic.

Description

CloudFront access logs show the requests viewers sent and how the edge responded.

Potential impact

Without those records, cache problems and unusual traffic are harder to trace.

Remediation

For legacy S3 standard logging, set the bucket domain and prefix in logging_config. Enable bucket ACLs, permit CloudFront log delivery and set retention. After test traffic, check for request records under the configured prefix.

Examples

The example uses myprefix and excludes cookies. Supply the required cache behavior and viewer certificate configuration. Check standard logging v2 separately if you use it.

Before

hcl
resource "aws_cloudfront_distribution" "example" {
  origin {
    domain_name = aws_s3_bucket.b.bucket_regional_domain_name
    origin_id   = local.s3_origin_id

    s3_origin_config {
      origin_access_identity = "origin-access-identity/cloudfront/ABCDEFG1234567"
    }
  }

  enabled             = true
  is_ipv6_enabled     = true
  comment             = "Some comment"
  default_root_object = "index.html"
}

After

hcl
resource "aws_cloudfront_distribution" "example" {
  origin {
    domain_name = aws_s3_bucket.b.bucket_regional_domain_name
    origin_id   = local.s3_origin_id

    s3_origin_config {
      origin_access_identity = "origin-access-identity/cloudfront/ABCDEFG1234567"
    }
  }

  enabled             = true
  is_ipv6_enabled     = true
  comment             = "Some comment"
  default_root_object = "index.html"

  logging_config {
    include_cookies = false
    bucket          = "mylogs.s3.amazonaws.com"
    prefix          = "myprefix"
  }
}

References