-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
95 lines (79 loc) · 2.5 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
resource "aws_cloudtrail" "cloudtrail" {
name = "CloudTrail"
s3_bucket_name = aws_s3_bucket.cloudtrail.id
include_global_service_events = true
is_multi_region_trail = true
cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.cloudtrail.arn}:*" # CloudTrail requires the Log Stream wildcard
cloud_watch_logs_role_arn = aws_iam_role.cloudtrail.arn
tags = var.common_tags
}
resource "random_id" "cloudtrail" {
byte_length = 8
}
resource "aws_s3_bucket" "cloudtrail" {
bucket = "cloudtrail-${random_id.cloudtrail.hex}"
policy = data.aws_iam_policy_document.cloudtrail_bucket.json
force_destroy = true
tags = var.common_tags
}
data "aws_iam_policy_document" "cloudtrail_bucket" {
statement {
sid = "AWSCloudTrailAclCheck"
actions = ["s3:GetBucketAcl"]
resources = ["arn:aws:s3:::cloudtrail-${random_id.cloudtrail.hex}"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
statement {
sid = "AWSCloudTrailWrite"
actions = ["s3:PutObject"]
resources = ["arn:aws:s3:::cloudtrail-${random_id.cloudtrail.hex}/*"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
}
resource "aws_cloudwatch_log_group" "cloudtrail" {
name = "CloudTrail"
retention_in_days = var.log_retention
tags = var.common_tags
}
resource "aws_iam_role" "cloudtrail" {
name = "cloudtrail"
description = "CloudTrail"
assume_role_policy = data.aws_iam_policy_document.cloudtrail_trust.json
}
data "aws_iam_policy_document" "cloudtrail_trust" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy" "cloudtrail" {
role = aws_iam_role.cloudtrail.id
name = "cloudtrail"
policy = data.aws_iam_policy_document.cloudtrail_role.json
}
data "aws_iam_policy_document" "cloudtrail_role" {
statement {
sid = "AWSCloudTrailCreateLogStream"
actions = ["logs:CreateLogStream"]
resources = [aws_cloudwatch_log_group.cloudtrail.arn]
}
statement {
sid = "AWSCloudTrailPutLogEvents"
actions = ["logs:PutLogEvents"]
resources = [aws_cloudwatch_log_group.cloudtrail.arn]
}
}