-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfe.tf
More file actions
136 lines (116 loc) · 3.45 KB
/
fe.tf
File metadata and controls
136 lines (116 loc) · 3.45 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
data "aws_iam_policy_document" "s3_bucket_policy" {
statement {
sid = "AllowCloudFrontServicePrincipalReadOnly"
principals {
type = "Service"
identifiers = ["cloudfront.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [module.cloudfront.cloudfront_distribution_arn]
}
effect = "Allow"
actions = ["s3:GetObject"]
resources = ["${module.s3_bucket.s3_bucket_arn}/*"]
}
}
module "s3_bucket" {
source = "git::https://github.com/terraform-aws-modules/terraform-aws-s3-bucket.git?ref=v5.2.0"
bucket_prefix = "igorovo-"
attach_policy = true
policy = data.aws_iam_policy_document.s3_bucket_policy.json
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
versioning = {
enabled = true
}
server_side_encryption_configuration = {
rule = {
apply_server_side_encryption_by_default = {
sse_algorithm = "AES256"
}
}
}
}
resource "aws_cloudfront_response_headers_policy" "index_html_no_cache" {
name = "NoCacheIndexHtml"
custom_headers_config {
items {
header = "Cache-Control"
value = "no-store, no-cache, must-revalidate"
override = true
}
items {
header = "Pragma"
value = "no-cache"
override = true
}
items {
header = "Expires"
value = "0"
override = true
}
}
}
module "cloudfront" {
source = "git::https://github.com/terraform-aws-modules/terraform-aws-cloudfront.git?ref=v5.0.0"
http_version = "http2and3"
create_origin_access_control = true
origin_access_control = {
s3_oac = {
description = "OAC for private S3 access"
origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
}
origin = {
s3 = {
domain_name = module.s3_bucket.s3_bucket_bucket_regional_domain_name
origin_access_control = "s3_oac"
}
}
default_cache_behavior = {
target_origin_id = "s3"
viewer_protocol_policy = "redirect-to-https"
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
compress = true
cache_policy_name = "Managed-CachingOptimized" # Optimized caching for static assets
use_forwarded_values = false # Have to be disabled for cache policy
}
ordered_cache_behavior = [
{
path_pattern = "/index.html"
target_origin_id = "s3"
viewer_protocol_policy = "redirect-to-https"
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
compress = true
response_headers_policy_id = aws_cloudfront_response_headers_policy.index_html_no_cache.id
cache_policy_name = "Managed-CachingDisabled"
use_forwarded_values = false
}
]
viewer_certificate = {
cloudfront_default_certificate = true
minimum_protocol_version = "TLSv1" # TLSv1.2_2021 is not supported with default cert
}
custom_error_response = [
{
error_code = 403
response_code = 200
response_page_path = "/index.html"
error_caching_min_ttl = 0
},
{
error_code = 404
response_code = 200
response_page_path = "/index.html"
error_caching_min_ttl = 0
}
]
}