-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.tf
105 lines (86 loc) · 2.02 KB
/
s3.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
resource "aws_s3_bucket" "website" {
bucket = var.domain_name
acl = "public-read"
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
website {
index_document = "index.html"
error_document = "error.html"
}
}
resource "aws_s3_bucket" "redirect" {
bucket = var.redirect_domain_name
website {
redirect_all_requests_to = "https://${var.domain_name}"
}
}
resource "aws_s3_bucket_public_access_block" "website" {
bucket = aws_s3_bucket.website.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_ownership_controls" "website" {
bucket = aws_s3_bucket.website.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_policy" "website" {
bucket = aws_s3_bucket.website.id
policy = data.aws_iam_policy_document.website.json
}
resource "aws_s3_bucket_policy" "redirect" {
bucket = aws_s3_bucket.redirect.id
policy = data.aws_iam_policy_document.redirect_website.json
}
resource "random_password" "referer" {
length = 32
special = false
}
data "aws_iam_policy_document" "website" {
statement {
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"s3:GetObject",
]
resources = [
aws_s3_bucket.website.arn,
"${aws_s3_bucket.website.arn}/*",
]
condition {
test = "StringLike"
values = [random_password.referer.result]
variable = "aws:Referer"
}
}
}
data "aws_iam_policy_document" "redirect_website" {
statement {
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"s3:GetObject",
]
resources = [
aws_s3_bucket.redirect.arn,
"${aws_s3_bucket.redirect.arn}/*",
]
condition {
test = "StringLike"
values = [random_password.referer.result]
variable = "aws:Referer"
}
}
}