This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
digital-user-engagement-events-database.template
1242 lines (1177 loc) · 49.2 KB
/
digital-user-engagement-events-database.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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Digital User Engagement Events Database Solution
#
# template for digital-user-engagement-events-database
# **DO NOT DELETE**
#
# author: rjlowe@
AWSTemplateFormatVersion: 2010-09-09
Description: (SO0105) - Automatically provision and configure the AWS services necessary to start analyzing real-time data from Amazon Simple Email Service and Amazon Pinpoint using Amazon Athena
Parameters:
PinpointProjectId:
Type: String
Description: Amazon Pinpoint Project ID if one already exists, leave blank to create one. Ensure existing Pinpoint Event Stream is removed.
AllowedPattern: ^[a-zA-Z0-9]*$
PinpointProjectName:
Type: String
Default: "My Pinpoint Project"
Description: "If no PinpointProjectId provided, name to be used to create the Pinpoint project"
ConfigurationSetNames:
Type: String
Description: If already configured, Comma delimitated list of existing SES configuration sets to update with a Pinpoint event destination. Ensure no Pinpoint Event Destination is already configured.
EventAthenaDatabaseName:
Type: String
Description: Name of the Athena database created in Glue, must be lowercase
Default: "due_eventdb"
AllowedPattern: ^[a-z][a-z0-9_]*$
ConstraintDescription: Only lower case and the underscore (_) characters are allowed
Mappings:
MetricsMap:
Send-Data:
ID: SO0105
Version: "v1.0.0"
SendAnonymousData: "Yes" # change to 'No' if needed
SourceCode:
General:
S3Bucket: "solutions"
KeyPrefix: "digital-user-engagement-events-database/v1.0.0"
#By default this CloudFormation template retrieves the AWS Lambda source code for the CustomBucket & CustomResource helper functions from an AWS managed S3 bucket. If you wish to use the AWS Lambda code in this GitHub repository and host it in your own Amazon S3 bucket, you can un-comment the two resources below (MetricsMap and SourceCode) and follow the GitHub ReadMe instructions.
# MetricsMap:
# Send-Data:
# ID: SO0105
# Version: "%%VERSION%%"
# SendAnonymousData: "Yes" # change to 'No' if needed
# SourceCode:
# General:
# S3Bucket: "%%BUCKET_NAME%%"
# KeyPrefix: "%%SOLUTION_NAME%%/%%VERSION%%"
LambdaRuntime:
Language:
Python: python3.11
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: Amazon Pinpoint Project Configuration
Parameters:
- PinpointProjectId
- PinpointProjectName
-
Label:
default: Amazon Simple Email Service Configuration
Parameters:
- ConfigurationSetNames
-
Label:
default: Amazon Athena Data Lake Configuration
Parameters:
- EventAthenaDatabaseName
ParameterLabels:
PinpointProjectId:
default: Amazon Pinpoint Project ID (Optional)
PinpointProjectName:
default: Amazon Pinpoint Project Name
ConfigurationSetNames:
default: Existing Amazon Simple Email Service Configuration Set Names
EventAthenaDatabaseName:
default: Amazon Athena / AWS Glue Database Name
Conditions:
NeedsPinpointProjectId: !Equals ['', !Ref PinpointProjectId]
Resources:
PinpointProject:
Type: AWS::Pinpoint::App
Condition: NeedsPinpointProjectId
DeletionPolicy: Retain
Properties:
Name: !Ref PinpointProjectName
##### S3 BUCKETS
#######################################
DUES3DataLake:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Metadata:
cfn_nag:
rules_to_suppress:
- id: W51
reason: Not public facing.
Properties:
BucketName: !Ref CreateBucketName
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerPreferred
PublicAccessBlockConfiguration:
BlockPublicAcls: True
BlockPublicPolicy: True
IgnorePublicAcls: True
RestrictPublicBuckets: True
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
LoggingConfiguration:
DestinationBucketName: !Ref LogBucket
LogFilePrefix: pinpoint-event-processing/
NotificationConfiguration:
LambdaConfigurations:
-
Event: "s3:ObjectCreated:*"
Filter:
S3Key:
Rules:
- Name: "prefix"
Value: "events/"
Function: !GetAtt AthenaPartitionLambda.Arn
DUES3DataLakePolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref DUES3DataLake
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: AllowSSLRequestsOnly
Effect: Deny
Principal: "*"
Action: "s3:*"
Resource:
- !Sub "arn:aws:s3:::${CreateBucketName}/*"
- !Sub "arn:aws:s3:::${CreateBucketName}"
Condition:
Bool:
"aws:SecureTransport": "false"
S3InvokeLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref AthenaPartitionLambda
Principal: s3.amazonaws.com
SourceArn: !Sub arn:aws:s3:::${CreateBucketName}
SourceAccount: !Ref "AWS::AccountId"
LogBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Metadata:
cfn_nag:
rules_to_suppress:
- id: W35
reason: This is the log bucket.
Properties:
AccessControl: LogDeliveryWrite
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerPreferred
PublicAccessBlockConfiguration:
BlockPublicAcls: True
BlockPublicPolicy: True
IgnorePublicAcls: True
RestrictPublicBuckets: True
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
LogBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref LogBucket
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: AWSCloudTrailAclCheck
Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Action: "s3:GetBucketAcl"
Resource: !Sub arn:aws:s3:::${LogBucket}
- Sid: AWSCloudTrailWrite
Effect: Allow
Principal:
Service: cloudtrail.amazonaws.com
Action: "s3:PutObject"
Resource: !Sub arn:aws:s3:::${LogBucket}/AWSLogs/${AWS::AccountId}/*
Condition:
StringEquals:
"s3:x-amz-acl": "bucket-owner-full-control"
- Sid: LogBucketAllowSSLRequestsOnly
Effect: Deny
Principal: "*"
Action: "s3:*"
Resource:
- !Sub "arn:aws:s3:::${LogBucket}/*"
- !Sub "arn:aws:s3:::${LogBucket}"
Condition:
Bool:
"aws:SecureTransport": "false"
#### KINESIS FIREHOSE - Pinpoint
#######################################
PinpointEventStream:
Type: AWS::Pinpoint::EventStream
Properties:
ApplicationId: !If
- NeedsPinpointProjectId
- !Ref PinpointProject
- !Ref PinpointProjectId
DestinationStreamArn: !GetAtt PinpointEventFirehose.Arn
RoleArn: !GetAtt PinpointKinesisStreamRole.Arn
PinpointEventFirehose:
Type: AWS::KinesisFirehose::DeliveryStream
DependsOn: SetupSampleFiles
Properties:
DeliveryStreamType: "DirectPut"
ExtendedS3DestinationConfiguration:
BucketARN: !Sub "arn:aws:s3:::${DUES3DataLake}"
BufferingHints:
IntervalInSeconds: 300
SizeInMBs: 128
DataFormatConversionConfiguration:
Enabled: true
InputFormatConfiguration:
Deserializer:
OpenXJsonSerDe: {}
OutputFormatConfiguration:
Serializer:
ParquetSerDe: {}
SchemaConfiguration:
DatabaseName: !Ref PinpointEventDatabase
Region: !Ref AWS::Region
RoleARN: !GetAtt PinpointKinesisFirehoseRole.Arn
TableName: "all_events"
VersionId: "LATEST"
CatalogId: !Ref AWS::AccountId
CompressionFormat: "UNCOMPRESSED"
Prefix: "events/"
ErrorOutputPrefix: "errors/"
RoleARN: !GetAtt PinpointKinesisFirehoseRole.Arn
CloudWatchLoggingOptions:
Enabled: true
LogGroupName: !Sub "/aws/kinesisfirehose/${AWS::StackName}-pinpoint-eventstream-firehose"
LogStreamName: "S3DeliveryErrors"
KinesisFirehoseLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/kinesisfirehose/${AWS::StackName}-pinpoint-eventstream-firehose"
KinesisFirehoseLogStreamName:
Type: AWS::Logs::LogStream
DependsOn: KinesisFirehoseLogGroup
Properties:
LogGroupName: !Sub "/aws/kinesisfirehose/${AWS::StackName}-pinpoint-eventstream-firehose"
LogStreamName: "S3DeliveryErrors"
PinpointKinesisStreamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- pinpoint.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: "/"
Policies:
-
PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "firehose:PutRecordBatch"
- "firehose:DescribeDeliveryStream"
Resource: !GetAtt PinpointEventFirehose.Arn
PinpointKinesisFirehoseRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- firehose.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: "/"
Policies:
-
PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "s3:AbortMultipartUpload"
- "s3:GetBucketLocation"
- "s3:GetObject"
- "s3:ListBucket"
- "s3:ListBucketMultipartUploads"
- "s3:PutObject"
Resource:
- !Sub "arn:aws:s3:::${CreateBucketName}/*"
- !Sub "arn:aws:s3:::${CreateBucketName}"
-
Effect: "Allow"
Action:
- "glue:GetTable"
- "glue:GetTableVersion"
- "glue:GetTableVersions"
Resource:
- !Sub "arn:aws:glue:${AWS::Region}:${AWS::AccountId}:table/${EventAthenaDatabaseName}/*"
- !Sub "arn:aws:glue:${AWS::Region}:${AWS::AccountId}:database/${EventAthenaDatabaseName}"
- !Sub "arn:aws:glue:${AWS::Region}:${AWS::AccountId}:catalog"
-
Effect: "Allow"
Action: "logs:PutLogEvents"
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/kinesisfirehose/*:log-stream:*"
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*"
#### GLUE AND ATHENA
#######################################
PinpointEventDatabase:
Type: AWS::Glue::Database
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseInput:
Name: !Ref EventAthenaDatabaseName
Description: "SES and Pinpoint Streaming Event Database"
EventTableAllNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref EventAthenaDatabaseName
Description: "Create a Table for All events"
QueryString:
!Sub
|
CREATE EXTERNAL TABLE IF NOT EXISTS `all_events` (
event_type string,
event_timestamp bigint,
arrival_timestamp bigint,
event_version string,
application struct<app_id:string,sdk:map<string,string>>,
client struct<client_id:string,cognito_id:string>,
device struct<platform:map<string,string>>,
session map<string,string>,
attributes map<string,string>,
client_context struct<custom:map<string,string>>,
awsAccountId string,
facets struct<
email_channel:struct<
mail_event:struct<
mail:struct<
message_id:string,
message_send_timestamp:bigint,
from_address:string,
destination:array<string>,
headers_truncated:boolean,
headers:array<map<string,string>>,
common_headers:struct<
`from`:string,
to:array<string>,
subject:string
>
>,
send:map<string,string>,
delivery:struct<
smtp_response:string,
reporting_mta:string,
recipients:array<string>,
processing_time_millis:int
>,
`open`:struct<
`ip_address`:string,
`user_agent`:string
>,
`click`:struct<
`ip_address`:string,
`user_agent`:string,
`link`:string,
`link_tags`:struct<
`unsubscribeLinkTag`:array<string>
>
>,
reject:struct<
reason:string
>,
complaint:struct<
complained_recipients:array<struct<email_address:string>>,
feedback_id:string,
user_agent:string,
complaint_feedback_type:string
>,
`bounce`:struct<
bounce_type:string,
bounce_sub_type:string,
bounced_recipients:array<struct<email_address:string,`action`:string,`status`:string,diagnostic_code:string>>,
feedback_id:string,
reporting_mta:string
>
>
>
>,
metrics struct<price_in_millicents_usd:double>
)
PARTITIONED BY (ingest_timestamp timestamp)
STORED AS parquet
LOCATION 's3://${CreateBucketName}/events'
TBLPROPERTIES ("parquet.compression"="SNAPPY")
EventTableSendNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Email Send View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW email_send AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, facets.email_channel.mail_event.mail.message_id message_id
, from_unixtime((facets.email_channel.mail_event.mail.message_send_timestamp / 1000)) message_send_timestamp
, facets.email_channel.mail_event.mail.from_address from_address
, facets.email_channel.mail_event.mail.destination destination
, facets.email_channel.mail_event.mail.common_headers.subject as subject
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_email.send')
EventTableHardBounceNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Email Hard Bounce View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW email_hardbounce AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, facets.email_channel.mail_event.mail.message_id message_id
, from_unixtime((facets.email_channel.mail_event.mail.message_send_timestamp / 1000)) message_send_timestamp
, facets.email_channel.mail_event.mail.from_address from_address
, facets.email_channel.mail_event.mail.destination destination
, facets.email_channel.mail_event.bounce.bounce_type as bounce_type
, facets.email_channel.mail_event.bounce.bounce_sub_type as bounce_sub_type
, facets.email_channel.mail_event.bounce.feedback_id as feedback_id
, facets.email_channel.mail_event.bounce.reporting_mta as reporting_mta
, bounced_recipient.email_address as bounced_recipient_email_address
, bounced_recipient.action as bounced_recipient_action
, bounced_recipient.status as bounced_recipient_status
, bounced_recipient.diagnostic_code as bounced_recipient_diagnostic_code
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
CROSS JOIN UNNEST(facets.email_channel.mail_event.bounce.bounced_recipients) as t(bounced_recipient)
WHERE (event_type = '_email.hardbounce')
EventTableSoftBounceNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Email Soft Bounce View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW email_softbounce AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, facets.email_channel.mail_event.mail.message_id message_id
, from_unixtime((facets.email_channel.mail_event.mail.message_send_timestamp / 1000)) message_send_timestamp
, facets.email_channel.mail_event.mail.from_address from_address
, facets.email_channel.mail_event.mail.destination destination
, facets.email_channel.mail_event.bounce.bounce_type as bounce_type
, facets.email_channel.mail_event.bounce.bounce_sub_type as bounce_sub_type
, facets.email_channel.mail_event.bounce.feedback_id as feedback_id
, facets.email_channel.mail_event.bounce.reporting_mta as reporting_mta
, bounced_recipient.email_address as bounced_recipient_email_address
, bounced_recipient.action as bounced_recipient_action
, bounced_recipient.status as bounced_recipient_status
, bounced_recipient.diagnostic_code as bounced_recipient_diagnostic_code
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
CROSS JOIN UNNEST(facets.email_channel.mail_event.bounce.bounced_recipients) as t(bounced_recipient)
WHERE (event_type = '_email.softbounce')
EventTableComplaintNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Email Complaint View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW email_complaint AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, facets.email_channel.mail_event.mail.message_id message_id
, from_unixtime((facets.email_channel.mail_event.mail.message_send_timestamp / 1000)) message_send_timestamp
, facets.email_channel.mail_event.mail.from_address from_address
, facets.email_channel.mail_event.mail.destination destination
, complained_recipient.email_address as complained_email_address
, facets.email_channel.mail_event.complaint.feedback_id as feedback_id
, facets.email_channel.mail_event.complaint.user_agent as user_agent
, facets.email_channel.mail_event.complaint.complaint_feedback_type as complaint_feedback_type
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
CROSS JOIN UNNEST(facets.email_channel.mail_event.complaint.complained_recipients) as t(complained_recipient)
WHERE (event_type = '_email.complaint')
EventTableDeliveryNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Email Delivered View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW email_delivered AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, facets.email_channel.mail_event.mail.message_id message_id
, from_unixtime((facets.email_channel.mail_event.mail.message_send_timestamp / 1000)) message_send_timestamp
, facets.email_channel.mail_event.mail.from_address from_address
, facets.email_channel.mail_event.mail.destination destination
, facets.email_channel.mail_event.mail.common_headers.subject as subject
, facets.email_channel.mail_event.delivery.smtp_response as smtp_response
, facets.email_channel.mail_event.delivery.reporting_mta as reporting_mta
, facets.email_channel.mail_event.delivery.recipients as recipients
, facets.email_channel.mail_event.delivery.processing_time_millis as processing_time_millis
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_email.delivered')
EventTableOpenNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Email Open View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW email_open AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, facets.email_channel.mail_event.mail.message_id message_id
, from_unixtime((facets.email_channel.mail_event.mail.message_send_timestamp / 1000)) message_send_timestamp
, facets.email_channel.mail_event.mail.from_address from_address
, facets.email_channel.mail_event.mail.destination destination
, facets.email_channel.mail_event.mail.common_headers.subject as subject
, facets.email_channel.mail_event.open.ip_address as ip_address
, facets.email_channel.mail_event.open.user_agent as user_agent
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_email.open')
EventTableClickNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Email Click View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW email_click AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, facets.email_channel.mail_event.mail.message_id message_id
, from_unixtime((facets.email_channel.mail_event.mail.message_send_timestamp / 1000)) message_send_timestamp
, facets.email_channel.mail_event.mail.from_address from_address
, facets.email_channel.mail_event.mail.destination destination
, facets.email_channel.mail_event.mail.common_headers.subject as subject
, facets.email_channel.mail_event.click.ip_address as ip_address
, facets.email_channel.mail_event.click.user_agent as user_agent
, facets.email_channel.mail_event.click.link as link
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_email.click')
EventTableUnsubNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Email Unsub View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW email_unsubscribe AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, facets.email_channel.mail_event.mail.message_id message_id
, from_unixtime((facets.email_channel.mail_event.mail.message_send_timestamp / 1000)) message_send_timestamp
, facets.email_channel.mail_event.mail.from_address from_address
, facets.email_channel.mail_event.mail.destination destination
, facets.email_channel.mail_event.mail.common_headers.subject as subject
, facets.email_channel.mail_event.click.ip_address as ip_address
, facets.email_channel.mail_event.click.user_agent as user_agent
, facets.email_channel.mail_event.click.link as link
, facets.email_channel.mail_event.click.link_tags.unsubscribeLinkTag
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_email.unsubscribe')
EventTableRejectNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Email Reject View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW email_rejected AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, facets.email_channel.mail_event.mail.message_id message_id
, from_unixtime((facets.email_channel.mail_event.mail.message_send_timestamp / 1000)) message_send_timestamp
, facets.email_channel.mail_event.mail.from_address from_address
, facets.email_channel.mail_event.mail.destination destination
, facets.email_channel.mail_event.mail.common_headers.subject as subject
, facets.email_channel.mail_event.reject.reason as reject_reason
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_email.rejected')
EventTableSMSBufferedNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the SMS Buffered View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW sms_buffered AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, attributes['sender_request_id'] as sender_request_id
, attributes['destination_phone_number'] as destination_phone_number
, attributes['record_status'] as record_status
, attributes['iso_country_code'] as iso_country_code
, attributes['number_of_message_parts'] as number_of_message_parts
, attributes['message_id'] as message_id
, attributes['message_type'] as message_type
, metrics.price_in_millicents_usd as price_in_millicents_usd
, CAST(JSON_PARSE(attributes['customer_context']) AS MAP(VARCHAR, VARCHAR)) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_SMS.BUFFERED')
EventTableSMSSuccessNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the SMS Success View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW sms_success AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, attributes['sender_request_id'] as sender_request_id
, attributes['destination_phone_number'] as destination_phone_number
, attributes['record_status'] as record_status
, attributes['iso_country_code'] as iso_country_code
, attributes['number_of_message_parts'] as number_of_message_parts
, attributes['message_id'] as message_id
, attributes['message_type'] as message_type
, attributes['origination_phone_number'] as origination_phone_number
, metrics.price_in_millicents_usd as price_in_millicents_usd
, CAST(JSON_PARSE(attributes['customer_context']) AS MAP(VARCHAR, VARCHAR)) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_SMS.SUCCESS')
EventTableSMSFailureNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the SMS Failure View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW sms_failure AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, attributes['sender_request_id'] as sender_request_id
, attributes['destination_phone_number'] as destination_phone_number
, attributes['record_status'] as record_status
, attributes['iso_country_code'] as iso_country_code
, attributes['number_of_message_parts'] as number_of_message_parts
, attributes['message_id'] as message_id
, metrics.price_in_millicents_usd as price_in_millicents_usd
, CAST(JSON_PARSE(attributes['customer_context']) AS MAP(VARCHAR, VARCHAR)) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_SMS.FAILURE')
EventTableSMSOptOutNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the SMS Output View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW sms_optout AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, attributes['sender_request_id'] as sender_request_id
, attributes['destination_phone_number'] as destination_phone_number
, attributes['record_status'] as record_status
, attributes['iso_country_code'] as iso_country_code
, attributes['number_of_message_parts'] as number_of_message_parts
, attributes['message_id'] as message_id
, attributes['message_type'] as message_type
, attributes['origination_phone_number'] as origination_phone_number
, metrics.price_in_millicents_usd as price_in_millicents_usd
, CAST(JSON_PARSE(attributes['customer_context']) AS MAP(VARCHAR, VARCHAR)) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_SMS.OPTOUT')
EventTableCampaignSendNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Campaign Send View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW campaign_send AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['campaign_id'] pinpoint_campaign_id
, attributes['treament_id'] pinpoint_treatment_id
, awsaccountid aws_account_id
, attributes as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_campaign.send')
EventTableJourneySendNamedQuery:
Type: AWS::Athena::NamedQuery
DeletionPolicy: Delete
Properties:
Database: !Ref PinpointEventDatabase
Description: "Create the Journey Send View"
QueryString:
!Sub
|
CREATE OR REPLACE VIEW journey_send AS
SELECT
from_unixtime((event_timestamp / 1000)) event_timestamp
, from_unixtime((arrival_timestamp / 1000)) arrival_timestamp
, application.app_id application_id
, client.client_id endpoint_id
, attributes['journey_run_id'] journey_run_id
, attributes['journey_send_status'] journey_send_status
, attributes['journey_id'] journey_id
, attributes['journey_activity_id'] journey_activity_id
, awsaccountid aws_account_id
, MAP_CONCAT(COALESCE(client_context.custom, CAST(JSON '{}' AS MAP(varchar,varchar))), attributes) as message_tags
, ingest_timestamp
FROM
all_events
WHERE (event_type = '_journey.send')
### Partitioner Lambda Helper
AthenaPartitionLambda:
Type: AWS::Lambda::Function
Properties:
Role: !GetAtt AthenaPartitionLambdaRole.Arn
Timeout: 60
Environment:
Variables:
LOG_LEVEL: "INFO"
DATABASE_NAME: !Ref EventAthenaDatabaseName
SOLUTION_ID: !FindInMap ["MetricsMap", "Send-Data", "ID"]
SOLUTION_VERSION: !FindInMap ["MetricsMap", "Send-Data", "Version"]
SEND_ANONYMOUS_DATA: !FindInMap ["MetricsMap", "Send-Data", "SendAnonymousData"]
SOLUTION_UUID: !GetAtt GenerateUUID.UUID
Handler: lambda_function.lambda_handler
Runtime: !FindInMap ["LambdaRuntime", "Language", "Python"]
Code:
S3Bucket: !Join ["-", [!FindInMap ["SourceCode", "General", "S3Bucket"], Ref: "AWS::Region" ]]
S3Key: !Join ["/", [!FindInMap ["SourceCode", "General", "KeyPrefix"], "athena-partition-lambda.zip"]]
AthenaPartitionLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
-
PolicyName: "root"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource: !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*"
AthenaPartitionLambdaRolePolicy:
Type: AWS::IAM::Policy
Properties:
Roles:
- !Ref AthenaPartitionLambdaRole
PolicyName: "AthenaPartitionLambdaRolePolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "s3:GetBucketLocation"
- "s3:GetObject"
- "s3:ListBucket"
- "s3:ListBucketMultipartUploads"
- "s3:ListMultipartUploadParts"
- "s3:AbortMultipartUpload"
- "s3:CreateBucket"
- "s3:PutObject"
Resource:
- !Sub arn:aws:s3:::${CreateBucketName}