forked from covidgreen/covid-green-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudtrail.tf
135 lines (122 loc) · 3.52 KB
/
cloudtrail.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
# #########################################
# Cloudtrail
# #########################################
resource "aws_cloudtrail" "cloudtrail" {
count = local.enable_cloudtrail_count
enable_log_file_validation = true
include_global_service_events = true
is_multi_region_trail = true
name = format("%s-%s", module.labels.id, "cloudtrail")
s3_bucket_name = aws_s3_bucket.cloudtrail[0].id
tags = module.labels.tags
cloud_watch_logs_role_arn = aws_iam_role.cloudtrail_cloudwatch_role[0].arn
cloud_watch_logs_group_arn = aws_cloudwatch_log_group.cloudtrail[0].arn
}
resource "aws_cloudwatch_log_group" "cloudtrail" {
count = local.enable_cloudtrail_count
name = format("%s%s", "/aws/cloudtrail/", module.labels.id)
retention_in_days = var.logs_retention_days
}
resource "aws_iam_role" "cloudtrail_cloudwatch_role" {
count = local.enable_cloudtrail_count
name = format("%s-%s", module.labels.id, "cloudtrail-cloudwatch")
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "cloudtrail.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_policy" "cloudtrail_cloudwatch_policy" {
count = local.enable_cloudtrail_count
name = format("%s-%s", module.labels.id, "cloudtrail-cloudwatch")
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSCloudTrailCreateLogStream20201707",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream"
],
"Resource": [
"${local.cloudtrail_log_stream_arn_pattern}"
]
},
{
"Sid": "AWSCloudTrailPutLogEvents20201707",
"Effect": "Allow",
"Action": [
"logs:PutLogEvents"
],
"Resource": [
"${local.cloudtrail_log_stream_arn_pattern}"
]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "cloudwatch_cloudtrail_attachment" {
count = local.enable_cloudtrail_count
role = aws_iam_role.cloudtrail_cloudwatch_role[0].name
policy_arn = aws_iam_policy.cloudtrail_cloudwatch_policy[0].arn
}
resource "aws_s3_bucket" "cloudtrail" {
count = local.enable_cloudtrail_count
bucket = local.cloudtrail_s3_bucket_name
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "s3:GetBucketAcl",
"Effect": "Allow",
"Principal": { "Service": "cloudtrail.amazonaws.com" },
"Resource": "arn:aws:s3:::${local.cloudtrail_s3_bucket_name}",
"Sid": "AWSCloudTrailAclCheck"
},
{
"Action": "s3:PutObject",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control"
}
},
"Effect": "Allow",
"Principal": { "Service": "cloudtrail.amazonaws.com" },
"Resource": "arn:aws:s3:::${local.cloudtrail_s3_bucket_name}/*",
"Sid": "AWSCloudTrailWrite"
}
]
}
EOF
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
versioning {
enabled = true
}
}
resource "aws_s3_bucket_public_access_block" "default" {
count = local.enable_cloudtrail_count
bucket = aws_s3_bucket.cloudtrail[0].id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}