-
Notifications
You must be signed in to change notification settings - Fork 51
/
Athena_infra_setup.yml
1132 lines (1106 loc) · 50.9 KB
/
Athena_infra_setup.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
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Create Athena Bootstrap Infrastructure Resources including default analyst Workgroup and CloudTrail, Flow Log, and DNS Resolver log Tables'
Metadata:
'AWS::CloudFormation::Interface':
ParameterGroups:
- Label:
default: 'Athena Environment Parameters'
Parameters:
- ParamWorkgroupName
- ParamWorkgroupDescription
- ParamQueryOutputLocationName
- Label:
default: 'Glue Database Parameters'
Parameters:
- ParamDatabaseName
- Label:
default: 'Glue CloudTrail Table Parameters'
Parameters:
- ParamCloudTrailTableEnabled
- ParamCloudTrailTableName
- ParamCloudTrailSource
- ParamCloudTrailProjectionEventStartDate
- ParamCloudTrailAccountEnum
- ParamCloudTrailRegionEnum
- Label:
default: 'Glue VPC Flow Log Table Parameters'
Parameters:
- ParamVPCFlowTableEnabled
- ParamVPCFlowTableName
- ParamVPCFlowSource
- ParamVPCFlowProjectionEventStartDate
- ParamVPCFlowAccountEnum
- ParamVPCFlowRegionEnum
- Label:
default: 'Glue Route53 DNS Resolver Log Table Parameters'
Parameters:
- ParamDNSResolverTableEnabled
- ParamDNSResolverTableName
- ParamDNSResolverSource
- ParamDNSResolverProjectionEventStartDate
- ParamDNSResolverAccountEnum
- ParamDNSResolverVPCEnum
Parameters:
# Athena Environment Parameters
ParamWorkgroupName:
Description: 'Name of the initial Athena Security Analysis workgroup to create'
Type: String
Default: SecurityAnalysis
ParamWorkgroupDescription:
Description: 'Description of the initial Athena Security Analysis workgroup'
Type: String
Default: Security Analysis Athena Workgroup
ParamQueryOutputLocationName:
Description: 'S3 path to write all query results, store 45-day query history, and created tables via queries'
Type: String
Default: s3://query_history_bucket/optional_prefix/
# Glue Database Parameters
ParamDatabaseName:
Description: 'Name of the Glue database to create, which will contain all security analysis tables created by this template (**cannot contain hyphen**)'
Type: String
Default: security_analysis
# CloudTrail Table Parameters
ParamCloudTrailTableEnabled:
Description: 'Do you want to create and enable a table for CloudTrail?'
Type: String
Default: 'YES'
AllowedValues:
- 'YES'
- 'NO'
ParamCloudTrailTableName:
Description: 'Name of the cloudtrail Glue table to create'
Type: String
Default: cloudtrail
ParamCloudTrailSource:
Description: "S3 base path of CloudTrail logs to be included in the CloudTrail table (must end with /AWSLogs/ or /AWSLogs/<your_org_id>/ if you're using an organization trail)"
Type: String
Default: s3://<bucket>/<prefix>/AWSLogs/
ParamCloudTrailProjectionEventStartDate:
Description: 'Start date for CloudTrail logs (replace <YYYY>/<MM>/<DD> with the first date of your logs, example: 2020/11/30)'
Type: String
Default: '<YYYY>/<MM>/<DD>'
ParamCloudTrailAccountEnum:
Description: 'Account(s) to include in the CloudTrail log table in a comma separated list with NO SPACES (example: "0123456789,0123456788,0123456777"); note that all accounts must be logging to the same source, with contents in {ParamVPCFlowSource}/AWSLogs/{account_number}/CloudTrail'
Type: String
Default: '<acount_num_1>,<account_num_2>,...'
ParamCloudTrailRegionEnum:
Description: 'Regions to include in the CloudTrail log table in a comma separated list with NO SPACES; Include all regions for full coverage even if there are no logs currently in that region'
Type: String
Default: us-east-2,us-east-1,us-west-1,us-west-2,af-south-1,ap-east-1,ap-south-1,ap-northeast-3,ap-northeast-2,ap-southeast-1,ap-southeast-2,ap-northeast-1,ca-central-1,cn-north-1,cn-northwest-1,eu-central-1,eu-west-1,eu-west-2,eu-south-1,eu-west-3,eu-north-1,me-south-1,sa-east-1
# VPC Flow Log Table Parameters
ParamVPCFlowTableEnabled:
Description: 'Do you want to create and enable a table for VPC Flow Logs?'
Type: String
Default: 'YES'
AllowedValues:
- 'YES'
- 'NO'
ParamVPCFlowTableName:
Description: 'Name of the VPC flow log Glue table to create'
Type: String
Default: vpcflow
ParamVPCFlowSource:
Description: 'S3 base path of VPC flow logs to be included in the VPC flow table (must end with /AWSLogs/)'
Type: String
Default: s3://<bucket>/<prefix>/AWSLogs/
ParamVPCFlowProjectionEventStartDate:
Description: 'Start date for VPC flow logs (replace <YYYY>/<MM>/<DD> with the first date of your logs, example: 2020/11/30)'
Type: String
Default: '<YYYY>/<MM>/<DD>'
ParamVPCFlowAccountEnum:
Description: 'Account(s) to include in the VPC flow log table in a comma separated list with NO SPACES (example: "0123456789,0123456788,0123456777"); note that all accounts must be logging to the same source, with contents in {ParamVPCFlowSource}/AWSLogs/{account_number}/vpcflowlogs/'
Type: String
Default: '<acount_num_1>,<account_num_2>,...'
ParamVPCFlowRegionEnum:
Description: 'Regions to include in the VPC flow log table in a comma separated list with NO SPACES; Include all regions for full coverage even if there are no logs currently in that region'
Type: String
Default: us-east-2,us-east-1,us-west-1,us-west-2,af-south-1,ap-east-1,ap-south-1,ap-northeast-3,ap-northeast-2,ap-southeast-1,ap-southeast-2,ap-northeast-1,ca-central-1,cn-north-1,cn-northwest-1,eu-central-1,eu-west-1,eu-west-2,eu-south-1,eu-west-3,eu-north-1,me-south-1,sa-east-1
# DNS Resolver Log Table Parameters
ParamDNSResolverTableEnabled:
Description: 'Do you want to create and enable a table for Route53 DNS Resolver Logs?'
Type: String
Default: 'YES'
AllowedValues:
- 'YES'
- 'NO'
ParamDNSResolverTableName:
Description: 'Name of the Route53 DNS Resolver log Glue table to create'
Type: String
Default: r53dns
ParamDNSResolverSource:
Description: 'S3 base path of Route53 DNS Resolver logs to be included in the Route53 DNS Resolver table (must end with /AWSLogs/)'
Type: String
Default: s3://<bucket>/<prefix>/AWSLogs/
ParamDNSResolverProjectionEventStartDate:
Description: 'Start date for Route53 DNS Resolver logs (replace <YYYY>/<MM>/<DD> with the first date of your logs, example: 2020/11/30)'
Type: String
Default: '<YYYY>/<MM>/<DD>'
ParamDNSResolverAccountEnum:
Description: 'Account(s) to include in the Route53 DNS Resolver log table in a comma separated list with NO SPACES (example: "0123456789,0123456788,0123456777"); note that all accounts must be logging to the same source, with contents in {ParamVPCFlowSource}/AWSLogs/{account_number}/vpcdnsquerylogs/'
Type: String
Default: <acount_num_1>,<account_num_2>,...
ParamDNSResolverVPCEnum:
Description: 'VPC IDs to include in the Route53 DNS Resolver log table in a comma separated list with NO SPACES; Include all VPC IDs for full coverage even if there are no logs currently in that VPC'
Type: String
Default: <vpc_id_1>,<vpc_id_2>,...
Conditions:
ConditionCreateCloudTrailTable: !Equals
- !Ref ParamCloudTrailTableEnabled
- 'YES'
ConditionCreateVPCFlowTable: !Equals
- !Ref ParamVPCFlowTableEnabled
- 'YES'
ConditionCreateDNSResolverTable: !Equals
- !Ref ParamDNSResolverTableEnabled
- 'YES'
Resources:
# Create an AWS Athena Workgroup
ResourceAthenaWorkgroupDefault:
Type: AWS::Athena::WorkGroup
Properties:
Description: !Ref ParamWorkgroupDescription
Name: !Ref ParamWorkgroupName
RecursiveDeleteOption: TRUE
State: ENABLED
WorkGroupConfiguration:
# EnforceWorkGroupConfiguration is True , Athena uses the workgroup-wide settings for query
# results location and encryption. It also overrides any other settings that you specified
# for the query in the console, by using the API operations, or with the drivers.
# This affects you only if you run queries in this workgroup. If you do, workgroup settings are used
EnforceWorkGroupConfiguration: TRUE
EngineVersion:
SelectedEngineVersion: 'Athena engine version 3'
PublishCloudWatchMetricsEnabled: FALSE
RequesterPaysEnabled: FALSE
ResultConfiguration:
OutputLocation: !Ref ParamQueryOutputLocationName
EncryptionConfiguration:
EncryptionOption: SSE_S3
# Create an AWS Glue database
ResourceDatabaseASL:
Type: AWS::Glue::Database
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseInput:
Name: !Ref ParamDatabaseName
Description: Database to hold tables for AWS Service logs logs
# Create an AWS Glue table
ResourceTableCloudTrail:
# Creating the table waits for the database to be created
DependsOn: ResourceDatabaseASL
Type: AWS::Glue::Table
Condition: ConditionCreateCloudTrailTable
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseName: !Ref ParamDatabaseName
TableInput:
Name: !Ref ParamCloudTrailTableName
Description: Table for CloudTrail logs
TableType: EXTERNAL_TABLE
Parameters: {
"classification": "json",
"EXTERNAL": "true",
"projection.enabled": "true",
"projection.date_partition.type": "date",
"projection.date_partition.range": !Join [ ',' , [!Ref ParamCloudTrailProjectionEventStartDate,'NOW']],
"projection.date_partition.format": "yyyy/MM/dd",
"projection.date_partition.interval": "1",
"projection.date_partition.interval.unit": "DAYS",
"projection.region_partition.type": "enum",
"projection.region_partition.values": !Ref ParamCloudTrailRegionEnum,
"projection.account_partition.type": "enum",
"projection.account_partition.values": !Ref ParamCloudTrailAccountEnum,
"storage.location.template": !Join [ "", [ !Ref ParamCloudTrailSource,"${account_partition}/CloudTrail/${region_partition}/${date_partition}" ] ]
}
PartitionKeys:
- Name: date_partition
Type: string
- Name: region_partition
Type: string
- Name: account_partition
Type: string
StorageDescriptor:
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Columns:
- Name: eventversion
Type: string
- Name: useridentity
Type: struct<type:string,principalid:string,arn:string,accountid:string,invokedby:string,accesskeyid:string,userName:string,sessioncontext:struct<attributes:struct<mfaauthenticated:string,creationdate:string>,sessionissuer:struct<type:string,principalId:string,arn:string,accountId:string,userName:string>>>
- Name: eventtime
Type: string
- Name: eventsource
Type: string
- Name: eventname
Type: string
- Name: awsregion
Type: string
- Name: sourceipaddress
Type: string
- Name: useragent
Type: string
- Name: errorcode
Type: string
- Name: errormessage
Type: string
- Name: requestparameters
Type: string
- Name: responseelements
Type: string
- Name: additionaleventdata
Type: string
- Name: requestid
Type: string
- Name: eventid
Type: string
- Name: resources
Type: array<struct<ARN:string,accountId:string,type:string>>
- Name: eventtype
Type: string
- Name: apiversion
Type: string
- Name: readonly
Type: string
- Name: recipientaccountid
Type: string
- Name: serviceeventdetails
Type: string
- Name: sharedeventid
Type: string
- Name: vpcendpointid
Type: string
InputFormat: com.amazon.emr.cloudtrail.CloudTrailInputFormat
Location: !Ref ParamCloudTrailSource
SerdeInfo:
Parameters:
serialization.format : "1"
SerializationLibrary: com.amazon.emr.hive.serde.CloudTrailSerde
# Create an AWS Glue table
ResourceTableVPCflow:
# Creating the table waits for the database to be created
DependsOn: ResourceDatabaseASL
Type: AWS::Glue::Table
Condition: ConditionCreateVPCFlowTable
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseName: !Ref ParamDatabaseName
TableInput:
Name: !Ref ParamVPCFlowTableName
Description: Table for VPC flow logs
TableType: EXTERNAL_TABLE
Parameters: {
"classification": "csv",
"EXTERNAL": "true",
"skip.header.line.count": "1",
"projection.enabled": "true",
"projection.date_partition.type": "date",
"projection.date_partition.range": !Join [ ',' , [!Ref ParamVPCFlowProjectionEventStartDate,'NOW']],
"projection.date_partition.format": "yyyy/MM/dd",
"projection.date_partition.interval": "1",
"projection.date_partition.interval.unit": "DAYS",
"projection.region_partition.type": "enum",
"projection.region_partition.values": !Ref ParamVPCFlowRegionEnum,
"projection.account_partition.type": "enum",
"projection.account_partition.values": !Ref ParamVPCFlowAccountEnum,
"storage.location.template": !Join [ "", [!Ref ParamVPCFlowSource,"${account_partition}/vpcflowlogs/${region_partition}/${date_partition}"]]
}
PartitionKeys:
- Name: date_partition
Type: string
- Name: region_partition
Type: string
- Name: account_partition
Type: string
StorageDescriptor:
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Columns:
- Name: version
Type: int
- Name: account
Type: string
- Name: interfaceid
Type: string
- Name: sourceaddress
Type: string
- Name: destinationaddress
Type: string
- Name: sourceport
Type: int
- Name: destinationport
Type: int
- Name: protocol
Type: int
- Name: numpackets
Type: int
- Name: numbytes
Type: bigint
- Name: starttime
Type: int
- Name: endtime
Type: int
- Name: action
Type: string
- Name: logstatus
Type: string
- Name: vpcid
Type: string
- Name: subnetid
Type: string
- Name: instanceid
Type: string
- Name: tcpflags
Type: smallint
- Name: type
Type: string
- Name: pktsrcaddr
Type: string
- Name: pktdstaddr
Type: string
- Name: region
Type: string
- Name: azid
Type: string
- Name: sublocationtype
Type: string
- Name: sublocationid
Type: string
- Name: pkt_src_aws_service
Type: string
- Name: pkt_dst_aws_service
Type: string
- Name: flow_direction
Type: string
- Name: traffic_path
Type: string
InputFormat: org.apache.hadoop.mapred.TextInputFormat
Location: !Ref ParamVPCFlowSource
SerdeInfo:
Parameters:
serialization.format : ""
field.delim : " "
SerializationLibrary: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
# Create an AWS Glue table
ResourceTableDNSResolver:
# Creating the table waits for the database to be created
DependsOn: ResourceDatabaseASL
Type: AWS::Glue::Table
Condition: ConditionCreateDNSResolverTable
Properties:
CatalogId: !Ref AWS::AccountId
DatabaseName: !Ref ParamDatabaseName
TableInput:
Name: !Ref ParamDNSResolverTableName
Description: Table for DNS Resolver logs
TableType: EXTERNAL_TABLE
Parameters: {
"classification": "csv",
"EXTERNAL": "true",
"projection.enabled": "true",
"projection.date_partition.type": "date",
"projection.date_partition.range": !Join [ ',' , [!Ref ParamDNSResolverProjectionEventStartDate,'NOW']],
"projection.date_partition.format": "yyyy/MM/dd",
"projection.date_partition.interval": "1",
"projection.date_partition.interval.unit": "DAYS",
"projection.vpc_partition.type": "enum",
"projection.vpc_partition.values": !Ref ParamDNSResolverVPCEnum,
"projection.account_partition.type": "enum",
"projection.account_partition.values": !Ref ParamDNSResolverAccountEnum,
"storage.location.template": !Join [ "", [ !Ref ParamDNSResolverSource,"${account_partition}/vpcdnsquerylogs/${vpc_partition}/${date_partition}" ] ]
}
PartitionKeys:
- Name: date_partition
Type: string
- Name: vpc_partition
Type: string
- Name: account_partition
Type: string
StorageDescriptor:
OutputFormat: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Columns:
- Name: version
Type: float
- Name: account_id
Type: string
- Name: region
Type: string
- Name: vpc_id
Type: string
- Name: query_timestamp
Type: string
- Name: query_name
Type: string
- Name: query_type
Type: string
- Name: query_class
Type: string
- Name: rcode
Type: string
- Name: answers
Type: array<string>
- Name: srcaddr
Type: string
- Name: srcport
Type: int
- Name: transport
Type: string
- Name: srcids
Type: string
InputFormat: org.apache.hadoop.mapred.TextInputFormat
Location: !Ref ParamDNSResolverSource
SerdeInfo:
Parameters:
serialization.format : "1"
SerializationLibrary: org.openx.data.jsonserde.JsonSerDe
# Create Named Queries for Demo Queries
# CloudTrail Demo Queries
ResourceDemoQueryCloudTrail1:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_AllPartitions"
Description: "preview first 10 rows with all fields, limited by a combination partition constraints"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- preview first 10 rows with all fields, limited by a combination partition constraints
-- NOTE: narrowing the scope of the query as much as possible will improve performance and minimize cost
SELECT * from cloudtrail
WHERE date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
LIMIT 10;
ResourceDemoQueryCloudTrail2:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_PartitionedByDateRange"
Description: "preview first 10 rows with all fields, limited to a certain date range"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- NOTE: date_partition format is 'YYYY/MM/DD' as a string
-- preview first 10 rows with all fields, limited to a certain date range
SELECT * from cloudtrail
WHERE date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
LIMIT 10;
ResourceDemoQueryCloudTrail3:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_PartitionedByRelativeDate"
Description: "preview first 10 rows with all fields, limited to the past 30 days (relative)"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- preview first 10 rows with all fields, limited to the past 30 days (relative)
SELECT * from cloudtrail
WHERE date_partition >= date_format(date_add('day',-30,current_timestamp), '%Y/%m/%d')
LIMIT 10;
ResourceDemoQueryCloudTrail4:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_EventCountByRegion"
Description: "Summary of event counts by Region (e.g. where is the most activity)"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Summary of event counts by Region (e.g. where is the most activity)
SELECT region_partition, count(*) as eventcount FROM cloudtrail
WHERE date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY region_partition
ORDER BY eventcount DESC
ResourceDemoQueryCloudTrail5:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_EventCountByRegionAndEventname"
Description: "Summary of event count by Region and EventName, ordered by event count (descending) for each region"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Summary of event count by Region and EventName, ordered by event count (descending) for each region
-- Quick way to identify top EventNames seen in each region
SELECT region_partition, eventname, count(*) as eventcount FROM cloudtrail
WHERE date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY region_partition, eventname
ORDER BY region_partition, eventcount DESC
ResourceDemoQueryCloudTrail6:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_UserLoginSummary"
Description: "User login summary, via AssumeRole or ConsoleLogin"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- User login summary, via AssumeRole or ConsoleLogin
-- includes a list of all source IPs for each user
SELECT useridentity.arn, eventname, array_agg(DISTINCT(sourceipaddress) ORDER BY sourceipaddress) AS sourceips FROM cloudtrail
WHERE useridentity.arn IS NOT NULL
AND (eventname = 'AssumeRole' OR eventname = 'ConsoleLogin')
AND date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY useridentity.arn, eventname
ORDER BY eventname
ResourceDemoQueryCloudTrail7:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_FilteredUserActivitySummary"
Description: "Filtered User Activity Summary"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- User Activity Summary
-- filter high volume read-only GET/LIST/DECRIBE calls
SELECT useridentity.arn, array_agg(DISTINCT(eventname)) AS eventnames,
array_agg(DISTINCT(sourceipaddress) ORDER BY sourceipaddress) AS sourceips,
array_agg(DISTINCT(useragent) ORDER BY useragent) AS useragents FROM cloudtrail
WHERE eventname <> 'AssumeRole'
AND eventname NOT LIKE 'Get%'
AND eventname NOT LIKE 'List%'
AND eventname NOT LIKE 'Describe%'
AND date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY useridentity.arn
ResourceDemoQueryCloudTrail8:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_FilteredUserActivitySummary2"
Description: "Filtered User Activity Summary With Username and PrincipalID"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- User Activity Summary, including username
-- filter high volume read-only GET/LIST/DECRIBE calls
-- same as above, but will include the ARN or the username (for IAM Users) of the principal
SELECT useridentity.arn, useridentity.username,
array_agg(DISTINCT(eventname) ORDER BY eventname) AS eventnames,
array_agg(DISTINCT(sourceipaddress) ORDER BY sourceipaddress) AS sourceips,
array_agg(DISTINCT(useragent) ORDER BY useragent) AS useragents FROM cloudtrail
WHERE eventname <> 'AssumeRole'
AND eventname NOT LIKE 'Get%'
AND eventname NOT LIKE 'List%'
AND eventname NOT LIKE 'Describe%'
AND date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY useridentity.arn, useridentity.principalid, useridentity.username
ResourceDemoQueryCloudTrail9:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_IAMChangeSummary"
Description: "IAM change summary"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- IAM change summary
-- * filter read-only GET/LIST/DESCRIBE
-- * filter unsuccessful calls
SELECT eventtime, useridentity.arn, useridentity.username, eventname, requestparameters
FROM cloudtrail
WHERE eventsource = 'iam.amazonaws.com'
AND eventname NOT LIKE 'Get%'
AND eventname NOT LIKE 'List%'
AND eventname NOT LIKE 'Describe%'
AND errorcode IS NULL
AND date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
ORDER BY account_partition, eventtime
ResourceDemoQueryCloudTrail10:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_IAMAccessKeyCreations"
Description: "Access Key creations with extract of username and keyid"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Access Key creations with extract of username and keyid
-- * filter unsuccessful calls
SELECT eventtime, useridentity.arn, useridentity.username, eventname,
JSON_EXTRACT_SCALAR(JSON_EXTRACT(responseelements, '$.accessKey'), '$.userName') AS userName,
JSON_EXTRACT_SCALAR(JSON_EXTRACT(responseelements, '$.accessKey'), '$.accessKeyId') AS accessKey
FROM cloudtrail
WHERE eventname = 'CreateAccessKey'
AND errorcode IS NULL
AND date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
ORDER BY account_partition, eventtime
ResourceDemoQueryCloudTrail11:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_IAMPasswordChanges"
Description: "Password changes with extract of username"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Password changes with extract of username
-- * filter unsuccessful calls
SELECT eventtime, useridentity.arn, useridentity.username, eventname,
JSON_EXTRACT_SCALAR(requestparameters, '$.userName') AS "username with password modified"
FROM cloudtrail
WHERE eventname IN ('UpdateLoginProfile', 'CreateLoginProfile')
AND errorcode IS NULL
AND date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
ORDER BY account_partition, eventtime
ResourceDemoQueryCloudTrail12:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_CTAS_SingleAccountRegion90Days"
Description: "Create optimized ORC columnar format table for a single account and region for the past 90 days"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Create optimized ORC columnar format table for a single account and region for the past 90 days
-- NOTE: single query limit is 100 partitions, to add additional accounts, regions, or days use the following INSERT INTO method
-- Reference: https://docs.aws.amazon.com/athena/latest/ug/ctas-insert-into.html
CREATE TABLE cloudtrail_orc
WITH (format = 'ORC', orc_compression = 'SNAPPY', partitioned_by = ARRAY['account_partition','region_partition','date_partition'] ) AS
SELECT eventversion,
useridentity,
eventtime,
eventsource,
eventname,
awsregion,
sourceipaddress,
useragent,
errorcode,
errormessage,
requestparameters,
responseelements,
additionaleventdata,
requestid,
eventid,
resources,
eventtype,
apiversion,
readonly,
recipientaccountid,
serviceeventdetails,
sharedeventid,
vpcendpointid,
account_partition,
region_partition,
date_partition
FROM cloudtrail
WHERE account_partition = '111122223333'
AND region_partition = 'us-east-1'
AND date_partition >= date_format(date_add('day',-90,current_timestamp), '%Y/%m/%d')
ResourceDemoQueryCloudTrail13:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_INSERT_INTO_SingleAccountRegion90Days"
Description: "Add optimized ORC columnar format table for a single account and region for the past 90 days"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Add optimized ORC columnar format table for a single account and region for the past 90 days
-- NOTE: single query limit is 100 partitions, to add additional accounts, regions, or days keep using this INSERT INTO method
-- Reference: https://docs.aws.amazon.com/athena/latest/ug/ctas-insert-into.html
INSERT INTO cloudtrail_orc
SELECT eventversion,
useridentity,
eventtime,
eventsource,
eventname,
awsregion,
sourceipaddress,
useragent,
errorcode,
errormessage,
requestparameters,
responseelements,
additionaleventdata,
requestid,
eventid,
resources,
eventtype,
apiversion,
readonly,
recipientaccountid,
serviceeventdetails,
sharedeventid,
vpcendpointid,
account_partition,
region_partition,
date_partition
FROM cloudtrail
WHERE account_partition = '111122223333'
AND region_partition = 'us-east-2'
AND date_partition >= date_format(date_add('day',-90,current_timestamp), '%Y/%m/%d')
ResourceDemoQueryCloudTrail14:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_CloudTrail_AllNonRFC1918SourceIPActivity"
Description: "Search for all events originating from a public (non-RCF1918) IP address"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Identify API events made from a public IP (i.e. a non-RFC1918 source IP address)
-- NOTE: this is an example of using the new IPADDRESS data type, as string a string comparison would correctly compare IP addresses
SELECT *
FROM cloudtrail
WHERE regexp_like(sourceipaddress, '^\d')
AND NOT ( (CAST(sourceipaddress AS IPADDRESS) > IPADDRESS '10.0.0.0'
AND CAST(sourceipaddress AS IPADDRESS) < IPADDRESS '10.255.255.255')
OR (CAST(sourceipaddress AS IPADDRESS) > IPADDRESS '172.16.0.0'
AND CAST(sourceipaddress AS IPADDRESS) < IPADDRESS '172.31.255.255')
OR (CAST(sourceipaddress AS IPADDRESS) > IPADDRESS '192.168.0.0'
AND CAST(sourceipaddress AS IPADDRESS) < IPADDRESS '192.168.255.255'))
AND date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
# VPCFlow Demo Queries
ResourceDemoQueryVPCFlow1:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_AllPartitions"
Description: "preview first 10 rows with all fields, limited by a combination partition constraints"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- preview first 10 rows with all fields, limited by a combination partition constraints
-- NOTE: narrowing the scope of the query as much as possible will improve performance and minimize cost
SELECT * from vpcflow
WHERE date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
LIMIT 10;
ResourceDemoQueryVPCFlow2:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_PartitionedByDateRange"
Description: "preview first 10 rows with all fields, limited to a certain date range"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- NOTE: date_partition format is 'YYYY/MM/DD' as a string
-- preview first 10 rows with all fields, limited to a certain date range
SELECT * from vpcflow
WHERE date_partition >= '2021/07/01'
AND date_partition <= '2021/07/31'
LIMIT 10;
ResourceDemoQueryVPCFlow3:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_PartitionedByRelativeDate"
Description: "preview first 10 rows with all fields, limited to the past 30 days (relative)"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- preview first 10 rows with all fields, limited to the past 30 days (relative)
SELECT * from vpcflow
WHERE date_partition >= date_format(date_add('day',-30,current_timestamp), '%Y/%m/%d')
LIMIT 10;
ResourceDemoQueryVPCFlow4:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_ListSrcDstIPPairRecordCounts"
Description: "Get list source/destination IP pairs ordered by the number of records "
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Get list source/destination IP pairs ordered by the number of records
SELECT region, instanceid, sourceaddress, destinationaddress, count(*) as record_count FROM vpcflow
WHERE date_partition >= '2020/07/01'
AND date_partition <= '2020/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY region, instanceid, sourceaddress, destinationaddress
ORDER BY record_count DESC
ResourceDemoQueryVPCFlow5:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_SrcDstIPPairSummary"
Description: "Get a summary of records between a given source/destination IP pair, ordered by the total number of bytes"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Get a summary of records between a given source/destination IP pair, ordered by the total number of bytes
SELECT region, instanceid, sourceaddress, destinationaddress, sum(numbytes) as byte_count FROM vpcflow
WHERE (sourceaddress = '192.0.2.1' OR destinationaddress = '192.0.2.1')
AND (sourceaddress = '203.0.113.2' OR destinationaddress = '203.0.113.2')
AND date_partition >= '2020/07/01'
AND date_partition <= '2020/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY region, instanceid, sourceaddress, destinationaddress
ORDER BY byte_count DESC
ResourceDemoQueryVPCFlow6:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_InstanceSummaryFromPort"
Description: "Get a summary of the number of bytes sent from port 443 limited to a single instance"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Get a summary of the number of bytes sent from port 443 limited to a single instance
-- NOTE: for remote IPs this represents the amount data downloaded from port 443 by the instance,
-- for instance IPs this represents the amount data downloaded by remote hosts from the instance on port 443
SELECT region, instanceid, sourceaddress, sourceport, destinationaddress, sum(numbytes) as byte_count FROM vpcflow
WHERE instanceid = 'i-000000000000000'
AND sourceport = 443
AND date_partition >= '2020/07/01'
AND date_partition <= '2020/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY region, instanceid, sourceaddress, sourceport, destinationaddress
ORDER BY byte_count DESC
ResourceDemoQueryVPCFlow7:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_InstanceSummaryToPort"
Description: "Get a summary of the number of bytes sent to port 443 limited to a single instance"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Get a summary of the number of bytes sent to port 443 limited to a single instance
-- NOTE: for remote IPs this represents the amount data uploaded to port 443 by the instance,
-- for instance IPs this represents the amount data uploaded by remote hosts to the instance on port 443
SELECT region, instanceid, sourceaddress, destinationaddress, destinationport, sum(numbytes) as byte_count FROM vpcflow
WHERE instanceid = 'i-000000000000000'
AND destinationport = 443
AND date_partition >= '2020/07/01'
AND date_partition <= '2020/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY region, instanceid, sourceaddress, destinationaddress, destinationport
ORDER BY byte_count DESC
ResourceDemoQueryVPCFlow8:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_IpConnectionsSummary"
Description: "Get a summary with the number of bytes for each src_ip,src_port,dst_ip,dst_port quad across all records to or from a specific IP"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Get a summary with the number of bytes for each src_ip,src_port,dst_ip,dst_port quad across all records to or from a specific IP
SELECT sourceaddress, destinationaddress, sourceport, destinationport, sum(numbytes) as byte_count FROM vpcflow
WHERE (sourceaddress = '192.0.2.1' OR destinationaddress = '192.0.2.1')
AND date_partition >= '2020/07/01'
AND date_partition <= '2020/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')
GROUP BY sourceaddress, destinationaddress, sourceport, destinationport
ORDER BY byte_count DESC
ResourceDemoQueryVPCFlow9:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_SrcDstIPPairDetailWithDirection"
Description: "Get a summary with the number of bytes for each src_ip,src_port,dst_ip,dst_port quad across all records to or from a specific IP"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Get all flow records between two IPs showing flow_direction (requires v5 flow-direction field to be enabled)
SELECT from_unixtime(starttime) AS start_time,
from_unixtime(endtime) AS end_time,
interfaceid,
sourceaddress,
destinationaddress,
sourceport,
destinationport,
numpackets,
numbytes,
flow_direction,
action
FROM vpcflow
WHERE (sourceaddress = '192.0.2.1'
AND destinationaddress = '192.0.2.254')
OR (sourceaddress = '192.0.2.254'
AND destinationaddress = '192.0.2.1')
ORDER BY starttime ASC
ResourceDemoQueryVPCFlow10:
Type: AWS::Athena::NamedQuery
Properties:
Name: "DEMO_VPCFlow_AllTrafficBetweenPublicAndPrivateIPs"
Description: "Search for traffic between a private (RFC1918) IP address and a public (non-RFC1918) IP address (exclude internal-to-internal traffic)"
Database: !Ref ParamDatabaseName
WorkGroup: !Ref ResourceAthenaWorkgroupDefault
QueryString: |
-- Search for traffic between a private (RFC1918) IP address and a public (non-RFC1918) IP address
-- NOTE: this is an example of using the new IPADDRESS data type, as string a string comparison would correctly compare IP addresses
SELECT *
FROM vpcflow
WHERE (sourceaddress <> '-' AND destinationaddress <> '-')
AND ( (
NOT ( (CAST(sourceaddress AS IPADDRESS) > IPADDRESS '10.0.0.0'
AND CAST(sourceaddress AS IPADDRESS) < IPADDRESS '10.255.255.255')
OR (CAST(sourceaddress AS IPADDRESS) > IPADDRESS '172.16.0.0'
AND CAST(sourceaddress AS IPADDRESS) < IPADDRESS '172.31.255.255')
OR (CAST(sourceaddress AS IPADDRESS) > IPADDRESS '192.168.0.0'
AND CAST(sourceaddress AS IPADDRESS) < IPADDRESS '192.168.255.255'))
AND ( (CAST(destinationaddress AS IPADDRESS) > IPADDRESS '10.0.0.0'
AND CAST(destinationaddress AS IPADDRESS) < IPADDRESS '10.255.255.255')
OR (CAST(destinationaddress AS IPADDRESS) > IPADDRESS '172.16.0.0'
AND CAST(destinationaddress AS IPADDRESS) < IPADDRESS '172.31.255.255')
OR (CAST(destinationaddress AS IPADDRESS) > IPADDRESS '192.168.0.0'
AND CAST(destinationaddress AS IPADDRESS) < IPADDRESS '192.168.255.255'))
)
OR (
NOT ( (CAST(destinationaddress AS IPADDRESS) > IPADDRESS '10.0.0.0'
AND CAST(destinationaddress AS IPADDRESS) < IPADDRESS '10.255.255.255')
OR (CAST(destinationaddress AS IPADDRESS) > IPADDRESS '172.16.0.0'
AND CAST(destinationaddress AS IPADDRESS) < IPADDRESS '172.31.255.255')
OR (CAST(destinationaddress AS IPADDRESS) > IPADDRESS '192.168.0.0'
AND CAST(destinationaddress AS IPADDRESS) < IPADDRESS '192.168.255.255'))
AND ( (CAST(sourceaddress AS IPADDRESS) > IPADDRESS '10.0.0.0'
AND CAST(sourceaddress AS IPADDRESS) < IPADDRESS '10.255.255.255')
OR (CAST(sourceaddress AS IPADDRESS) > IPADDRESS '172.16.0.0'
AND CAST(sourceaddress AS IPADDRESS) < IPADDRESS '172.31.255.255')
OR (CAST(sourceaddress AS IPADDRESS) > IPADDRESS '192.168.0.0'
AND CAST(sourceaddress AS IPADDRESS) < IPADDRESS '192.168.255.255'))
)
)
AND date_partition >= '2020/07/01'
AND date_partition <= '2020/07/31'
AND account_partition = '111122223333'
AND region_partition in ('us-east-1','us-east-2','us-west-2', 'us-west-2')