-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
120 lines (117 loc) · 4.53 KB
/
template.yml
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
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: A collection of utilities that integrate with CloudTrail
Resources:
# This is an _organization sink_ custom EventBridge event bus. It's intended
# to exist once within an AWS Organization (i.e., in a single region in a
# single account).
#
# Relevant CloudTrail activity within the organization is expected to be sent
# to this event bus. Generally this is done by creating rules on all default
# event buses within the organization, which forward CloudTrail events from
# all accounts and regions to this bus.
#
# Any tooling that responds to CloudTrail activity can subscribe to
# the relevant events on this bus.
#
# This bus should always be called `CloudTrail-org-sink`, as that is what
# the rules on all default buses expect.
OrgSinkEventBus:
Type: AWS::Events::EventBus
Properties:
Name: CloudTrail-org-sink
OrgSinkEventBusPolicy:
# TODO Should have a Condition to limit this to events sent by events.amazonaws.com
# since it's only intended to get events from other event buses, not from
# any arbitrary sender
Type: AWS::Events::EventBusPolicy
Properties:
EventBusName: !Ref OrgSinkEventBus
StatementId: AllowEventsToCloudTrailOrgSink
Statement:
Action: events:PutEvents
Condition:
StringEquals:
aws:ResourceOrgID: ${aws:PrincipalOrgID}
Effect: Allow
Principal: "*"
Resource: !GetAtt OrgSinkEventBus.Arn
# Sends notifications to Slack when activity is found in CloudTrail coming
# from root AWS accounts.
RootActivityMonitorFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/root-activity-monitor
Description: >-
Sends messages to Slack in response to root account activity found in
CloudTrail
Environment:
Variables:
AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
Events:
RootActivityEvent:
Properties:
EventBusName: !Ref OrgSinkEventBus
Pattern:
detail:
userIdentity:
type:
- Root
detail-type:
- AWS API Call via CloudTrail
- AWS Console Sign In via CloudTrail
Type: EventBridgeRule
Handler: index.handler
MemorySize: 192
Policies:
- Statement:
- Action: events:PutEvents
Effect: Allow
Resource: !Sub arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:event-bus/default
Version: "2012-10-17"
Runtime: nodejs22.x
Tags:
prx:meta:tagging-version: "2021-04-07"
prx:cloudformation:stack-name: !Ref AWS::StackName
prx:cloudformation:stack-id: !Ref AWS::StackId
prx:ops:environment: Production
prx:dev:application: CloudTrail Toolkit
Timeout: 20
RootActivityMonitorLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${RootActivityMonitorFunction}
RetentionInDays: 90
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudTrail Toolkit }
RootActivityMonitorErrorAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: !Sub FATAL [CloudTrail] Root Activity Monitor <prod> FUNCTION ERRORS (${AWS::StackName})
AlarmDescription: >-
The Lambda function that relays CloudTrail root account activity to
Slack is experiencing errors, which means there could be security
events that need to be investigated.
ComparisonOperator: GreaterThanThreshold
Dimensions:
- Name: FunctionName
Value: !Ref RootActivityMonitorFunction
EvaluationPeriods: 1
MetricName: Errors
Namespace: AWS/Lambda
Period: 60
Statistic: Sum
Tags:
- { Key: prx:meta:tagging-version, Value: "2021-04-07" }
- { Key: prx:cloudformation:stack-name, Value: !Ref AWS::StackName }
- { Key: prx:cloudformation:stack-id, Value: !Ref AWS::StackId }
- { Key: prx:ops:environment, Value: Production }
- { Key: prx:dev:application, Value: CloudTrail Toolkit }
Threshold: 0
TreatMissingData: notBreaching