-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket_ondemand_derivatives.tf
155 lines (125 loc) · 4.37 KB
/
bucket_ondemand_derivatives.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#
# A bucket just for our on-demand derivatives, serves as a kind of cache, has
# lifecycle rules to delete ones that haven't been accessed in a while.
#
# Doesn't need a public policy cause we just set public-read ACLs on individual objects.
resource "aws_s3_bucket" "ondemand_derivatives" {
bucket = "${local.name_prefix}-ondemand-derivatives"
lifecycle {
prevent_destroy = true
}
tags = {
"service" = local.service_tag
"use" = "cache"
"S3-Bucket-Name" = "${local.name_prefix}-ondemand-derivatives"
}
}
resource "aws_s3_bucket_lifecycle_configuration" "ondemand_derivatives" {
bucket = aws_s3_bucket.ondemand_derivatives.id
rule {
status = "Enabled"
id = "Expire files"
expiration {
days = 20
expired_object_delete_marker = false
}
}
rule {
status = "Enabled"
id = "scihist-digicoll-${terraform.workspace}-ondemand-derivatives-IA-Rule"
transition {
days = 30
storage_class = "STANDARD_IA"
}
}
}
resource "aws_s3_bucket_policy" "ondemand_derivatives" {
bucket = aws_s3_bucket.ondemand_derivatives.id
policy = templatefile("templates/s3_cloudfront_access_policy.tftpl",
{
bucket_name : aws_s3_bucket.ondemand_derivatives.id,
cloudfront_arn : aws_cloudfront_distribution.ondemand_derivatives.arn
})
}
resource "aws_s3_bucket_public_access_block" "ondemand_derivatives" {
bucket = aws_s3_bucket.ondemand_derivatives.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_cloudfront_distribution" "ondemand_derivatives" {
comment = "${terraform.workspace}-ondemand-derivatives S3"
enabled = true
is_ipv6_enabled = true
http_version = "http2and3"
# Only North America/Europe to save money
price_class = "PriceClass_100"
default_cache_behavior {
allowed_methods = [
"GET",
"HEAD",
"OPTIONS",
]
cached_methods = [
"GET",
"HEAD",
"OPTIONS",
]
compress = true
target_origin_id = aws_s3_bucket.ondemand_derivatives.bucket_regional_domain_name
viewer_protocol_policy = "https-only"
cache_policy_id = aws_cloudfront_cache_policy.caching-optimized-plus-s3-params.id
response_headers_policy_id = data.aws_cloudfront_response_headers_policy.Managed-CORS-with-preflight-and-SecurityHeadersPolicy.id
}
origin {
connection_attempts = 3
connection_timeout = 1
domain_name = aws_s3_bucket.ondemand_derivatives.bucket_regional_domain_name
origin_id = aws_s3_bucket.ondemand_derivatives.bucket_regional_domain_name
# Sign requests for access to non-public bucket
origin_access_control_id = aws_cloudfront_origin_access_control.signing-s3.id
}
restrictions {
geo_restriction {
locations = []
restriction_type = "none"
}
}
viewer_certificate {
cloudfront_default_certificate = true
}
# Tag same as bucket origin to aggregate costs together
tags = {
"service" = local.service_tag
"use" = "ondemand-derivatives"
"S3-Bucket-Name" = "${local.name_prefix}-ondemand-derivatives"
"Cloudfront-Distribution-Origin-Id" = "${terraform.workspace}-ondemand-derivatives.s3"
}
logging_config {
bucket = aws_s3_bucket.chf-logs.bucket_domain_name
include_cookies = false
prefix = "cloudfront_access_logs/${terraform.workspace}-ondemand-derivatives/"
}
}
resource "aws_s3_bucket_versioning" "ondemand_derivatives" {
bucket = aws_s3_bucket.ondemand_derivatives.id
versioning_configuration {
status = "Disabled"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "ondemand_derivatives" {
bucket = aws_s3_bucket.ondemand_derivatives.id
rule {
bucket_key_enabled = false
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
# $ terraform import aws_s3_bucket_logging.originals_logging scihist-digicoll-staging-originals
resource "aws_s3_bucket_logging" "ondemand_derivatives" {
bucket = aws_s3_bucket.ondemand_derivatives.id
target_bucket = aws_s3_bucket.chf-logs.id
target_prefix = "s3_access_logs/${terraform.workspace}-ondemand-derivatives/"
}