-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
140 lines (110 loc) · 3.62 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
140
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
locals {
account_id = data.aws_caller_identity.current.account_id
bucket_name = var.bucket_name == null ? "${local.account_id}-${var.region}-cloudtrail" : var.bucket_name
partition = data.aws_partition.current.partition
# Account IDs that will have access to stream CloudTrail logs
account_ids = concat([local.account_id], var.allowed_account_ids)
# Format account IDs into necessary resource lists.
bucket_policy_put_resources = formatlist("${aws_s3_bucket.this.arn}/AWSLogs/%s/*", local.account_ids)
kms_key_encrypt_resources = formatlist("arn:${local.partition}:cloudtrail:*:%s:trail/*", local.account_ids)
# Roles that will have access to KMS decrypt, can be used to grant read access to logs in S3
allow_kms_decrypt = formatlist("arn:aws:iam::${local.account_id}:role/%s", var.roles_allowed_kms_decrypt)
}
resource "aws_s3_bucket" "this" {
bucket = local.bucket_name
tags = var.tags
lifecycle {
prevent_destroy = true
}
}
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_logging" "this" {
bucket = aws_s3_bucket.this.id
target_bucket = var.logging_bucket
target_prefix = "${local.account_id}-${var.region}-cloudtrail/"
}
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_server_side_encryption_configuration" "this" {
bucket = aws_s3_bucket.this.bucket
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.this.arn
sse_algorithm = "aws:kms"
}
}
}
resource "aws_s3_bucket_versioning" "this" {
count = var.versioning_enabled ? 1 : 0
bucket = aws_s3_bucket.this.id
versioning_configuration {
status = "Enabled"
}
}
data "aws_iam_policy_document" "this" {
statement {
actions = ["s3:GetBucketAcl"]
effect = "Allow"
resources = [aws_s3_bucket.this.arn]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
statement {
actions = ["s3:PutObject"]
effect = "Allow"
resources = local.bucket_policy_put_resources
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
}
resource "aws_s3_bucket_policy" "this" {
bucket = aws_s3_bucket.this.id
policy = data.aws_iam_policy_document.this.json
}