-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
143 lines (113 loc) · 3.36 KB
/
Copy pathmain.tf
File metadata and controls
143 lines (113 loc) · 3.36 KB
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
resource "random_string" "suffix" {
count = var.add_suffix ? 1 : 0
length = local.suffix_length
lower = true
numeric = true
special = false
upper = false
}
resource "aws_s3_bucket" "this" {
bucket = local.bucket_name
force_destroy = var.force_delete
tags = local.tags
lifecycle {
precondition {
condition = length(local.bucket_name) >= 3 && length(local.bucket_name) <= 63
error_message = <<-EOT
The bucket name must be between 3 and 63 characters. Shorten project,
environment, or name, or set add_suffix to true to truncate the name
automatically.
EOT
}
}
}
resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_ownership_controls" "this" {
bucket = aws_s3_bucket.this.id
rule {
object_ownership = "BucketOwnerEnforced"
}
}
resource "aws_s3_bucket_versioning" "this" {
bucket = aws_s3_bucket.this.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_object_lock_configuration" "this" {
for_each = var.object_lock.enabled ? toset(["this"]) : toset([])
# Object lock requires versioning to be enabled on the bucket first.
depends_on = [aws_s3_bucket_versioning.this]
bucket = aws_s3_bucket.this.id
dynamic "rule" {
for_each = var.object_lock.days != null ? toset(["this"]) : toset([])
content {
default_retention {
mode = var.object_lock.mode
days = var.object_lock.days
}
}
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
bucket = aws_s3_bucket.this.id
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = local.kms_key_arn
sse_algorithm = "aws:kms"
}
bucket_key_enabled = true
}
}
resource "aws_s3_bucket_logging" "this" {
bucket = aws_s3_bucket.this.id
target_bucket = var.logging_bucket
target_prefix = "${local.logs_path}/s3accesslogs/${local.bucket_name}"
}
resource "aws_s3_bucket_lifecycle_configuration" "this" {
# Referencing noncurrent-version behavior requires versioning to be enabled
# first.
depends_on = [aws_s3_bucket_versioning.this]
bucket = aws_s3_bucket.this.id
rule {
id = "state"
status = "Enabled"
filter {
prefix = ""
}
abort_incomplete_multipart_upload {
days_after_initiation = var.abort_incomplete_multipart_upload_days
}
noncurrent_version_expiration {
noncurrent_days = var.noncurrent_version_expiration_days
}
dynamic "expiration" {
for_each = var.expiration != null ? toset(["this"]) : toset([])
content {
days = var.expiration
}
}
dynamic "transition" {
for_each = var.storage_class_transitions
content {
days = transition.value.days
storage_class = transition.value.storage_class
}
}
}
}
resource "aws_s3_bucket_policy" "this" {
# The public access block must be in place before a bucket policy can be
# applied.
depends_on = [aws_s3_bucket_public_access_block.this]
bucket = aws_s3_bucket.this.id
policy = jsonencode(merge(local.base_bucket_policy, {
Statement = concat(local.base_bucket_policy.Statement, var.additional_policy_statements)
}))
}