-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcloudfront.tf
97 lines (80 loc) · 2.87 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
resource "aws_cloudfront_distribution" "this" {
provider = aws.us-east-1
origin {
domain_name = aws_s3_bucket.this.bucket_regional_domain_name
origin_id = local.s3_origin_id
origin_access_control_id = aws_cloudfront_origin_access_control.this.id
}
aliases = [
var.domain_name,
"www.${var.domain_name}"
]
enabled = true
is_ipv6_enabled = true
default_root_object = var.cloudfront_default_root_object
default_cache_behavior {
allowed_methods = [
"GET",
"HEAD",
]
cached_methods = [
"GET",
"HEAD",
]
target_origin_id = local.s3_origin_id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
# https://stackoverflow.com/questions/67845341/cloudfront-s3-etag-possible-for-cloudfront-to-send-updated-s3-object-before-t
min_ttl = var.cloudfront_min_ttl
default_ttl = var.cloudfront_default_ttl
max_ttl = var.cloudfront_max_ttl
}
price_class = var.cloudfront_price_class
restrictions {
geo_restriction {
restriction_type = "none"
}
}
dynamic "viewer_certificate" {
for_each = [aws_acm_certificate.this.id]
content {
acm_certificate_arn = aws_acm_certificate.this.arn
ssl_support_method = "sni-only"
minimum_protocol_version = var.cloudfront_minimum_protocol_version
}
}
dynamic "custom_error_response" {
for_each = var.cloudfront_custom_error_responses
content {
error_code = custom_error_response.value.error_code
response_code = custom_error_response.value.response_code
error_caching_min_ttl = custom_error_response.value.error_caching_min_ttl
response_page_path = custom_error_response.value.response_page_path
}
}
wait_for_deployment = false
depends_on = [
# Hope to avoid errors like
#
# │ Error: error creating CloudFront Distribution: InvalidViewerCertificate: The specified SSL certificate doesn't exist, isn't in us-east-1 region, isn't valid, or doesn't include a valid certificate chain.
# │ status code: 400, request id: 281f6901-fe5b-472b-a357-d918449f7b60
# │
# │ with module.static-website-s3-cloudfront-acm.aws_cloudfront_distribution.this,
# │ on ../../cloudfront.tf line 1, in resource "aws_cloudfront_distribution" "this":
# │ 1: resource "aws_cloudfront_distribution" "this" {
aws_acm_certificate.this,
aws_acm_certificate_validation.this
]
}
resource "aws_cloudfront_origin_access_control" "this" {
name = var.domain_name
description = "${var.domain_name} OAC"
origin_access_control_origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}