Cloudfront Compression


I discovered that content delivered by AWS Cloudfront is not compressed, although I switched on compression. Taking a closer look at the documentation I noticed:

Content-Length header

The origin must include a Content-Length header in the response so CloudFront can determine whether the size of the file is in the range that CloudFront compresses. If the Content-Length header is missing, CloudFront won’t compress the file.

Adding this cors rule helped out:

resource "aws_s3_bucket" "website" {
  bucket = var.website_bucket_name
  acl = "public-read"

  force_destroy = true
  cors_rule {
    allowed_headers = ["Content-Length"]
    allowed_methods = ["GET"]
    allowed_origins = ["*"]
    expose_headers = ["ETag"]
    max_age_seconds = 3000
  }
}