-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.tf
99 lines (73 loc) · 2.05 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
module "template_files" {
source = "hashicorp/dir/template"
base_dir = "${path.module}/web"
}
# create a s3 bucket
resource "aws_s3_bucket" "hosting_bucket" {
bucket = var.bucket_name
tags={
"Name": "Static Webiste Bucket"
"Environment": "Dev"
}
}
resource "aws_s3_bucket_ownership_controls" "hosting_bucket_ownership" {
bucket = aws_s3_bucket.hosting_bucket.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_public_access_block" "hosting_bucket_access_block" {
bucket = aws_s3_bucket.hosting_bucket.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_acl" "hosting_bucket_acl" {
depends_on = [
aws_s3_bucket_ownership_controls.hosting_bucket_ownership,
aws_s3_bucket_public_access_block.hosting_bucket_access_block,
]
bucket = aws_s3_bucket.hosting_bucket.id
acl = "public-read"
}
resource "aws_s3_bucket_policy" "hosting_bucket_policy" {
bucket = aws_s3_bucket.hosting_bucket.id
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${var.bucket_name}/*"
}
]
})
}
resource "aws_s3_bucket_website_configuration" "hosting_bucket_website_configuration" {
bucket = aws_s3_bucket.hosting_bucket.id
index_document {
suffix = "index.html"
}
error_document {
key = "error.html"
}
routing_rule {
condition {
key_prefix_equals = "docs/"
}
redirect {
replace_key_prefix_with = "documents/"
}
}
}
resource "aws_s3_object" "hosting_bucket_files" {
bucket = aws_s3_bucket.hosting_bucket.id
for_each = module.template_files.files
key = each.key
content_type = each.value.content_type
source = each.value.source_path
content = each.value.content
etag = each.value.digests.md5
}