Infra & Cloud terraform Intermediate

AWS CloudFront + S3 CDN

Terraform to serve an S3 static site through CloudFront with HTTPS and caching.

terraform
resource "aws_cloudfront_distribution" "cdn" {
  origin {
    domain_name = aws_s3_bucket.site.bucket_regional_domain_name
    origin_id   = "s3-origin"
  }
  enabled             = true
  default_root_object = "index.html"
  default_cache_behavior {
    viewer_protocol_policy = "redirect-to-https"
    allowed_methods  = ["GET", "HEAD"]
    cached_methods   = ["GET", "HEAD"]
    target_origin_id = "s3-origin"
    forwarded_values { query_string = false; cookies { forward = "none" } }
  }
  viewer_certificate { cloudfront_default_certificate = true }
}

Prerequisites

  • terraform >= 1.0
  • AWS provider
  • S3 bucket

Pro Tips

  • → Invalidate cache with aws cloudfront create-invalidation after deploy.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
terraform
Level
Intermediate
Read time
5 min
Published
Dec 2025
License
Commercial License

Newsletter

New modules every week, directly to your inbox.