|
| 1 | + |
| 2 | +locals { |
| 3 | + static_origin_id = "ashley-static-origin" |
| 4 | + s3_media_origin_id = "ashley-media-origin" |
| 5 | +} |
| 6 | + |
| 7 | +# Create an origin access identity that will allow CloudFront to access S3 |
| 8 | +# See bucket policies in s3.tf or documentation for more details: |
| 9 | +# https://www.terraform.io/docs/providers/aws/r/cloudfront_origin_access_identity.html |
| 10 | +resource "aws_cloudfront_origin_access_identity" "ashley_oai" { |
| 11 | + comment = "ashley origin for the ${terraform.workspace} environment" |
| 12 | +} |
| 13 | + |
| 14 | +resource "aws_cloudfront_distribution" "ashley_cloudfront_distribution" { |
| 15 | + # Origin for static distribution |
| 16 | + origin { |
| 17 | + domain_name = lookup(var.app_domain, terraform.workspace) |
| 18 | + origin_id = local.static_origin_id |
| 19 | + |
| 20 | + custom_origin_config { |
| 21 | + http_port = 80 |
| 22 | + https_port = 443 |
| 23 | + origin_protocol_policy = "https-only" |
| 24 | + origin_ssl_protocols = ["SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"] |
| 25 | + } |
| 26 | + } |
| 27 | + # Origin for the media S3 bucket |
| 28 | + origin { |
| 29 | + domain_name = aws_s3_bucket.ashley_media.bucket_domain_name |
| 30 | + origin_id = local.s3_media_origin_id |
| 31 | + |
| 32 | + s3_origin_config { |
| 33 | + origin_access_identity = aws_cloudfront_origin_access_identity.ashley_oai.cloudfront_access_identity_path |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + enabled = true |
| 38 | + is_ipv6_enabled = true |
| 39 | + wait_for_deployment = false |
| 40 | + |
| 41 | + # Allow public access by default, served by static bucket |
| 42 | + default_cache_behavior { |
| 43 | + allowed_methods = ["GET", "HEAD", "OPTIONS"] |
| 44 | + cached_methods = ["GET", "HEAD", "OPTIONS"] |
| 45 | + target_origin_id = local.static_origin_id |
| 46 | + |
| 47 | + forwarded_values { |
| 48 | + query_string = true |
| 49 | + headers = ["Access-Control-Request-Headers", "Access-Control-Request-Method", "Authorization", "Origin"] |
| 50 | + |
| 51 | + cookies { |
| 52 | + forward = "none" |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + min_ttl = 0 |
| 57 | + default_ttl = 3600 |
| 58 | + max_ttl = 86400 |
| 59 | + compress = true |
| 60 | + viewer_protocol_policy = "redirect-to-https" |
| 61 | + } |
| 62 | + |
| 63 | + # Media bucket: allow public access |
| 64 | + ordered_cache_behavior { |
| 65 | + path_pattern = "/media/*" |
| 66 | + allowed_methods = ["GET", "HEAD", "OPTIONS"] |
| 67 | + cached_methods = ["GET", "HEAD", "OPTIONS"] |
| 68 | + target_origin_id = local.s3_media_origin_id |
| 69 | + |
| 70 | + forwarded_values { |
| 71 | + query_string = false |
| 72 | + headers = ["Access-Control-Request-Headers", "Access-Control-Request-Method", "Authorization", "Origin"] |
| 73 | + |
| 74 | + cookies { |
| 75 | + forward = "none" |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + min_ttl = 0 |
| 80 | + default_ttl = 3600 |
| 81 | + max_ttl = 86400 |
| 82 | + compress = true |
| 83 | + viewer_protocol_policy = "redirect-to-https" |
| 84 | + } |
| 85 | + |
| 86 | + price_class = lookup(var.cloudfront_price_class, terraform.workspace, "PriceClass_100") |
| 87 | + |
| 88 | + restrictions { |
| 89 | + geo_restriction { |
| 90 | + restriction_type = "none" |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + tags = { |
| 95 | + Environment = terraform.workspace |
| 96 | + } |
| 97 | + |
| 98 | + viewer_certificate { |
| 99 | + cloudfront_default_certificate = true |
| 100 | + } |
| 101 | +} |
0 commit comments