-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-s3-bucket.tf
81 lines (66 loc) · 2.46 KB
/
04-s3-bucket.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
## ---------------------------------------------------------------------------------------------------------------------
## S3 BUCKET CREATION AND UPLOAD INDEX.HTML
## Modification History:
## - 1.0.0 May 17,2023 -- Initial Version
## ---------------------------------------------------------------------------------------------------------------------
######################################## Local Variables ####################################
locals {
tags = tomap({
Environment = var.environment
ProjectName = var.project_name
})
}
locals {
bucket-name = "${var.s3_bucket_name}-${var.environment}-${var.aws_region}"
}
######################################## Creating aa S3 Bucket ####################################
resource "aws_s3_bucket" "heather_s3_bucket" {
bucket = local.bucket-name
force_destroy = true
tags = local.tags
}
resource "aws_s3_bucket_ownership_controls" "heather_s3_bucket" {
bucket = aws_s3_bucket.heather_s3_bucket.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_acl" "heather_s3_bucket" {
depends_on = [aws_s3_bucket_ownership_controls.heather_s3_bucket]
bucket = aws_s3_bucket.heather_s3_bucket.id
acl = "private"
}
######################################## SSE Encryption ############################################
resource "aws_s3_bucket_server_side_encryption_configuration" "heather_s3_bucket_sse" {
bucket = aws_s3_bucket.heather_s3_bucket.id
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = var.kms_key
sse_algorithm = "aws:kms"
}
}
}
######################################## S3 Lifecycle Policy To Delete Incomplete Upload ###########
resource "aws_s3_bucket_lifecycle_configuration" "heather_s3_bucket_lifecycle_policy" {
bucket = aws_s3_bucket.heather_s3_bucket.id
rule {
id = "Keep previous version 30 days, then in Glacier another 60"
status = "Enabled"
noncurrent_version_transition {
noncurrent_days = 30
storage_class = "GLACIER"
}
noncurrent_version_expiration {
noncurrent_days = 90
}
}
}
######################################## Upload Objects To The S3 Bucket ###########################
resource "aws_s3_object" "s3_object" {
for_each = fileset("html/", "*")
bucket = aws_s3_bucket.heather_s3_bucket.id
key = each.value
source = "html/${each.value}"
etag = filemd5("html/${each.value}")
content_type = "text/html"
}