-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
139 lines (111 loc) · 3.2 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
data "aws_caller_identity" "current" {
}
data "aws_region" "current" {
}
locals {
account_id = data.aws_caller_identity.current.account_id
bucket_name = coalesce(
var.bucket_name,
"${local.account_id}-${local.region}-s3logging-${var.bucket_suffix}"
)
region = data.aws_region.current.name
}
# Ignore logging requirement - access logging for a logging bucket is a little meta
#tfsec:ignore:AWS002
resource "aws_s3_bucket" "this" {
bucket = local.bucket_name
tags = var.tags
}
resource "aws_s3_bucket_acl" "this" {
count = var.object_ownership == "BucketOwnerEnforced" ? 0 : 1
bucket = aws_s3_bucket.this.id
acl = "log-delivery-write"
}
resource "aws_s3_bucket_lifecycle_configuration" "this" {
count = var.lifecycle_rules == null ? 0 : 1
bucket = aws_s3_bucket.this.id
dynamic "rule" {
iterator = rule
for_each = var.lifecycle_rules
content {
id = rule.value.id
status = rule.value.enabled ? "Enabled" : "Disabled"
filter {
prefix = lookup(rule.value, "prefix", null)
}
expiration {
days = lookup(rule.value, "expiration", 2147483647)
}
dynamic "noncurrent_version_expiration" {
for_each = lookup(rule.value, "noncurrent_version_expiration", null) != null ? [1] : []
content {
noncurrent_days = lookup(rule.value, "noncurrent_version_expiration", null)
}
}
dynamic "transition" {
for_each = coalesce(rule.value.transition, [])
content {
days = transition.value.days
storage_class = transition.value.storage_class
}
}
}
}
depends_on = [aws_s3_bucket_versioning.this]
}
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 = var.object_ownership
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
bucket = aws_s3_bucket.this.bucket
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = var.kms_key_id
sse_algorithm = var.kms_key_id != null ? "aws:kms" : "AES256"
}
}
}
resource "aws_s3_bucket_versioning" "this" {
count = var.versioning_enabled ? 1 : 0
bucket = aws_s3_bucket.this.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_policy" "this" {
count = var.object_ownership == "BucketOwnerEnforced" ? 1 : 0
bucket = aws_s3_bucket.this.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "S3ServerAccessLogsPolicy",
"Effect": "Allow",
"Principal": {
"Service": "logging.s3.amazonaws.com"
},
"Action": [
"s3:PutObject"
],
"Resource": "arn:aws:s3:::${aws_s3_bucket.this.bucket}/*",
"Condition": {
"StringEquals": {
"aws:SourceAccount": "${local.account_id}"
}
}
}
]
}
EOF
}