Difference between revisions of "Template:Official aws cloudfront distribution example"

From wikieduonline
Jump to navigation Jump to search
Line 55: Line 55:
 
     max_ttl                = 86400
 
     max_ttl                = 86400
 
   }
 
   }
<pre>
+
  # Cache behavior with precedence 0
+
  # Cache behavior with precedence 0
  ordered_cache_behavior {
+
  ordered_cache_behavior {
    path_pattern    = "/content/immutable/*"
+
    path_pattern    = "/content/immutable/*"
    allowed_methods  = ["GET", "HEAD", "OPTIONS"]
+
    allowed_methods  = ["GET", "HEAD", "OPTIONS"]
    cached_methods  = ["GET", "HEAD", "OPTIONS"]
+
    cached_methods  = ["GET", "HEAD", "OPTIONS"]
    target_origin_id = local.s3_origin_id
+
    target_origin_id = local.s3_origin_id
 
+
    forwarded_values {
+
    forwarded_values {
      query_string = false
+
      query_string = false
      headers      = ["Origin"]
+
      headers      = ["Origin"]  
 
+
      cookies {
+
      cookies {
        forward = "none"
+
        forward = "none"
      }
+
      }
    }
+
    }
 
+
    min_ttl                = 0
+
    min_ttl                = 0
    default_ttl            = 86400
+
    default_ttl            = 86400
    max_ttl                = 31536000
+
    max_ttl                = 31536000
    compress              = true
+
    compress              = true
    viewer_protocol_policy = "redirect-to-https"
+
    viewer_protocol_policy = "redirect-to-https"
  }
+
  }
 
+
  # Cache behavior with precedence 1
+
  # Cache behavior with precedence 1
  ordered_cache_behavior {
+
    ordered_cache_behavior {
    path_pattern    = "/content/*"
+
    path_pattern    = "/content/*"
    allowed_methods  = ["GET", "HEAD", "OPTIONS"]
+
    allowed_methods  = ["GET", "HEAD", "OPTIONS"]
    cached_methods  = ["GET", "HEAD"]
+
    cached_methods  = ["GET", "HEAD"]
    target_origin_id = local.s3_origin_id
+
    target_origin_id = local.s3_origin_id  
 
+
    forwarded_values {
+
    forwarded_values {
      query_string = false
+
      query_string = false
 
+
      cookies {
+
      cookies {
        forward = "none"
+
        forward = "none"
      }
+
      }
    }
+
    }
 
+
    min_ttl                = 0
+
    min_ttl                = 0
    default_ttl            = 3600
+
    default_ttl            = 3600
    max_ttl                = 86400
+
    max_ttl                = 86400
    compress              = true
+
    compress              = true
    viewer_protocol_policy = "redirect-to-https"
+
    viewer_protocol_policy = "redirect-to-https"
  }
+
  }
 
+
  price_class = "PriceClass_200"
+
  price_class = "PriceClass_200"
 
+
  restrictions {
+
  restrictions {
    geo_restriction {
+
    [[geo_restriction]] {
      restriction_type = "whitelist"
+
      restriction_type = "whitelist"
      locations        = ["US", "CA", "GB", "DE"]
+
      locations        = ["US", "CA", "GB", "DE"]
    }
+
    }
  }
+
  }
 
+
  tags = {
+
  tags = {
    Environment = "production"
+
    Environment = "production"
  }
+
  }
 
+
  viewer_certificate {
+
  [[viewer_certificate]] {
    cloudfront_default_certificate = true
+
    cloudfront_default_certificate = true
  }
+
  }
}
+
}
</pre>
 

Revision as of 14:20, 8 June 2023

resource "aws_s3_bucket" "b" {
  bucket = "mybucket"

  tags = {
    Name = "My bucket"
  }
}

resource "aws_s3_bucket_acl" "b_acl" {
  bucket = aws_s3_bucket.b.id
  acl    = "private"
}

locals {
  s3_origin_id = "myS3Origin"
}

resource "aws_cloudfront_distribution" "s3_distribution" {
  origin {
    domain_name              = aws_s3_bucket.b.bucket_regional_domain_name
    origin_access_control_id = aws_cloudfront_origin_access_control.default.id
    origin_id                = local.s3_origin_id
  }

  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"
  }

  aliases = ["mysite.example.com", "yoursite.example.com"]

  default_cache_behavior {
    allowed_methods  = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
    cached_methods   = ["GET", "HEAD"]
    target_origin_id = local.s3_origin_id

    forwarded_values {
      query_string = false 

      cookies {
        forward = "none"
      }
    }

    viewer_protocol_policy = "allow-all"
    min_ttl                = 0
    default_ttl            = 3600
    max_ttl                = 86400
  }

  # Cache behavior with precedence 0
  ordered_cache_behavior {
    path_pattern     = "/content/immutable/*"
    allowed_methods  = ["GET", "HEAD", "OPTIONS"]
    cached_methods   = ["GET", "HEAD", "OPTIONS"]
    target_origin_id = local.s3_origin_id

    forwarded_values {
      query_string = false
      headers      = ["Origin"] 

      cookies {
        forward = "none"
      }
    }

    min_ttl                = 0
    default_ttl            = 86400
    max_ttl                = 31536000
    compress               = true
    viewer_protocol_policy = "redirect-to-https"
  }

  # Cache behavior with precedence 1
   ordered_cache_behavior {
    path_pattern     = "/content/*"
    allowed_methods  = ["GET", "HEAD", "OPTIONS"]
    cached_methods   = ["GET", "HEAD"]
    target_origin_id = local.s3_origin_id 

    forwarded_values {
      query_string = false

      cookies {
        forward = "none"
      }
    }

    min_ttl                = 0
    default_ttl            = 3600
    max_ttl                = 86400
    compress               = true
    viewer_protocol_policy = "redirect-to-https"
  }

  price_class = "PriceClass_200"

  restrictions {
    geo_restriction {
      restriction_type = "whitelist"
      locations        = ["US", "CA", "GB", "DE"]
    }
  }

  tags = {
    Environment = "production"
  }

  viewer_certificate {
    cloudfront_default_certificate = true
  }
}

Advertising: