This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathtemplate.yaml
212 lines (201 loc) · 6.19 KB
/
template.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Kinesis
Resources:
## Raw data bucket
RawDataBucket:
Type: AWS::S3::Bucket
## Processed Data
ProcessedDataBucket:
Type: AWS::S3::Bucket
## Link count table
CountTable:
Type: AWS::Serverless::SimpleTable
# Raw data tale
ProcessedDataTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
AttributeDefinitions:
- AttributeName: id
AttributeType: S
TimeToLiveSpecification:
AttributeName: ttl
Enabled: true
## Ingest Firehose
Firehose:
Type: AWS::KinesisFirehose::DeliveryStream
Properties:
DeliveryStreamType: DirectPut
ExtendedS3DestinationConfiguration:
BucketARN: !GetAtt ProcessedDataBucket.Arn
CompressionFormat: GZIP
RoleARN: !GetAtt FirehoseAccessRole.Arn
ProcessingConfiguration:
Enabled: true
Processors:
- Type: Lambda
Parameters:
- ParameterName: LambdaArn
ParameterValue: !GetAtt ProcessFunction.Arn
BufferingHints:
IntervalInSeconds: 60
SizeInMBs: 1
S3BackupMode: Enabled
S3BackupConfiguration:
BucketARN: !GetAtt RawDataBucket.Arn
CompressionFormat: GZIP
RoleARN: !GetAtt FirehoseAccessRole.Arn
BufferingHints:
IntervalInSeconds: 60
SizeInMBs: 1
# Initial process function
ProcessFunction:
Type: AWS::Serverless::Function
Properties:
Timeout: 180
CodeUri: src/
Handler: process.handler
Runtime: nodejs16.x
Policies:
- DynamoDBCrudPolicy: {TableName: !Ref ProcessedDataTable}
Environment:
Variables:
TABLE_NAME: !Ref ProcessedDataTable
# Link count function
CountFunction:
Type: AWS::Serverless::Function
Properties:
Timeout: 180
CodeUri: src/
Handler: count.handler
Runtime: nodejs16.x
Policies:
- DynamoDBCrudPolicy: {TableName: !Ref CountTable}
Environment:
Variables:
TABLE_NAME: !Ref CountTable
# Kinesis Data Analytics Application
KinesisAnalyticsApp:
Type: AWS::KinesisAnalytics::Application
Properties:
ApplicationCode: >
CREATE OR REPLACE STREAM "LINK_STREAM" ("resourcePath" varchar(16), link_count INTEGER);
CREATE OR REPLACE PUMP "STREAM_PUMP" AS INSERT INTO "LINK_STREAM"
SELECT STREAM "resourcePath", COUNT(*) AS link_count
FROM "SESSIONS_STREAM_001"
GROUP BY "resourcePath", STEP("SESSIONS_STREAM_001".ROWTIME BY INTERVAL '10' SECOND);
Inputs:
- InputSchema:
RecordColumns:
- Name: requestId
Mapping: $.requestId
SqlType: bigint
- Name: ip
Mapping: $.ip
SqlType: varchar(16)
- Name: status
Mapping: $.status
SqlType: varchar(8)
- Name: resourcePath
Mapping: $.resourcePath
SqlType: varchar(16)
RecordFormat:
RecordFormatType: JSON
KinesisFirehoseInput:
ResourceARN: !GetAtt Firehose.Arn
RoleARN: !GetAtt KinesisAnalyticsAccessRole.Arn
NamePrefix: SESSIONS_STREAM
# Output for Kinesis Data Analytics application
KinesisAnalyticsOutput:
Type: AWS::KinesisAnalytics::ApplicationOutput
Properties:
ApplicationName: !Ref KinesisAnalyticsApp
Output:
DestinationSchema:
RecordFormatType: JSON
LambdaOutput:
ResourceARN: !GetAtt CountFunction.Arn
RoleARN: !GetAtt KinesisAnalyticsAccessRole.Arn
Name: LINK_STREAM
# Access role for Firehose
FirehoseAccessRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "firehose.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: S3WritePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
Action:
- s3:PutObject
Effect: Allow
Resource:
- !GetAtt RawDataBucket.Arn
- !Sub
- ${Arn}/*
- { Arn: !GetAtt RawDataBucket.Arn }
- !GetAtt ProcessedDataBucket.Arn
- !Sub
- ${Arn}/*
- { Arn: !GetAtt ProcessedDataBucket.Arn }
- PolicyName: LambdaInvokePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
Action:
- lambda:InvokeFunction
Effect: Allow
Resource:
- !GetAtt ProcessFunction.Arn
# Access role for Kinesis Data Analytics
KinesisAnalyticsAccessRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "kinesisanalytics.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: KinesisAccessPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
Action:
- firehose:DescribeDeliveryStream
- firehose:Get*
- kinesis:Describe*
- kinesis:Get*
- kinesis:List*
- kinesis:Put*
Effect: Allow
Resource:
- !GetAtt Firehose.Arn
- PolicyName: LambdaAccessPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
Action:
- lambda:InvokeFunction
- lambda:Get*
Effect: Allow
Resource:
- !GetAtt CountFunction.Arn
- !Sub
- ${Func}:$LATEST
- { Func: !GetAtt CountFunction.Arn }