-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yml
270 lines (260 loc) · 8.98 KB
/
template.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
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Parameters:
Environment:
Type: String
Shard:
Type: Number
Default: 1
Description: The amount of shards contained in a stream
DataSource:
Type: String
Description: The custom data source name. It's recommended to use the original DynamoDB table's name.
DataSourceArn:
Type: String
Description: ARN of the DynamoDB stream. It can be found in the "Exports and streams" tab.
Resources:
# =========================================================================================
# IAM ROLE, POLICY
# =========================================================================================
IamRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub '${Environment}-${AWS::StackName}-role'
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- firehose.amazonaws.com
- kinesis.amazonaws.com
- logs.amazonaws.com
- s3.amazonaws.com
- glue.amazonaws.com
- dynamodb.amazonaws.com
Action:
- sts:AssumeRole
Path: '/'
IamPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyName: !Sub '${Environment}-${AWS::StackName}-policy'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: DeliveryStreamAccess
Effect: Allow
Action:
- firehose:CreateDeliveryStream
- firehose:DeleteDeliveryStream
- firehose:DescribeDeliveryStream
- firehose:PutRecord
- firehose:PutRecordBatch
- firehose:UpdateDestination
- firehose:ListDeliveryStreams
Resource: !Join
- ''
- - 'arn:aws:firehose:*:*:deliverystream/'
- !Ref DeliveryStream
- Sid: S3Access
Effect: Allow
Action:
- s3:PutObject
- s3:AbortMultipartUpload
- s3:GetBucketLocation
- s3:GetObject
- s3:ListBucket
- s3:ListBucketMultipartUploads
Resource: !GetAtt S3Bucket.Arn
- Sid: LogsAccess
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:DeleteLogGroup
- logs:CreateLogStream
- logs:DeleteLogStream
- logs:PutLogEvents
- logs:DeleteRetentionPolicy
Resource: !Join
- ''
- - 'arn:aws:logs:*:log-group:'
- !Ref LogGroup
- ':*'
- Sid: LambdaAccess
Effect: Allow
Action:
- lambda:InvokeFunction
- lambda:GetFunctionConfiguration
Resource: arn:aws:lambda:*:*:*:*:*
- Sid: GlueAccess
Effect: Allow
Action:
- glue:CreateTable
- glue:CreateDatabase
- glue:GetDatabase
- glue:GetPartition
- glue:GetTable
- glue:UpdateTable
- glue:UpdatePartition
- glue:BatchGetPartition
- glue:BatchCreatePartition
Resource: arn:aws:glue:*:*:*
- Sid: DynamoDBAccess
Effect: Allow
Action:
- dynamodb:GetRecords
- dynamodb:GetShardIterator
- dynamodb:DescribeStream
- dynamodb:ListShards
- dynamodb:ListStreams
Resource: arn:aws:dynamodb:us-east-1:*:table/*/stream/*
Roles:
- !Ref IamRole
# =========================================================================================
# Logs
# =========================================================================================
# log group
LogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub '${Environment}-${AWS::StackName}-Log-Group'
# log stream
LogStream:
Type: AWS::Logs::LogStream
Properties:
LogGroupName: !Ref LogGroup
# =========================================================================================
# S3 Bucket
# =========================================================================================
S3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub '${Environment}-${AWS::StackName}-crawler-target'
AccessControl: 'BucketOwnerFullControl'
# =========================================================================================
# Glue Crawlers
# =========================================================================================
GlueCrawler:
Type: AWS::Glue::Crawler
Properties:
Configuration: '{"Version": 1.0, "Grouping": {"TableGroupingPolicy": "CombineCompatibleSchemas" }, "CrawlerOutput": {"Partitions": { "AddOrUpdateBehavior": "InheritFromTable" }}}'
DatabaseName: !Sub '${Environment}-${AWS::StackName}-Raw'
Name: !Sub '${Environment}-${AWS::StackName}-Glue-Crawler'
RecrawlPolicy:
RecrawlBehavior: 'CRAWL_NEW_FOLDERS_ONLY'
Role: !Ref IamRole
Schedule:
ScheduleExpression: 'cron(1 * * * ? *)'
SchemaChangePolicy:
DeleteBehavior: 'LOG'
UpdateBehavior: 'LOG'
Targets:
S3Targets:
- Path: !Ref S3Bucket
# =========================================================================================
# Kinesis Delivery Stream
# =========================================================================================
DeliveryStream:
Type: AWS::KinesisFirehose::DeliveryStream
Properties:
DeliveryStreamName: !Sub '${Environment}-${AWS::StackName}-Delivery-Stream'
DeliveryStreamType: 'DirectPut'
ExtendedS3DestinationConfiguration:
BucketARN: !GetAtt S3Bucket.Arn
BufferingHints:
SizeInMBs: 128
IntervalInSeconds: 900
RoleARN: !GetAtt IamRole.Arn
Prefix: !Join
- ''
- - !Ref DataSource
- '/year=!{timestamp:yyyy}/month=!{timestamp:MM}/day=!{timestamp:dd}/hour=!{timestamp:HH}'
ErrorOutputPrefix: !Join
- ''
- - !Ref DataSource
- '-error/year=!{timestamp:yyyy}/month=!{timestamp:MM}/day=!{timestamp:dd}/hour=!{timestamp:HH}/!{firehose:error-output-type}'
CompressionFormat: 'GZIP'
DataFormatConversionConfiguration:
Enabled: false
EncryptionConfiguration:
NoEncryptionConfig: 'NoEncryption'
CloudWatchLoggingOptions:
Enabled: true
LogGroupName: !Ref LogGroup
LogStreamName: !Ref LogStream
# =========================================================================================
# AWS LAMBDA FUNCTION
# =========================================================================================
StreamingFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${Environment}-${AWS::StackName}-Streaming-Function'
Handler: lambda_function.lambda_handler
Runtime: python3.7
CodeUri: lambda/
Description: Deliver new data into S3 whenever a new upsert in DynamoDB tables is detected via Kinesis Firehose
MemorySize: 128
Timeout: 900
Role: !GetAtt IamRole.Arn
Environment:
Variables:
LOGGING_LEVEL: INFO
APP_NAME: !Sub '${Environment}-${AWS::StackName}-Streaming-Function'
APP_ENV: !Ref Environment
STREAM: !Ref DeliveryStream
Events:
DynamoDBTrigger:
Type: DynamoDB
Properties:
Stream: !Ref DataSourceArn
BatchSize: 1000
StartingPosition: TRIM_HORIZON
MaximumRetryAttempts: 1
# =========================================================================================
# AWS CLOUDWATCH ALARM
# =========================================================================================
Topic:
Type: AWS::SNS::Topic
Properties:
FifoTopic: false
Subscription:
- Endpoint: [email protected]
Protocol: email
TopicName: !Sub '${Environment}-${AWS::StackName}-Topic'
DeliveryStreamAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
MetricName: 'WriteProvisionedThroughputExceeded'
Namespace: 'AWS/Kinesis'
Period: 300
Dimensions:
- Name: StreamName
Value: !Ref DeliveryStream
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 300
Statistic: Sum
DatapointsToAlarm: 1
Threshold: 1
AlarmName: !Sub '${Environment}-${AWS::StackName}-Delivery-Stream-Alarm'
ActionsEnabled: true
TreatMissingData: missing
AlarmActions:
- !GetAtt Topic.TopicArn
StreamingFunctionAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
ComparisonOperator: GreaterThanOrEqualToThreshold
Dimensions:
- Name: FunctionName
Value: !Ref StreamingFunction
EvaluationPeriods: 1
MetricName: Errors
Namespace: AWS/Lambda
Period: 300
Statistic: Sum
Threshold: '1'
AlarmActions:
- !GetAtt Topic.TopicArn