-
Notifications
You must be signed in to change notification settings - Fork 70
/
template.yaml
550 lines (546 loc) · 20.1 KB
/
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Stack that deploys a bucket which you can use as a target for your
Amazon CloudFront access logs (use the prefix 'new/'). An event
notification is configured so that new objects created will fire an
AWS Lambda function that moves the objects to prefixes (under
'partitioned-gz/') that adhere to the Apache Hive partitioning format.
This way the data is easier to consume for big data tools (as Amazon
Athena and AWS Glue).
Read the blog post
https://aws.amazon.com/blogs/big-data/analyze-your-amazon-cloudfront-access-logs-at-scale/
for further details about this sample asset (uksb-1tupboc62).
Parameters:
ResourcePrefix:
Type: String
Default: 'myapp'
AllowedPattern: '[a-z0-9]+'
MinLength: 1
MaxLength: 20
Description: >
Prefix that is used for the created resources (20 chars, a-z and 0-9 only)
NewKeyPrefix:
Type: String
Default: 'new/'
AllowedPattern: '[A-Za-z0-9\-]+/'
Description: >
Prefix of new access log files that are written by Amazon CloudFront.
Including the trailing slash.
GzKeyPrefix:
Type: String
Default: 'partitioned-gz/'
AllowedPattern: '[A-Za-z0-9\-]+/'
Description: >
Prefix of gzip'ed access log files that are moved to the Apache Hive
like style. Including the trailing slash.
ParquetKeyPrefix:
Type: String
Default: 'partitioned-parquet/'
AllowedPattern: '[A-Za-z0-9\-]+/'
Description: >
Prefix of parquet files that are created in Apache Hive
like style by the CTAS query. Including the trailing slash.
Resources:
TransformPartFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/
Handler: transformPartition.handler
Runtime: nodejs20.x
Timeout: 900
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- athena:StartQueryExecution
- athena:GetQueryExecution
Resource: '*'
- Effect: Allow
Action:
- s3:ListBucket
- s3:GetBucketLocation
Resource: !Sub "arn:${AWS::Partition}:s3:::${ResourcePrefix}-${AWS::AccountId}-cf-access-logs"
- Effect: Allow
Action:
- s3:PutObject
- s3:GetObject
Resource: !Sub "arn:${AWS::Partition}:s3:::${ResourcePrefix}-${AWS::AccountId}-cf-access-logs/*"
- Effect: Allow
Action:
- glue:CreatePartition
- glue:GetDatabase
- glue:GetTable
- glue:BatchCreatePartition
- glue:GetPartition
- glue:GetPartitions
- glue:CreateTable
- glue:DeleteTable
- glue:DeletePartition
Resource: '*'
Environment:
Variables:
SOURCE_TABLE: !Ref PartitionedGzTable
TARGET_TABLE: !Ref PartitionedParquetTable
DATABASE: !Ref CfLogsDatabase
ATHENA_QUERY_RESULTS_LOCATION: !Sub "s3://${ResourcePrefix}-${AWS::AccountId}-cf-access-logs/athena-query-results"
Events:
HourlyEvt:
Type: Schedule
Properties:
Schedule: cron(1 * * * ? *)
CreatePartFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/
Handler: createPartitions.handler
Runtime: nodejs20.x
Timeout: 5
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- athena:StartQueryExecution
- athena:GetQueryExecution
Resource: '*'
- Effect: Allow
Action:
- s3:ListBucket
- s3:GetBucketLocation
Resource: !Sub "arn:${AWS::Partition}:s3:::${ResourcePrefix}-${AWS::AccountId}-cf-access-logs"
- Effect: Allow
Action:
- s3:PutObject
Resource: !Sub "arn:${AWS::Partition}:s3:::${ResourcePrefix}-${AWS::AccountId}-cf-access-logs/*"
- Effect: Allow
Action:
- glue:CreatePartition
- glue:GetDatabase
- glue:GetTable
- glue:BatchCreatePartition
Resource: '*'
Environment:
Variables:
TABLE: !Ref PartitionedGzTable
DATABASE: !Ref CfLogsDatabase
ATHENA_QUERY_RESULTS_LOCATION: !Sub "s3://${ResourcePrefix}-${AWS::AccountId}-cf-access-logs/athena-query-results"
Events:
HourlyEvt:
Type: Schedule
Properties:
Schedule: cron(55 * * * ? *)
MoveNewAccessLogsFn:
Type: AWS::Serverless::Function
Properties:
CodeUri: functions/
Handler: moveAccessLogs.handler
Runtime: nodejs20.x
Timeout: 30
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:DeleteObject
Resource: !Sub "arn:${AWS::Partition}:s3:::${ResourcePrefix}-${AWS::AccountId}-cf-access-logs/${NewKeyPrefix}*"
- Effect: Allow
Action:
- s3:PutObject
Resource: !Sub "arn:${AWS::Partition}:s3:::${ResourcePrefix}-${AWS::AccountId}-cf-access-logs/${GzKeyPrefix}*"
Environment:
Variables:
TARGET_KEY_PREFIX: !Ref GzKeyPrefix
Events:
AccessLogsUploadedEvent:
Type: S3
Properties:
Bucket: !Ref CloudFrontAccessLogsBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: prefix
Value: !Ref NewKeyPrefix
CloudFrontAccessLogsBucket:
Type: "AWS::S3::Bucket"
Description: "Bucket for Amazon CloudFront access logs"
Properties:
BucketName: !Sub "${ResourcePrefix}-${AWS::AccountId}-cf-access-logs"
LifecycleConfiguration:
Rules:
- Id: ExpireAthenaQueryResults
Prefix: athena-query-results/
Status: Enabled
ExpirationInDays: 1
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
PublicAccessBlockConfiguration:
BlockPublicAcls: Yes
BlockPublicPolicy: Yes
IgnorePublicAcls: Yes
RestrictPublicBuckets: Yes
CloudFrontAccessLogsBucketPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: !Ref CloudFrontAccessLogsBucket
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Deny
Principal: "*"
Action: s3:*
Resource:
- !Sub "${CloudFrontAccessLogsBucket.Arn}"
- !Sub "${CloudFrontAccessLogsBucket.Arn}/*"
Condition:
Bool:
"aws:SecureTransport": "false"
# Glue Resources
# - Database
# - Partitioned Gzip Table
# - Partitioned Parquet Table
# - Combined view of both tables
CfLogsDatabase:
Type: AWS::Glue::Database
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseInput:
Name: !Sub "${ResourcePrefix}_cf_access_logs_db"
PartitionedGzTable:
Type: AWS::Glue::Table
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseName: !Ref CfLogsDatabase
TableInput:
Name: 'partitioned_gz'
Description: 'Gzip logs delivered by Amazon CloudFront partitioned'
TableType: EXTERNAL_TABLE
Parameters: { "skip.header.line.count": "2" }
PartitionKeys:
- Name: year
Type: string
- Name: month
Type: string
- Name: day
Type: string
- Name: hour
Type: string
StorageDescriptor:
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Columns:
- Name: date
Type: date
- Name: time
Type: string
- Name: location
Type: string
- Name: bytes
Type: bigint
- Name: request_ip
Type: string
- Name: method
Type: string
- Name: host
Type: string
- Name: uri
Type: string
- Name: status
Type: int
- Name: referrer
Type: string
- Name: user_agent
Type: string
- Name: query_string
Type: string
- Name: cookie
Type: string
- Name: result_type
Type: string
- Name: request_id
Type: string
- Name: host_header
Type: string
- Name: request_protocol
Type: string
- Name: request_bytes
Type: bigint
- Name: time_taken
Type: float
- Name: xforwarded_for
Type: string
- Name: ssl_protocol
Type: string
- Name: ssl_cipher
Type: string
- Name: response_result_type
Type: string
- Name: http_version
Type: string
- Name: fle_status
Type: string
- Name: fle_encrypted_fields
Type: int
- Name: c_port
Type: int
- Name: time_to_first_byte
Type: float
- Name: x_edge_detailed_result_type
Type: string
- Name: sc_content_type
Type: string
- Name: sc_content_len
Type: bigint
- Name: sc_range_start
Type: bigint
- Name: sc_range_end
Type: bigint
InputFormat: org.apache.hadoop.mapred.TextInputFormat
Location: !Sub "s3://${ResourcePrefix}-${AWS::AccountId}-cf-access-logs/${GzKeyPrefix}"
SerdeInfo:
Parameters:
field.delim": "\t"
serialization.format: "\t"
SerializationLibrary: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
PartitionedParquetTable:
Type: AWS::Glue::Table
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseName: !Ref CfLogsDatabase
TableInput:
Name: 'partitioned_parquet'
Description: 'Parquet format access logs as transformed from gzip version'
TableType: EXTERNAL_TABLE
Parameters: { 'has_encrypted_data': 'false', 'parquet.compression': 'SNAPPY' }
PartitionKeys:
- Name: year
Type: string
- Name: month
Type: string
- Name: day
Type: string
- Name: hour
Type: string
StorageDescriptor:
OutputFormat: org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat
Columns:
- Name: date
Type: date
- Name: time
Type: string
- Name: location
Type: string
- Name: bytes
Type: bigint
- Name: request_ip
Type: string
- Name: method
Type: string
- Name: host
Type: string
- Name: uri
Type: string
- Name: status
Type: int
- Name: referrer
Type: string
- Name: user_agent
Type: string
- Name: query_string
Type: string
- Name: cookie
Type: string
- Name: result_type
Type: string
- Name: request_id
Type: string
- Name: host_header
Type: string
- Name: request_protocol
Type: string
- Name: request_bytes
Type: bigint
- Name: time_taken
Type: float
- Name: xforwarded_for
Type: string
- Name: ssl_protocol
Type: string
- Name: ssl_cipher
Type: string
- Name: response_result_type
Type: string
- Name: http_version
Type: string
- Name: fle_status
Type: string
- Name: fle_encrypted_fields
Type: int
- Name: c_port
Type: int
- Name: time_to_first_byte
Type: float
- Name: x_edge_detailed_result_type
Type: string
- Name: sc_content_type
Type: string
- Name: sc_content_len
Type: bigint
- Name: sc_range_start
Type: bigint
- Name: sc_range_end
Type: bigint
InputFormat: org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat
Location: !Sub "s3://${ResourcePrefix}-${AWS::AccountId}-cf-access-logs/${ParquetKeyPrefix}"
SerdeInfo:
SerializationLibrary: org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe
CombinedView:
Type: AWS::Glue::Table
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseName: !Ref CfLogsDatabase
TableInput:
Name: 'combined'
Description: 'combined view over gzip and parquet tables'
TableType: VIRTUAL_VIEW
Parameters: { 'presto_view': 'true' }
PartitionKeys: []
StorageDescriptor:
Columns:
- Name: date
Type: date
- Name: time
Type: string
- Name: location
Type: string
- Name: bytes
Type: bigint
- Name: request_ip
Type: string
- Name: method
Type: string
- Name: host
Type: string
- Name: uri
Type: string
- Name: status
Type: int
- Name: referrer
Type: string
- Name: user_agent
Type: string
- Name: query_string
Type: string
- Name: cookie
Type: string
- Name: result_type
Type: string
- Name: request_id
Type: string
- Name: host_header
Type: string
- Name: request_protocol
Type: string
- Name: request_bytes
Type: bigint
- Name: time_taken
Type: float
- Name: xforwarded_for
Type: string
- Name: ssl_protocol
Type: string
- Name: ssl_cipher
Type: string
- Name: response_result_type
Type: string
- Name: http_version
Type: string
- Name: fle_status
Type: string
- Name: fle_encrypted_fields
Type: int
- Name: c_port
Type: int
- Name: time_to_first_byte
Type: float
- Name: x_edge_detailed_result_type
Type: string
- Name: sc_content_type
Type: string
- Name: sc_content_len
Type: bigint
- Name: sc_range_start
Type: bigint
- Name: sc_range_end
Type: bigint
- Name: year
Type: string
- Name: month
Type: string
- Name: day
Type: string
- Name: hour
Type: string
- Name: file
Type: string
SerdeInfo: {}
ViewOriginalText:
Fn::Join:
- ''
- - '/* Presto View: '
- Fn::Base64:
Fn::Sub:
- |-
{
"originalSql": "SELECT *, \"$path\" as file FROM ${database}.${partitioned_gz_table} WHERE (concat(year, month, day, hour) >= date_format(date_trunc('hour', ((current_timestamp - INTERVAL '15' MINUTE) - INTERVAL '1' HOUR)), '%Y%m%d%H')) UNION ALL SELECT *, \"$path\" as file FROM ${database}.${partitioned_parquet_table} WHERE (concat(year, month, day, hour) < date_format(date_trunc('hour', ((current_timestamp - INTERVAL '15' MINUTE) - INTERVAL '1' HOUR)), '%Y%m%d%H'))",
"catalog": "awsdatacatalog",
"schema": "${database}",
"columns": [
{"name": "date", "type": "date"},
{"name": "time", "type": "varchar"},
{"name": "location", "type": "varchar"},
{"name": "bytes", "type": "bigint"},
{"name": "request_ip", "type": "varchar"},
{"name": "method", "type": "varchar"},
{"name": "host", "type": "varchar"},
{"name": "uri", "type": "varchar"},
{"name": "status", "type": "integer"},
{"name": "referrer", "type": "varchar"},
{"name": "user_agent", "type": "varchar"},
{"name": "query_string", "type": "varchar"},
{"name": "cookie", "type": "varchar"},
{"name": "result_type", "type": "varchar"},
{"name": "request_id", "type": "varchar"},
{"name": "host_header", "type": "varchar"},
{"name": "request_protocol", "type": "varchar"},
{"name": "request_bytes", "type": "bigint"},
{"name": "time_taken", "type": "real"},
{"name": "xforwarded_for", "type": "varchar"},
{"name": "ssl_protocol", "type": "varchar"},
{"name": "ssl_cipher", "type": "varchar"},
{"name": "response_result_type", "type": "varchar"},
{"name": "http_version", "type": "varchar"},
{"name": "fle_status", "type": "varchar"},
{"name": "fle_encrypted_fields", "type": "integer"},
{"name": "c_port", "type": "integer"},
{"name": "time_to_first_byte", "type": "real"},
{"name": "x_edge_detailed_result_type", "type": "varchar"},
{"name": "sc_content_type", "type": "varchar"},
{"name": "sc_content_len", "type": "bigint"},
{"name": "sc_range_start", "type": "bigint"},
{"name": "sc_range_end", "type": "bigint"},
{"name": "year", "type": "varchar"},
{"name": "month", "type": "varchar"},
{"name": "day", "type": "varchar"},
{"name": "hour", "type": "varchar"},
{"name": "file", "type": "varchar"}
]
}
- { database: !Ref CfLogsDatabase,
partitioned_gz_table: !Ref PartitionedGzTable,
partitioned_parquet_table: !Ref PartitionedParquetTable }
- ' */'