-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudformation.yaml
357 lines (336 loc) · 11.3 KB
/
cloudformation.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
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
AWSTemplateFormatVersion: 2010-09-09
Parameters:
# Twitter
ConsumerKey:
Default: '***ConsumerKey***'
Description: Twitter App Consumer Key
Type: String
ConsumerSecret:
Default: '***ConsumerSecret***'
Description: Twitter App Consumer Secret
Type: String
AccessToken:
Default: '***AccessToken***'
Description: Twitter App Access Token
Type: String
AccessTokenSecret:
Default: '***AccessTokenSecret***'
Description: Twitter App Access Token Secret
Type: String
# deploy package location
UploadedPackageBucketName:
Default: netebakari
AllowedPattern: '^[a-z0-9\-\.]+$'
Description: Your S3 Bucket Name
Type: String
UploadedPackageKeyName:
Default: twitter-timeline-tracker/timeline-tracker-latest.zip
Description: Path to uploaded deploy package
Type: String
# config
TTLinDays:
Default: 3
Description: DynamoDB TTL in days
Type: Number
DaysToArchive:
Default: 4
Description: Number of days to archive tweets
Type: Number
IncludeFollowers:
Default: false
AllowedPattern: "true|false"
Description: "'true' or 'false'. If true, all the tweets of your followers fetched and archived"
Type: String
utcOffsetInHours:
Default: 9
Description: timezone
Type: Number
TwitterUserId:
Default: "12345"
AllowedPattern: "^[0-9]+$"
Description: Your Twitter Id *not screen name*
Type: String
# AWS Resources
S3BucketName:
Default: "your-bucket-name-for-tweet-log"
AllowedPattern: '^[a-z0-9\-\.]+$'
Description: S3 Bucket Name (MUST already exits)
Type: String
DynamoDbTableName:
Default: TwitterTLTracker-Timelines
Description: DynamoDB Table Name
Type: String
QueueName:
Default: TwitterTLTracker-ScheduleQueue
Description: SQS Queue Name
Type: String
RoleName:
Default: TwitterTLTracker-Lambda
Description: IAM Role Name (only for Lambda)
Type: String
Resources:
UserIdQueue:
Type: 'AWS::SQS::Queue'
Properties:
VisibilityTimeout: 300
QueueName: !Sub '${QueueName}'
TimelineDynamoDB:
Type: 'AWS::DynamoDB::Table'
Properties:
TableName: !Sub '${DynamoDbTableName}'
AttributeDefinitions:
- AttributeName: id_str
AttributeType: S
KeySchema:
- AttributeName: id_str
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TimeToLiveSpecification:
AttributeName: TTL
Enabled: true
IAMRoleForLambda:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Sub ${RoleName}
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
IamPolicyLambda1:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: CloudWatch-Logs
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: "*"
Roles:
- !Ref IAMRoleForLambda
IamPolicyLambda2:
Type: 'AWS::IAM::Policy'
Properties:
PolicyName: SQS-DynamoDB-S3-Lambda
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'lambda:InvokeFunction'
Resource: !GetAtt LambdaTLMerger.Arn
- Effect: Allow
Action:
- 'sqs:DeleteMessage'
- 'sqs:ReceiveMessage'
- 'sqs:SendMessage'
- 'sqs:GetQueueAttributes'
Resource: !GetAtt UserIdQueue.Arn
- Effect: Allow
Action:
- "dynamodb:PutItem"
- "dynamodb:Scan"
- "dynamodb:Query"
- "dynamodb:UpdateItem"
Resource: !GetAtt TimelineDynamoDB.Arn
- Effect: Allow
Action:
- "s3:PutObject"
- "s3:ListBucket"
- "s3:GetObject"
Resource:
- !Sub arn:aws:s3:::${S3BucketName}/*
- !Sub arn:aws:s3:::${S3BucketName}
Roles:
- !Ref IAMRoleForLambda
LambdaHomeTL:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: Twitter-TL-Tracker-HomeTimeline
Description: '[Twitter-TL-Tracker] Home TL'
Environment:
Variables:
twitter_consumer_key: !Sub ${ConsumerKey}
twitter_consumer_secret: !Sub ${ConsumerSecret}
twitter_access_token: !Sub ${AccessToken}
twitter_access_token_secret: !Sub ${AccessTokenSecret}
dynamoDb_region: !Sub ${AWS::Region}
dynamoDb_tableName: !Sub ${DynamoDbTableName}
dynamoDb_ttlInDays: !Sub ${TTLinDays}
sqs_region: !Sub ${AWS::Region}
sqs_queueUrl: !Sub https://sqs.${AWS::Region}.amazonaws.com/${AWS::AccountId}/${QueueName}
s3_region: !Sub ${AWS::Region}
s3_bucket: !Sub ${S3BucketName}
options_daysToArchive: !Sub ${DaysToArchive}
options_includeFollowers: !Sub ${IncludeFollowers}
options_utcOffset: !Sub ${utcOffsetInHours}
options_myUserIdStr: !Sub ${TwitterUserId}
Handler: "index.homeTimeline"
MemorySize: 256
Role: !GetAtt IAMRoleForLambda.Arn
Runtime: nodejs12.x
Timeout: 55
Code:
S3Bucket: !Sub ${UploadedPackageBucketName}
S3Key: !Sub ${UploadedPackageKeyName}
LambdaQueueFiller:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: Twitter-TL-Tracker-QueueFiller
Description: '[Twitter-TL-Tracker] Scheduler'
Environment:
Variables:
twitter_consumer_key: !Sub ${ConsumerKey}
twitter_consumer_secret: !Sub ${ConsumerSecret}
twitter_access_token: !Sub ${AccessToken}
twitter_access_token_secret: !Sub ${AccessTokenSecret}
dynamoDb_region: !Sub ${AWS::Region}
dynamoDb_tableName: !Sub ${DynamoDbTableName}
dynamoDb_ttlInDays: !Sub ${TTLinDays}
sqs_region: !Sub ${AWS::Region}
sqs_queueUrl: !Sub https://sqs.${AWS::Region}.amazonaws.com/${AWS::AccountId}/${QueueName}
s3_region: !Sub ${AWS::Region}
s3_bucket: !Sub ${S3BucketName}
options_daysToArchive: !Sub ${DaysToArchive}
options_includeFollowers: !Sub ${IncludeFollowers}
options_utcOffset: !Sub ${utcOffsetInHours}
options_myUserIdStr: !Sub ${TwitterUserId}
Handler: "index.hourlyTask"
MemorySize: 128
Role: !GetAtt IAMRoleForLambda.Arn
Runtime: nodejs12.x
Timeout: 240
Code:
S3Bucket: !Sub ${UploadedPackageBucketName}
S3Key: !Sub ${UploadedPackageKeyName}
LambdaUserTL:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: Twitter-TL-Tracker-userTL
Description: '[Twitter-TL-Tracker] User TL'
Environment:
Variables:
twitter_consumer_key: !Sub ${ConsumerKey}
twitter_consumer_secret: !Sub ${ConsumerSecret}
twitter_access_token: !Sub ${AccessToken}
twitter_access_token_secret: !Sub ${AccessTokenSecret}
dynamoDb_region: !Sub ${AWS::Region}
dynamoDb_tableName: !Sub ${DynamoDbTableName}
dynamoDb_ttlInDays: !Sub ${TTLinDays}
sqs_region: !Sub ${AWS::Region}
sqs_queueUrl: !Sub https://sqs.${AWS::Region}.amazonaws.com/${AWS::AccountId}/${QueueName}
s3_region: !Sub ${AWS::Region}
s3_bucket: !Sub ${S3BucketName}
options_daysToArchive: !Sub ${DaysToArchive}
options_includeFollowers: !Sub ${IncludeFollowers}
options_utcOffset: !Sub ${utcOffsetInHours}
options_myUserIdStr: !Sub ${TwitterUserId}
Handler: "index.userTL"
MemorySize: 320
Role: !GetAtt IAMRoleForLambda.Arn
Runtime: nodejs12.x
Timeout: 240
Code:
S3Bucket: !Sub ${UploadedPackageBucketName}
S3Key: !Sub ${UploadedPackageKeyName}
LambdaTLMerger:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: Twitter-TL-Tracker-merger
Description: '[Twitter-TL-Tracker] Merge TL'
Environment:
Variables:
twitter_consumer_key: !Sub ${ConsumerKey}
twitter_consumer_secret: !Sub ${ConsumerSecret}
twitter_access_token: !Sub ${AccessToken}
twitter_access_token_secret: !Sub ${AccessTokenSecret}
dynamoDb_region: !Sub ${AWS::Region}
dynamoDb_tableName: !Sub ${DynamoDbTableName}
dynamoDb_ttlInDays: !Sub ${TTLinDays}
sqs_region: !Sub ${AWS::Region}
sqs_queueUrl: !Sub https://sqs.${AWS::Region}.amazonaws.com/${AWS::AccountId}/${QueueName}
s3_region: !Sub ${AWS::Region}
s3_bucket: !Sub ${S3BucketName}
options_daysToArchive: !Sub ${DaysToArchive}
options_includeFollowers: !Sub ${IncludeFollowers}
options_utcOffset: !Sub ${utcOffsetInHours}
options_myUserIdStr: !Sub ${TwitterUserId}
Handler: "index.archive"
MemorySize: 1024
Role: !GetAtt IAMRoleForLambda.Arn
Runtime: nodejs12.x
Timeout: 300
Code:
S3Bucket: !Sub ${UploadedPackageBucketName}
S3Key: !Sub ${UploadedPackageKeyName}
LamdaUserTLTrigger:
Type: "AWS::Events::Rule"
Properties:
Description: "every 2 minutes"
Name: LamdaUserTLTrigger
ScheduleExpression: "cron(*/2 * * * ? *)"
State: ENABLED
Targets:
- Arn: !GetAtt LambdaUserTL.Arn
Id: LambdaUserTL-every2minutes
- Arn: !GetAtt LambdaHomeTL.Arn
Id: LambdaHomeTL-every2minutes
LambdaInvokePermission1:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref LambdaUserTL
Principal: events.amazonaws.com
SourceArn: !GetAtt LamdaUserTLTrigger.Arn
LambdaInvokePermission2:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref LambdaHomeTL
Principal: events.amazonaws.com
SourceArn: !GetAtt LamdaUserTLTrigger.Arn
LamdaUserQueueTrigger:
Type: "AWS::Events::Rule"
Properties:
Description: "every 30 minutes"
Name: LamdaUserQueueTrigger
ScheduleExpression: "cron(*/30 * * * ? *)"
State: ENABLED
Targets:
- Arn: !GetAtt LambdaQueueFiller.Arn
Id: LambdaQueueFiller-every2minutes
LambdaInvokePermission3:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref LambdaQueueFiller
Principal: events.amazonaws.com
SourceArn: !GetAtt LamdaUserQueueTrigger.Arn
LamdaTLMergerTrigger:
Type: "AWS::Events::Rule"
Properties:
Description: "every midnight"
Name: LamdaTLMergerTrigger
ScheduleExpression: "cron(5 15 * * ? *)"
State: ENABLED
Targets:
- Arn: !GetAtt LambdaTLMerger.Arn
Id: LambdaTLMerger-everyMidnight
LambdaInvokePermission4:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref LambdaTLMerger
Principal: events.amazonaws.com
SourceArn: !GetAtt LamdaTLMergerTrigger.Arn