forked from aws-samples/aws-control-tower-guardduty-enabler
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaws-control-tower-guardduty-enabler.template
225 lines (213 loc) · 8 KB
/
aws-control-tower-guardduty-enabler.template
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
AWSTemplateFormatVersion: 2010-09-09
Description: Creates an SNS topic and Lambda function used to enable GuardDuty and configure alert centralization in the Security account.
Parameters:
SecurityAccountId:
Type: String
Description: Which account will be the GuardDuty Admin? Enter the AWS account ID.
AllowedPattern: '^[0-9]{12}$'
ConstraintDescription: The Security Account ID must be a 12 character string.
MinLength: 12
MaxLength: 12
OrganizationId:
Type: String
Description: AWS Organizations ID for the Control Tower. This is used to restrict permissions to least privilege.
MinLength: 12
MaxLength: 12
AllowedPattern: '^[o][\-][a-z0-9]{10}$'
ConstraintDescription: The Org Id must be a 12 character string starting with o- and followed by 10 lower case alphanumeric characters
RegionFilter:
Type: String
Description: Should GuardDuty be enabled for all GuardDuty supported regions, or only Control Tower supported regions?
Default: ControlTower
AllowedValues:
- GuardDuty
- ControlTower
S3SourceBucket:
Type: String
Description: Which S3 bucket contains the guardduty_enabler.zip file for the GuardDutyEnabler lambda function?
S3SourceFile:
Type: String
Description: What is the S3 path to the zip file for the GuardDutyEnabler lambda function?
Default: guardduty_enabler.zip
ComplianceFrequency:
Type: Number
Description: How frequently (in minutes, between 1 and 3600, default is 60) should organizational compliance be checked?
Default: 60
MinValue: 1
MaxValue: 3600
ConstraintDescription: Compliance Frequency must be a number between 1 and 3600, inclusive.
RoleToAssume:
Type: String
Default: AWSControlTowerExecution
Description: What role should be assumed in child accounts to enable GuardDuty? The default is AWSControlTowerExecution for a Control Tower Environment.
LambdaLogRetenion:
Type: Number
Description: How long (in days, between 1 and 3653, default is 30) should CloudWatch logs for the GuardDutyEnabler lambda function be retained?
Default: 30
AllowedValues: ['1', '3', '5', '7', '14', '30', '60', '90', '120', '150', '180', '365', '400', '545', '731', '1827', '2192', '2557', '2922', '3288', '3653']
ConstraintDescription: Lambda Log Retention must be a number between 1 (1 day) and 3653 (10 years). Valid values are 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 2192, 2557, 2922, 3288, and 3653.
Resources:
GuardDutyEnablerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: GuardDutyEnablerPolicy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- organizations:ListAccounts
- organizations:DescribeAccount
Resource: '*'
Condition:
StringEquals:
"aws:PrincipalOrgId": !Sub ${OrganizationId}
- Effect: Allow
Action: sts:AssumeRole
Resource: !Sub arn:${AWS::Partition}:iam::*:role/${RoleToAssume}
Condition:
StringEquals:
"aws:PrincipalOrgId": !Sub ${OrganizationId}
- Effect: Allow
Action: sns:Publish
Resource: !Ref GuardDutyEnablerTopic
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*
- Effect: Allow
Action: CloudFormation:ListStackInstances
Resource: !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stackset/AWSControlTowerBP-BASELINE-CLOUDWATCH:*
- Effect: Allow
Action:
- iam:GetRole
- iam:CreateServiceLinkedRole
Resource: '*'
- Effect: Allow
Action: sts:AssumeRole
Resource: !Sub arn:${AWS::Partition}:iam::*:role/${RoleToAssume}
- Effect: Allow
Action:
- guardduty:acceptinvitation
- guardduty:createdetector
- guardduty:createmembers
- guardduty:getdetector
- guardduty:invitemembers
- guardduty:listdetectors
- guardduty:listmembers
- guardduty:listinvitations
- guardduty:updatedetector
Resource: '*'
Metadata:
cfn_nag:
rules_to_suppress:
- id: W11
reason: "Organizations doesn't have arns, so we have to use an asterisk in the policy"
GuardDutyEnablerLambda:
DependsOn: GuardDutyEnablerRole
Type: AWS::Lambda::Function
Properties:
Handler: guardduty_enabler.lambda_handler
Role: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${GuardDutyEnablerRole}
Code:
S3Bucket: !Ref S3SourceBucket
S3Key: !Ref S3SourceFile
Runtime: python3.7
MemorySize: 128
Timeout: 300
Environment:
Variables:
assume_role: !Sub ${RoleToAssume}
region_filter: !Ref RegionFilter
ct_root_account: !Sub ${AWS::AccountId}
admin_account: !Sub ${SecurityAccountId}
topic: !Ref GuardDutyEnablerTopic
log_level: "ERROR"
GuardDutyEnablerLambdaLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
Properties:
LogGroupName: !Sub "/aws/lambda/${GuardDutyEnablerLambda}"
RetentionInDays: !Ref LambdaLogRetenion
GuardDutyEnablerTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: GuardDuty_Enabler
TopicName: GuardDutyEnablerTopic
GuardDutyEnablerTopicLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt GuardDutyEnablerLambda.Arn
Principal: sns.amazonaws.com
SourceArn: !Ref GuardDutyEnablerTopic
GuardDutyEnablerSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint: !GetAtt GuardDutyEnablerLambda.Arn
Protocol: lambda
TopicArn: !Ref GuardDutyEnablerTopic
GDScheduledRule:
Type: AWS::Events::Rule
Properties:
Description: GuardDutyScheduledComplianceTrigger
ScheduleExpression: !Sub rate(${ComplianceFrequency} minutes)
State: ENABLED
Targets:
-
Arn: !GetAtt GuardDutyEnablerLambda.Arn
Id: DailyInvite
GDLifeCycleRule:
Type: AWS::Events::Rule
Properties:
Description: GuardDutyLifeCycleTrigger
EventPattern:
source:
- aws.controltower
detail-type:
- AWS Service Event via CloudTrail
detail:
eventName:
- CreateManagedAccount
State: ENABLED
Targets:
-
Arn: !GetAtt GuardDutyEnablerLambda.Arn
Id: DailyInvite
GuardDutyEnablerPermissionForEventsToInvokeLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt GuardDutyEnablerLambda.Arn
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt GDScheduledRule.Arn
GuardDutyEnablerPermissionForLifecycleEventsToInvokeLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt GuardDutyEnablerLambda.Arn
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt GDLifeCycleRule.Arn
GuardDutyEnablerLambdaFirstRun:
DependsOn:
- GuardDutyEnablerTopic
- GuardDutyEnablerRole
- GuardDutyEnablerTopicLambdaPermission
- GuardDutyEnablerSubscription
- GuardDutyEnablerLambdaLogGroup
Type: Custom::GuardDutyEnablerLambdaFirstRun
Properties:
ServiceToken: !GetAtt GuardDutyEnablerLambda.Arn