-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathserverless.yml
271 lines (250 loc) · 8.22 KB
/
serverless.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
service: sls-analytics
plugins:
- serverless-dynamodb-autoscaling
- serverless-plugin-typescript
- serverless-stack-output
provider:
name: aws
runtime: nodejs6.10
memorySize: 128
timeout: 120
region: us-east-1
iamRoleStatements:
- Effect: Allow
Action: [ dynamodb:UpdateItem, dynamodb:Scan ]
Resource: { Fn::GetAtt: [ DynamoDBTable, Arn ] }
environment:
DYNAMODB_TABLE_NAME: ${self:custom.names.dynamodb}
custom:
stage: ${opt:stage, self:provider.stage}
website: yfFbTv1GslRcIkUsWpa7 # Random ID
names:
bucket:
website: ${self:service}-website
dashboard: ${self:service}-dashboard
resource: track
dynamodb: ${self:service}-data
kinesis: ${self:service}-stream
output:
file: .serverless/output.json
capacities:
- table: DynamoDBTable
read:
minimum: 10
maximum: 120
write:
minimum: 10
maximum: 120
package:
exclude:
- .gitignore
- CODE_OF_CONDUCT.md
- dist/**
- infra.png
- LICENSE.md
- node_modules/**
- package.json
- README.md
- scripts/**
- sites/**
- yarn.lock
functions:
series:
handler: src/api/series.get
events:
- http:
path: /series
method: GET
ranking:
handler: src/api/ranking.get
events:
- http:
path: /ranking
method: GET
process:
handler: src/process.run
events:
- stream:
type: kinesis
arn: { Fn::GetAtt: [ KinesisStream, Arn ] }
batchSize: 100
startingPosition: TRIM_HORIZON
resources:
Outputs:
TrackingRootURL:
Description: Root URL of the tracking API Gateway
Value: { Fn::Join: [ '', [ https://, { Ref: HTTPRestApi }, .execute-api., { Ref: AWS::Region }, .amazonaws.com ] ] }
TrackingStageName:
Description: Name of the tracking API Gateway Stage
Value: ${self:custom.stage}
TrackingResource:
Description: Name of the tracking API Gateway Resource
Value: ${self:custom.names.resource}
BucketNameWebsite:
Description: Name of the example website S3 Bucket
Value: ${self:custom.names.bucket.website}
BucketNameDashboard:
Description: Name of the dashboard S3 Bucket
Value: ${self:custom.names.bucket.dashboard}
APIRootURL:
Description: Root URL of the data API Gateway
Value: { Fn::Join: [ '', [ https://, { Ref: ApiGatewayRestApi }, .execute-api., { Ref: AWS::Region }, .amazonaws.com ] ] }
APIStageName:
Description: Name of the data API Gateway Stage
Value: ${self:custom.stage}
Region:
Description: AWS Region
Value: { Ref: 'AWS::Region' }
WebsiteID:
Description: Website ID
Value: ${self:custom.website}
Resources:
KinesisStream:
Type: AWS::Kinesis::Stream
Properties:
Name: ${self:custom.names.kinesis}
ShardCount: 1
DynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.names.dynamodb}
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: date
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
- AttributeName: date
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
BucketDashboard:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.names.bucket.dashboard}
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
BucketDashboardPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: { Ref: BucketDashboard }
PolicyDocument:
Statement:
- Effect: Allow
Principal: '*'
Action: s3:GetObject
Resource: arn:aws:s3:::${self:custom.names.bucket.dashboard}/*
BucketWebsite:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.names.bucket.website}
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: index.html
BucketWebsitePolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: { Ref: BucketWebsite }
PolicyDocument:
Statement:
- Effect: Allow
Principal: '*'
Action: s3:GetObject
Resource: arn:aws:s3:::${self:custom.names.bucket.website}/*
HTTPResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: { Ref: HTTPRestApi }
ParentId: { Fn::GetAtt: [ HTTPRestApi, RootResourceId ] }
PathPart: ${self:custom.names.resource}
HTTPRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: ${self:custom.stage}-${self:service}/${self:custom.names.resource}
Description: Tracking HTTP endpoint to put data to Kinesis
FailOnWarnings: true
HTTPAccount:
Type: AWS::ApiGateway::Account
HTTPDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: [ HTTPMethod, HTTPMethodOptions ]
Properties:
RestApiId: { Ref: HTTPRestApi }
StageName: ${self:custom.stage}
HTTPMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: POST
Integration:
Type: AWS
IntegrationHttpMethod: POST
Credentials: { Fn::GetAtt: [ IAMRole, Arn ] }
Uri: { Fn::Join: [ ':', [ arn:aws:apigateway, { Ref: AWS::Region }, kinesis:action/PutRecord ] ] }
RequestTemplates:
application/json: "{\"StreamName\": \"${self:custom.names.kinesis}\",\"PartitionKey\": \"one\",\"Data\": \"$util.base64Encode($input.json('$'))\"}"
PassthroughBehavior: WHEN_NO_MATCH
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'"
method.response.header.Access-Control-Allow-Methods: "'GET,POST,OPTIONS'"
method.response.header.Access-Control-Allow-Origin: "'*'"
ResourceId: { Ref: HTTPResource }
RestApiId: { Ref: HTTPRestApi }
MethodResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: true
method.response.header.Access-Control-Allow-Methods: true
method.response.header.Access-Control-Allow-Origin: true
HTTPMethodOptions:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: OPTIONS
Integration:
Type: MOCK
IntegrationResponses:
- ResponseParameters:
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Methods: "'GET,POST,OPTIONS'"
method.response.header.Access-Control-Allow-Origin: "'*'"
ResponseTemplates:
application/json: ''
StatusCode: 200
PassthroughBehavior: NEVER
RequestTemplates:
application/json: '{"statusCode": 200}'
ResourceId: { Ref: HTTPResource }
RestApiId: { Ref: HTTPRestApi }
MethodResponses:
- ResponseModels:
application/json: Empty
ResponseParameters:
method.response.header.Access-Control-Allow-Headers: true
method.response.header.Access-Control-Allow-Methods: true
method.response.header.Access-Control-Allow-Origin: true
StatusCode: 200
IAMRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal: { Service: [ apigateway.amazonaws.com ] }
Action: [ sts:AssumeRole ]
Path: /
Policies:
- PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: kinesis:PutRecord
Resource: { Fn::GetAtt: [ KinesisStream, Arn ] }