-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathKinesisFirehoseCWMetrics.template.yaml
438 lines (410 loc) · 15.4 KB
/
KinesisFirehoseCWMetrics.template.yaml
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
AWSTemplateFormatVersion: '2010-09-09'
Description: "Template to setup the AWS CloudWatch Metrics collection using Amazon Kinesis Firehose and send the data to provided Sumo Logic URL."
Metadata:
AWS::ServerlessRepo::Application:
Author: Sumo Logic
Description: This solution helps to setup the AWS CloudWatch Metrics collection using Amazon Kinesis Firehose and send the data to provided Sumo Logic URL.
HomePageUrl: https://github.com/SumoLogic/sumologic-aws-lambda
Labels:
- lambda
- sumologic
- serverless
- kinesis
- firehose
- cloudwatch
- metrics
Name: sumologic-kinesis-cloudwatch-metrics
SemanticVersion: 1.0.0
SourceCodeUrl: https://github.com/SumoLogic/sumologic-aws-lambda/tree/master/kinesis-firehose-cloudwatch-collection/metrics
LicenseUrl: ./LICENSE
ReadmeUrl: ./README.md
SpdxLicenseId: Apache-2.0
'AWS::CloudFormation::Interface':
ParameterGroups:
- Label:
default: "1. Sumo Logic Kinesis Firehose Metrics Configuration"
Parameters:
- Section1aSumoLogicKinesisMetricsURL
- Section1bNamespaceFilter
- Label:
default: "2. Failed Data AWS S3 Bucket Configuration"
Parameters:
- Section2aCreateS3Bucket
- Section2bFailedDataS3Bucket
ParameterLabels:
Section1aSumoLogicKinesisMetricsURL:
default: "Sumo Logic AWS Kinesis Firehose for Metrics Source URL"
Section1bNamespaceFilter:
default: "Select Namespaces to collect AWS CloudWatch Metrics from"
Section2aCreateS3Bucket:
default: "Create AWS S3 Bucket"
Section2bFailedDataS3Bucket:
default: "AWS S3 Bucket Name for Failed Data"
Parameters:
Section1aSumoLogicKinesisMetricsURL:
Type: String
Description: "Provide HTTP Source Address from AWS Kinesis Firehose for Metrics source created on your Sumo Logic account."
AllowedPattern: ".+"
ConstraintDescription: "Sumo Logic AWS Kinesis Firehose for Metrics Source URL can not be empty."
Section1bNamespaceFilter:
Default: ""
Description: "Enter in a comma-delimited list of namespaces which you want to collect AWS CloudWatch metrics from. Example: AWS/ELB, AWS/DynamoDB .... (give a list of all the ones we support). To collect all namespaces simply leave this field blank; this is the default value."
Type: String
Section2aCreateS3Bucket:
Type: String
Default: 'Yes'
Description: "Yes - Create a new AWS S3 Bucket to store failed data.'.
No - Use an existing AWS S3 Bucket to store failed data."
AllowedValues:
- 'Yes'
- 'No'
Section2bFailedDataS3Bucket:
Type: String
Description: "Provide a unique name of AWS S3 bucket where you would like to store Failed data. In case of existing AWS S3 bucket, provide the bucket from the current AWS Account. For Metrics, failed data will be stored in folder prefix as SumoLogic-Kinesis-Failed-Metrics."
AllowedPattern: "[a-z0-9-.]{3,63}$"
ConstraintDescription: "3-63 characters; must contain only lowercase letters, numbers, hyphen or period. For more details - https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html"
Conditions:
create_bucket: !Equals [ !Ref Section2aCreateS3Bucket, 'Yes' ]
Mappings:
ArnValue:
us-east-1:
ArnValue: "aws"
us-east-2:
ArnValue: "aws"
us-west-1:
ArnValue: "aws"
us-west-2:
ArnValue: "aws"
af-south-1:
ArnValue: "aws"
ca-central-1:
ArnValue: "aws"
eu-central-1:
ArnValue: "aws"
eu-west-1:
ArnValue: "aws"
eu-west-2:
ArnValue: "aws"
eu-south-1:
ArnValue: "aws"
eu-west-3:
ArnValue: "aws"
eu-north-1:
ArnValue: "aws"
ap-east-1:
ArnValue: "aws"
ap-northeast-1:
ArnValue: "aws"
ap-northeast-2:
ArnValue: "aws"
ap-northeast-3:
ArnValue: "aws"
ap-southeast-1:
ArnValue: "aws"
ap-southeast-2:
ArnValue: "aws"
ap-south-1:
ArnValue: "aws"
me-south-1:
ArnValue: "aws"
sa-east-1:
ArnValue: "aws"
us-gov-west-1:
ArnValue: "aws-us-gov"
us-gov-east-1:
ArnValue: "aws-us-gov"
cn-north-1:
ArnValue: "aws"
cn-northwest-1:
ArnValue: "aws"
Resources:
# Common Resources including creating bucket, create logs role and attach bucket policy.
FailedDataBucket:
Type: AWS::S3::Bucket
Condition: create_bucket
Metadata:
cfn_nag:
rules_to_suppress:
- id: W51
reason: "Role has been assigned permission to put logs in AWS S3 bucket."
- id: W35
reason: "Access logging not required for AWS S3 Bucket."
- id: W41
reason: "Encryption not required for AWS S3 Bucket."
Properties:
BucketName: !Ref Section2bFailedDataS3Bucket
AccessControl: Private
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
FirehoseLogsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: 'FirehoseAssumeRole'
Effect: Allow
Principal:
Service: firehose.amazonaws.com
Action: sts:AssumeRole
Condition:
StringEquals:
sts:ExternalId: !Ref "AWS::AccountId"
AttachBucketPolicyToFirehoseLogsRole:
Type: AWS::IAM::Policy
Properties:
PolicyName: "S3BucketPermissionPolicy"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:AbortMultipartUpload
- s3:GetBucketLocation
- s3:GetObject
- s3:ListBucket
- s3:ListBucketMultipartUploads
- s3:PutObject
Resource:
- !Sub
- "arn:${arn}:s3:::${S3Bucket}/*"
- arn: !FindInMap [ ArnValue, !Ref "AWS::Region", ArnValue ]
S3Bucket: !Ref Section2bFailedDataS3Bucket
- !Sub
- "arn:${arn}:s3:::${S3Bucket}"
- arn: !FindInMap [ ArnValue, !Ref "AWS::Region", ArnValue ]
S3Bucket: !Ref Section2bFailedDataS3Bucket
Roles:
- Ref: FirehoseLogsRole
# Resources for AWS Kinesis Metrics including log group, stream, delivery stream, subscription filter, policy and role.
KinesisMetricsLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub
- "/aws/kinesisfirehose/Kinesis-Metrics-${StackID}"
- StackID: !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split [ "/", !Ref "AWS::StackId" ]
RetentionInDays: 7
KinesisMetricsLogStream:
Type: AWS::Logs::LogStream
Properties:
LogGroupName: !Ref KinesisMetricsLogGroup
LogStreamName: "HttpEndpointDelivery"
KinesisMetricsLogStreamS3:
Type: AWS::Logs::LogStream
Properties:
LogGroupName: !Ref KinesisMetricsLogGroup
LogStreamName: "S3Delivery"
KinesisMetricsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: 'MetricsAssumeRole'
Effect: Allow
Principal:
Service: "streams.metrics.cloudwatch.amazonaws.com"
Action: sts:AssumeRole
KinesisMetricsRolePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: "KinesisFirehoseMetricsPolicy"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- firehose:PutRecord
- firehose:PutRecordBatch
Resource: !Sub
- "arn:${arn}:firehose:${Region}:${AccountId}:*"
- arn: !FindInMap [ ArnValue, !Ref "AWS::Region", ArnValue ]
Region: !Ref "AWS::Region"
AccountId: !Ref "AWS::AccountId"
- Effect: Allow
Action:
- iam:PassRole
Resource: !Sub
- "arn:${arn}:iam::${AccountId}:role/${KinesisMetricsRole}"
- arn: !FindInMap [ ArnValue, !Ref "AWS::Region", ArnValue ]
AccountId: !Ref "AWS::AccountId"
KinesisMetricsRole: !Ref KinesisMetricsRole
Roles:
- Ref: KinesisMetricsRole
KinesisMetricsDeliveryStream:
Type: AWS::KinesisFirehose::DeliveryStream
Properties:
DeliveryStreamName: !Sub
- "Kinesis-Metrics-${StackID}"
- StackID: !Select
- 0
- !Split
- "-"
- !Select
- 2
- !Split [ "/", !Ref "AWS::StackId" ]
DeliveryStreamType: DirectPut
HttpEndpointDestinationConfiguration:
RoleARN: !GetAtt FirehoseLogsRole.Arn
EndpointConfiguration:
Url: !Ref Section1aSumoLogicKinesisMetricsURL
Name: !Sub "${AWS::StackName}-sumologic-metrics-endpoint"
RequestConfiguration:
ContentEncoding: GZIP
CloudWatchLoggingOptions:
Enabled: true
LogGroupName: !Ref KinesisMetricsLogGroup
LogStreamName: !Ref KinesisMetricsLogStream
BufferingHints:
IntervalInSeconds: 60
SizeInMBs: 1
RetryOptions:
DurationInSeconds: 60
S3BackupMode: FailedDataOnly
S3Configuration:
BucketARN: !If
- create_bucket
- !GetAtt FailedDataBucket.Arn
- !Sub
- "arn:${arn}:s3:::${S3Bucket}"
- arn: !FindInMap [ ArnValue, !Ref "AWS::Region", ArnValue ]
S3Bucket: !Ref Section2bFailedDataS3Bucket
CompressionFormat: UNCOMPRESSED
ErrorOutputPrefix: "SumoLogic-Kinesis-Failed-Metrics/"
RoleARN: !GetAtt FirehoseLogsRole.Arn
CloudWatchLoggingOptions:
Enabled: true
LogGroupName: !Ref KinesisMetricsLogGroup
LogStreamName: !Ref KinesisMetricsLogStreamS3
KinesisMetricsFirehoseRolePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: "KinesisMetricsLogStreamPermissionPolicy"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:PutLogEvents
Resource:
- !GetAtt KinesisMetricsLogGroup.Arn
- !Sub "${KinesisMetricsLogGroup.Arn}:log-stream:*"
- Effect: Allow
Action:
- kinesis:DescribeStream
- kinesis:GetShardIterator
- kinesis:GetRecords
- kinesis:ListShards
Resource:
- !GetAtt KinesisMetricsDeliveryStream.Arn
Roles:
- Ref: FirehoseLogsRole
KinesisMetricsCloudWatchMetricsStreamLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Condition: { }
Path: /
Policies:
- PolicyName: inline-policy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- cloudwatch:PutMetricStream
- cloudwatch:DeleteMetricStream
Resource: !Sub
- "arn:${arn}:cloudwatch:${Region}:${AccountId}:metric-stream/*"
- arn: !FindInMap [ ArnValue, !Ref "AWS::Region", ArnValue ]
Region: !Ref "AWS::Region"
AccountId: !Ref "AWS::AccountId"
- Effect: Allow
Action:
- iam:PassRole
Resource: !Sub
- "arn:${arn}:iam::${AccountId}:role/${KinesisMetricsRole}"
- arn: !FindInMap [ ArnValue, !Ref "AWS::Region", ArnValue ]
AccountId: !Ref "AWS::AccountId"
KinesisMetricsRole: !Ref KinesisMetricsRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
KinesisMetricsCloudWatchMetricsStreamLambda:
Type: AWS::Lambda::Function
Properties:
Description: "The lambda function is used to create CloudWatch Metric Stream."
Handler: index.handler
Code:
ZipFile: |
import sys
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", '--upgrade', 'boto3', '--target', '/tmp/'])
sys.path.insert(0, '/tmp/')
import boto3
import cfnresponse
def handler(event, context):
response_data = {}
physical_id = ""
namespaces_list = []
try:
client = boto3.client('cloudwatch')
properties = event['ResourceProperties']
physical_id = properties['Name']
print("Received %s Event Type." % event['RequestType'])
if event['RequestType'] == "Delete":
response_data = client.delete_metric_stream(Name=physical_id)
else:
namespaces = properties["Namespaces"]
if namespaces:
namespaces = namespaces.split(",")
for namespace in namespaces:
namespaces_list.append({'Namespace': namespace.strip()})
response_data = client.put_metric_stream(Name=physical_id, FirehoseArn=properties['FirehoseArn'],
RoleArn=properties['RoleArn'],
OutputFormat=properties['OutputFormat'],
IncludeFilters=namespaces_list)
except Exception as e:
print(e)
cfnresponse.send(event, context, cfnresponse.FAILED, {"Error": str(e)}, physical_id)
cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data, physical_id)
Runtime: python3.11
Timeout: 300
Role: !GetAtt KinesisMetricsCloudWatchMetricsStreamLambdaRole.Arn
KinesisMetricsCloudWatchMetricsStream:
Type: AWS::CloudFormation::CustomResource
Properties:
ServiceToken: !GetAtt KinesisMetricsCloudWatchMetricsStreamLambda.Arn
Name: !Ref "AWS::StackName"
OutputFormat: 'opentelemetry0.7'
FirehoseArn: !GetAtt KinesisMetricsDeliveryStream.Arn
RoleArn: !GetAtt KinesisMetricsRole.Arn
Namespaces: !Ref Section1bNamespaceFilter
Outputs:
FailedDataBucketArn:
Description: "S3 Bucket Arn where failed deliveries will be saved"
Condition: create_bucket
Value: !GetAtt FailedDataBucket.Arn
KinesisMetricsDeliveryStreamARN:
Description: "The ARN for your Kinesis Firehose Delivery Stream, use this as the destination when adding CloudWatch Metrics subscription filters."
Value: !GetAtt KinesisMetricsDeliveryStream.Arn
KinesisMetricsRoleARN:
Description: "The ARN for your CloudWatch Logs role to write to your delivery stream, use this as the role-arn when adding CloudWatch Logs subscription filters."
Value: !GetAtt KinesisMetricsRole.Arn
KinesisMetricsLogGroupARN:
Description: "The CloudWatch log group name where kinesis stream logs will be sent."
Value: !Ref KinesisMetricsLogGroup