-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.tf
124 lines (102 loc) · 2.68 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# S3 bucket for website.
resource "aws_s3_bucket" "www_bucket" {
bucket = "www.${var.bucket_name}"
tags = var.common_tags
}
resource "aws_s3_bucket_ownership_controls" "www" {
bucket = aws_s3_bucket.www_bucket.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_public_access_block" "www" {
bucket = aws_s3_bucket.www_bucket.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_acl" "www" {
depends_on = [
aws_s3_bucket_ownership_controls.www,
aws_s3_bucket_public_access_block.www,
]
bucket = aws_s3_bucket.www_bucket.id
acl = "public-read"
}
resource "aws_s3_bucket_cors_configuration" "cors" {
bucket = aws_s3_bucket.www_bucket.id
cors_rule {
allowed_headers = ["Authorization", "Content-Length"]
allowed_methods = ["GET", "POST"]
allowed_origins = ["https://www.${var.domain_name}"]
max_age_seconds = 3000
}
cors_rule {
allowed_methods = ["GET"]
allowed_origins = ["*"]
}
}
resource "aws_s3_bucket_website_configuration" "www" {
bucket = aws_s3_bucket.www_bucket.id
redirect_all_requests_to {
host_name = var.domain_name
protocol = "https"
}
}
# S3 bucket for redirecting non-www to www.
resource "aws_s3_bucket" "root_bucket" {
bucket = var.bucket_name
tags = var.common_tags
}
resource "aws_s3_bucket_website_configuration" "root" {
bucket = aws_s3_bucket.root_bucket.id
index_document {
suffix = "index.html"
}
error_document {
key = "404.html"
}
}
resource "aws_s3_bucket_policy" "allow_read_root" {
bucket = aws_s3_bucket.root_bucket.id
policy = data.aws_iam_policy_document.allow_public_s3_read.json
}
resource "aws_s3_bucket_ownership_controls" "root" {
bucket = aws_s3_bucket.root_bucket.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_public_access_block" "root" {
bucket = aws_s3_bucket.root_bucket.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_acl" "root" {
depends_on = [
aws_s3_bucket_ownership_controls.root,
aws_s3_bucket_public_access_block.root,
]
bucket = aws_s3_bucket.root_bucket.id
acl = "public-read"
}
# S3 Allow Public read access as data object
data "aws_iam_policy_document" "allow_public_s3_read" {
statement {
sid = "PublicReadGetObject"
effect = "Allow"
actions = [
"s3:GetObject",
]
principals {
type = "*"
identifiers = ["*"]
}
resources = [
"arn:aws:s3:::${var.bucket_name}/*",
]
}
}