-
Notifications
You must be signed in to change notification settings - Fork 8
/
cloudformation.yml
217 lines (185 loc) · 8.87 KB
/
cloudformation.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
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
AWSTemplateFormatVersion: "2010-09-09"
Description: "Creates an OpenSearch cluster and a Lambda function that will write CloudTrail events into it"
Parameters:
LambdaName:
Description: "Name for the Lambda function and associated resources"
Type: "String"
Default: "CloudTrail_to_OpenSearch"
CloudTrailBucket:
Description: "The S3 bucket that holds CloudTrail events"
Type: "String"
CloudTrailPrefix:
Description: "The path in that bucket for new CloudTrail event files, with trailing slash"
Type: "String"
Default: ""
OpenSearchDomainName:
Description: "Name for the OpenSearch cluster (must be DNS-legal)"
Type: "String"
Default: "cloudtrail-events"
OpenSearchInstanceType:
Description: "Type of instance used for OpenSearch cluster; default is useful for small environment"
Type: "String"
Default: "t3.small.search"
OpenSearchStorageType:
Description: "Type of the OpenSearch storage volume (allowed types depend on instance type)"
Type: "String"
Default: "gp3"
OpenSearchStorageSize:
Description: "Size of the OpenSearch storage volume, in GB (allowed sizes depend on instance type)"
Type: "Number"
Default: 64
AllowedIPs:
Description: "IP addresses that are allowed to access OpenSearch/Kibana (leave blank for none)"
Type: "CommaDelimitedList"
Default: ""
Resources:
##
## OpenSearch cluster
##
OpenSearchDomain:
Type: "AWS::OpenSearchService::Domain"
UpdatePolicy:
EnableVersionUpgrade: true
Properties:
DomainName: !Ref OpenSearchDomainName
EngineVersion: "OpenSearch_2.13"
ClusterConfig:
InstanceType: !Ref OpenSearchInstanceType
InstanceCount: 1
DedicatedMasterEnabled: false
ZoneAwarenessEnabled: false
EBSOptions:
EBSEnabled: true
VolumeType: !Ref OpenSearchStorageType
VolumeSize: !Ref OpenSearchStorageSize
SnapshotOptions:
AutomatedSnapshotStartHour: 8
AdvancedOptions: {
"rest.action.multi.allow_explicit_index": "true"
}
AccessPolicies:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal: { "AWS": !Sub "${AWS::AccountId}" }
Action: "es:*"
Resource: !Sub "arn:aws:es:${AWS::Region}:${AWS::AccountId}:domain/${OpenSearchDomainName}/*"
- Effect: "Allow"
Principal: { "AWS": "*" }
Action: "es:*"
Resource: !Sub "arn:aws:es:${AWS::Region}:${AWS::AccountId}:domain/${OpenSearchDomainName}/*"
Condition:
IpAddress:
aws:SourceIp: !Ref AllowedIPs
##
## SQS Queues
##
NotificationQueue:
Type: "AWS::SQS::Queue"
Properties:
QueueName: !Sub "${LambdaName}-Notifications"
VisibilityTimeout: 3600 # one hour, a multiple of max Lambda execution time
RedrivePolicy:
maxReceiveCount: 8 # this should be enough to cover cluster upgrades
deadLetterTargetArn: !GetAtt DeadLetterQueue.Arn
NotificationQueuePolicy:
Type: "AWS::SQS::QueuePolicy"
Properties:
Queues:
- !Ref NotificationQueue
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: "sqs:SendMessage"
Resource: !GetAtt NotificationQueue.Arn
Principal:
Service: [ "s3.amazonaws.com" ]
DeadLetterQueue:
Type: "AWS::SQS::Queue"
Properties:
QueueName: !Sub "${LambdaName}-DLQ"
MessageRetentionPeriod: 1209600 # two weeks (maximum allowed)
##
## Lambda Function
##
LambdaLogGroup:
Type: "AWS::Logs::LogGroup"
DeletionPolicy: "Delete"
Properties:
LogGroupName: !Sub "/aws/lambda/${LambdaName}"
RetentionInDays: 7
LambdaRole:
Type: "AWS::IAM::Role"
Properties:
Path: "/lambda/"
RoleName: !Sub "${LambdaName}-ExecutionRole-${AWS::Region}"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Principal:
Service: "lambda.amazonaws.com"
Action: "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
Policies:
- PolicyName: "ReadFromSQS"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "sqs:ReceiveMessage"
- "sqs:DeleteMessage"
- "sqs:GetQueueAttributes"
Resource: !GetAtt NotificationQueue.Arn
- PolicyName: "ReadFromLogBucket"
PolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Action:
- "s3:GetObject"
Resource: [ !Sub "arn:aws:s3:::${CloudTrailBucket}/${CloudTrailPrefix}*" ]
- PolicyName: "WriteToDestination"
PolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Action: [ "es:ESHttpGet", "es:ESHttpPost", "es:ESHttpPut" ]
Resource: [ !Sub "${OpenSearchDomain.Arn}/*" ]
LambdaFunction:
Type: "AWS::Lambda::Function"
Properties:
FunctionName: !Ref LambdaName
Description: "Reacts to CloudTrail files being stored on S3 by uploading them to OpenSearch"
Role: !GetAtt LambdaRole.Arn
Runtime: "python3.12"
Handler: "cloudtrail_to_elasticsearch.lambda_handler.handle"
Code:
ZipFile: "# this is an invalid file, used as a placeholder in the CloudFormation script"
MemorySize: 512
Timeout: 60
Environment:
Variables:
ES_HOSTNAME: !GetAtt OpenSearchDomain.DomainEndpoint
LambdaTrigger:
Type: "AWS::Lambda::EventSourceMapping"
Properties:
EventSourceArn: !GetAtt NotificationQueue.Arn
FunctionName: !GetAtt LambdaFunction.Arn
BatchSize: 10
MaximumBatchingWindowInSeconds: 90
ScalingConfig:
MaximumConcurrency: 2
Outputs:
NotificationQueueArn:
Description: "The ARN of the notification queue (to create bucket notification)"
Value: !GetAtt NotificationQueue.Arn
OpenSearchArn:
Description: "The ARN of the OpenSearch cluster"
Value: !GetAtt OpenSearchDomain.Arn
OpenSearchHostname:
Description: "The hostname of the OpenSearch cluster"
Value: !GetAtt OpenSearchDomain.DomainEndpoint