-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathStream_firehose_snowflake.yml
438 lines (408 loc) · 12.1 KB
/
Stream_firehose_snowflake.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
AWSTemplateFormatVersion: '2010-09-09'
Description: 'CloudFormation template for Amazon Marketing Stream to Snowflake via Kinesis Data Firehose'
Parameters:
StreamDestinationFirehose:
Type: String
Description: Name of the data stream for receiving data
Default: StreamDestinationFirehoseDefaultName
StreamDatasetId:
Type: String
Description: Please select the stream dataset for which you wish to receive the data
AllowedValues:
- sp-traffic
- sp-conversion
- budget-usage
- sd-traffic
- sd-conversion
- sponsored-ads-campaign-diagnostics-recommendations
- campaigns
- adgroups
- ads
- targets
- sb-traffic
- sb-conversion
- sb-clickstream
- sb-rich-media
- adsp-campaigns
- adsp-campaign-flights
- adsp-adgroups
- adsp-adgroup-targets
- sp-budget-recommendations
StreamRealm:
Type: String
Description: Select the AWS region for your stream destination
AllowedValues:
- NA
- EU
- FE
S3Storage:
Description: Choose a unique S3 bucket name for your stream destination
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: '^[a-zA-Z][-a-zA-Z0-9]*$'
SnowflakeAccountUrl:
Type: String
Description: Your Snowflake account URL
AllowedPattern: ^(https://)?[a-zA-Z0-9-]+\.snowflakecomputing\.com
ConstraintDescription: "Must be a valid Snowflake account URL"
SnowflakeUser:
Type: String
Description: Snowflake username for Firehose connection
SnowflakeDatabase:
Type: String
Description: Snowflake database name
SnowflakeSchema:
Type: String
Description: Snowflake schema name
SnowflakeTable:
Type: String
Description: Snowflake table name for data insertion
SnowflakeRole:
Type: String
Description: Snowflake role for Firehose connection
SnowflakeDataLoadingOption:
Type: String
Description: Data loading option
Default: 'JSON_MAPPING'
AllowedValues:
- 'JSON_MAPPING'
- 'VARIANT_CONTENT_MAPPING'
- 'VARIANT_CONTENT_AND_METADATA_MAPPING'
SnowflakePrivateKey:
Type: String
Description: Private key used to encrypt the data
Default: ''
NoEcho: true
Mappings:
Region:
NA:
Region: us-east-1
EU:
Region: eu-west-1
FE:
Region: us-west-2
NA:
sp-traffic:
Account: 906013806264
sp-conversion:
Account: 802324068763
budget-usage:
Account: 055588217351
sd-traffic:
Account: 370941301809
sd-conversion:
Account: 877712924581
sponsored-ads-campaign-diagnostics-recommendations:
Account: 084590724871
campaigns:
Account: 570159413969
adgroups:
Account: 118846437111
ads:
Account: 305370293182
targets:
Account: 644124924521
sb-traffic:
Account: 709476672186
sb-conversion:
Account: 154357381721
sb-clickstream:
Account: 091028706140
sb-rich-media:
Account: 010312603579
adsp-campaigns:
Account: 153247821255
adsp-campaign-flights:
Account: 700228448367
adsp-adgroups:
Account: 222778752755
adsp-adgroup-targets:
Account: 419834811630
sp-budget-recommendations:
Account: 678715897637
EU:
sp-traffic:
Account: 668473351658
sp-conversion:
Account: 562877083794
budget-usage:
Account: 675750596317
sd-traffic:
Account: 947153514089
sd-conversion:
Account: 664093967423
sponsored-ads-campaign-diagnostics-recommendations:
Account: 059061853903
campaigns:
Account: 834862128520
adgroups:
Account: 130948361130
ads:
Account: 648558082147
targets:
Account: 503759481754
sb-traffic:
Account: 623198756881
sb-conversion:
Account: 195770945541
sb-clickstream:
Account: 219513501272
sb-rich-media:
Account: 662188760626
adsp-campaigns:
Account: 599052634802
adsp-campaign-flights:
Account: 633559263003
adsp-adgroups:
Account: 682324742468
adsp-adgroup-targets:
Account: 764057072099
sp-budget-recommendations:
Account: 158915609581
FE:
sp-traffic:
Account: 074266271188
sp-conversion:
Account: 622939981599
budget-usage:
Account: 100899330244
sd-traffic:
Account: 310605068565
sd-conversion:
Account: 818973306977
sponsored-ads-campaign-diagnostics-recommendations:
Account: 489995134625
campaigns:
Account: 527383333093
adgroups:
Account: 668585072850
ads:
Account: 802070757281
targets:
Account: 248074939493
sb-traffic:
Account: 485899199471
sb-conversion:
Account: 112347756703
sb-clickstream:
Account: 632322331982
sb-rich-media:
Account: 618223300352
adsp-campaigns:
Account: 216875695489
adsp-campaign-flights:
Account: 451213518288
adsp-adgroups:
Account: 360850786875
adsp-adgroup-targets:
Account: 178122609971
sp-budget-recommendations:
Account: 007292432803
Rules:
NA:
RuleCondition:
Fn::Equals: [ !Ref StreamRealm, NA ]
Assertions:
- Assert:
Fn::Equals: [ !Ref AWS::Region, us-east-1 ]
EU:
RuleCondition:
Fn::Equals: [ !Ref StreamRealm, EU ]
Assertions:
- Assert:
Fn::Equals: [ !Ref AWS::Region, eu-west-1 ]
FE:
RuleCondition:
Fn::Equals: [ !Ref StreamRealm, FE ]
Assertions:
- Assert:
Fn::Equals: [ !Ref AWS::Region, us-west-2 ]
Resources:
FirehoseLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join ['-', [!Ref StreamRealm, !Ref StreamDatasetId, !Ref StreamDestinationFirehose, 'snowflakeLog']]
RetentionInDays: 30
LogStream:
Type: AWS::Logs::LogStream
Properties:
LogGroupName: !Ref FirehoseLogGroup
LogStreamName: !Join ['-', [!Ref StreamRealm, !Ref StreamDatasetId, !Ref StreamDestinationFirehose, 'snowflakeLogstream']]
StreamDestinationFirehoseName:
Type: 'AWS::KinesisFirehose::DeliveryStream'
Properties:
DeliveryStreamName: !Sub ${StreamRealm}-${StreamDatasetId}-${StreamDestinationFirehose}
DeliveryStreamType: 'DirectPut'
SnowflakeDestinationConfiguration:
AccountUrl: !Ref SnowflakeAccountUrl
BufferingHints:
IntervalInSeconds: 60
SizeInMBs: 5
CloudWatchLoggingOptions:
Enabled: true
LogGroupName: !Ref FirehoseLogGroup
LogStreamName: !Ref LogStream
Database: !Ref SnowflakeDatabase
DataLoadingOption: !Ref SnowflakeDataLoadingOption
PrivateKey: !Ref SnowflakePrivateKey
SnowflakeRoleConfiguration:
Enabled: true
SnowflakeRole: !Ref SnowflakeRole
ProcessingConfiguration:
Enabled: false
RetryOptions:
DurationInSeconds: 300
RoleARN: !GetAtt FirehoseDeliveryRole.Arn
S3BackupMode: 'AllData'
S3Configuration:
BucketARN: !GetAtt S3Bucket.Arn
BufferingHints:
IntervalInSeconds: 300
SizeInMBs: 5
CompressionFormat: 'GZIP'
Prefix: !Sub '${StreamDestinationFirehose}/backup/!{timestamp:yyyy/MM/dd}/'
ErrorOutputPrefix: !Sub '${StreamDestinationFirehose}/errors/!{firehose:error-output-type}/!{timestamp:yyyy/MM/dd}/'
RoleARN: !GetAtt FirehoseDeliveryRole.Arn
Schema: !Ref SnowflakeSchema
Table: !Ref SnowflakeTable
User: !Ref SnowflakeUser
S3Bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Ref S3Storage
FirehoseDeliveryRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Join ['-', [!Ref StreamRealm, !Ref StreamDatasetId, FirehoseDeliveryRole]]
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
Effect: Allow
Principal:
Service: firehose.amazonaws.com
Action: 'sts:AssumeRole'
FirehoseDeliveryPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: !Join ['-', [!Ref StreamRealm, !Ref StreamDatasetId, FirehoseDeliveryPolicy]]
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 's3:AbortMultipartUpload'
- 's3:GetBucketLocation'
- 's3:GetObject'
- 's3:ListBucket'
- 's3:ListBucketMultipartUploads'
- 's3:PutObject'
Resource: !Join
- ''
- - 'arn:aws:s3:::'
- !Sub '${S3Storage}'
- '/*'
- Effect: Allow
Action: 'logs:PutLogEvents'
Resource:
- !Sub 'arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:${StreamRealm}-${StreamDatasetId}-${StreamDestinationFirehose}-snowflakeLog:log-stream:*'
- Effect: Allow
Action: 'cfn:*'
Resource: '*'
Roles:
- !Ref FirehoseDeliveryRole
FirehoseSubscriptionRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Join ['-', [!Ref StreamRealm, !Ref StreamDatasetId, FirehoseSubscriptionRole]]
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: sns.amazonaws.com
Action: 'sts:AssumeRole'
- Effect: Allow
Principal:
AWS:
- arn:aws:iam::926844853897:role/ReviewerRole
Action: 'sts:AssumeRole'
FirehoseSubscriptionRolePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: !Join ['-', [!Ref StreamRealm, !Ref StreamDatasetId, FirehoseSubscriptionRolePolicy]]
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- firehose:DescribeDeliveryStream
- firehose:ListTagsForDeliveryStream
- firehose:ListDeliveryStreams
- firehose:PutRecord
- firehose:PutRecordBatch
Resource: !GetAtt StreamDestinationFirehoseName.Arn
Roles:
- !Ref FirehoseSubscriptionRole
FirehoseSubscriberRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Join ['-', [!Ref StreamRealm, !Ref StreamDatasetId, FirehoseSubscriberRole]]
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
AWS:
- arn:aws:iam::926844853897:role/SubscriberRole
Action:
- sts:AssumeRole
- sts:TagSession
FirehoseSubscriberRolePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: !Join ['-', [!Ref StreamRealm, !Ref StreamDatasetId, FirehoseSubscriberRolePolicy]]
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- iam:PassRole
Resource: !GetAtt FirehoseSubscriptionRole.Arn
- Effect: Allow
Action:
- sns:Subscribe
- sns:Unsubscribe
Resource: !Join
- ':'
- - "arn:aws:sns"
- !FindInMap
- Region
- !Ref 'StreamRealm'
- Region
- !FindInMap
- !Ref 'StreamRealm'
- !Ref 'StreamDatasetId'
- Account
- "*"
Roles:
- !Ref FirehoseSubscriberRole
Outputs:
StreamDestinationFirehoseName:
Description: Name of the Kinesis Data Firehose Delivery Stream
Value: !Ref StreamDestinationFirehoseName
S3Bucket:
Description: ARN of the S3 bucket used for Firehose backup
Value:
Fn::GetAtt: [S3Bucket, Arn]
FirehoseDeliveryRole:
Description: Name of the IAM role used by Firehose
Value: !Ref FirehoseDeliveryRole
FirehoseSubscriptionRole:
Description: ARN of the IAM role used for SNS subscription
Value:
Fn::GetAtt: [FirehoseSubscriptionRole, Arn]
FirehoseSubscriberRole:
Description: ARN of the IAM role used by Firehose subscriber
Value:
Fn::GetAtt: [FirehoseSubscriberRole, Arn]