-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloudfront.tf
123 lines (103 loc) · 3.65 KB
/
cloudfront.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
locals {
acm_certificate_arn = local.create_certificate ? aws_acm_certificate.this[0].arn : var.certificate_arn
}
resource "aws_cloudfront_distribution" "this" {
origin {
origin_id = aws_s3_bucket.origin.id
origin_access_control_id = aws_cloudfront_origin_access_control.this.id
domain_name = aws_s3_bucket.origin.bucket_regional_domain_name
}
aliases = local.enable_custom_domain_name ? [var.domain_name] : null
enabled = true
comment = var.comment
price_class = var.price_class
default_root_object = var.index_document_default
wait_for_deployment = var.wait_for_deployment
dynamic "custom_error_response" {
for_each = var.cloudfront_custom_errors
content {
error_caching_min_ttl = custom_error_response.value["error_caching_min_ttl"]
error_code = custom_error_response.value["error_code"]
response_code = custom_error_response.value["response_code"]
response_page_path = custom_error_response.value["response_page_path"]
}
}
default_cache_behavior {
allowed_methods = var.cloudfront_allowed_methods
cached_methods = var.cloudfront_cached_methods
target_origin_id = aws_s3_bucket.origin.id
compress = var.enable_cdn_compression
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = var.min_ttl
default_ttl = var.default_ttl
max_ttl = var.max_ttl
}
ordered_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
compress = true
default_ttl = 0
max_ttl = 0
min_ttl = 0
path_pattern = var.index_document_default
smooth_streaming = false
target_origin_id = aws_s3_bucket.origin.id
viewer_protocol_policy = "redirect-to-https"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
ordered_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
compress = true
default_ttl = 0
max_ttl = 0
min_ttl = 0
path_pattern = "config.json"
smooth_streaming = false
target_origin_id = aws_s3_bucket.origin.id
viewer_protocol_policy = "redirect-to-https"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
restrictions {
geo_restriction {
restriction_type = "none"
locations = []
}
}
viewer_certificate {
# Certificates for cloudfront _must_ be created in us-east-1
cloudfront_default_certificate = !local.enable_custom_domain_name
acm_certificate_arn = local.enable_custom_domain_name ? local.acm_certificate_arn : null
ssl_support_method = local.enable_custom_domain_name ? "sni-only" : null
minimum_protocol_version = local.enable_custom_domain_name ? "TLSv1.2_2021" : null
}
logging_config {
bucket = aws_s3_bucket.log.bucket_domain_name
prefix = "cloudfront/"
}
tags = merge(local.tags, var.cloudfront_tags)
depends_on = [aws_s3_bucket_acl.log]
}
resource "aws_cloudfront_origin_access_control" "this" {
name = aws_s3_bucket.origin.id
description = "S3 Bucket accsss policy"
origin_access_control_origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}