/
template.yaml
544 lines (532 loc) · 17 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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Easy serverless Amazon Trascribe
Metadata:
AWS::ServerlessRepo::Application:
Name: serverless-transcribe
Description: Easy serverless Amazon Trascribe via the web
Author: Christopher Kalafarski
SpdxLicenseId: MIT
LicenseUrl: LICENSE
ReadmeUrl: README.md
Labels: ['transcribe', 'transcription', 'serverless']
HomePageUrl: https://github.com/farski/serverless-transcribe
SemanticVersion: 2.2.0
SourceCodeUrl: https://github.com/farski/serverless-transcribe
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Authentication
Parameters:
- BasicAuthUsername
- BasicAuthPassword
- Label:
default: Configuration
Parameters:
- NotificationSourceEmailAddress
- TranscriptionsExpirationInDays
- JobTagKey
- JobTagValue
ParameterLabels:
BasicAuthUsername:
default: Username
BasicAuthPassword:
default: Password
NotificationSourceEmailAddress:
default: Email FROM
JobTagKey:
default: Transcribe job tag
JobTagValue:
default: Transcribe job value
TranscriptionsExpirationInDays:
default: Days to keep transcriptions
Parameters:
BasicAuthUsername:
Type: String
Description: The HTTP Basic authentication username for the website
BasicAuthPassword:
Type: String
Description: The HTTP Basic authentication password for the website
NotificationSourceEmailAddress:
Type: String
Description: Emails sent by the applications will come from this address
JobTagKey:
Type: String
Default: ""
Description: >-
(optional) Transcription jobs will be tagged with this key
JobTagValue:
Type: String
Default: ""
Description: >-
(optional) Transcription jobs will be tagged with this value
TranscriptionsExpirationInDays:
Type: Number
Default: 30
Description: >-
The number of days transcription files should remain in S3 after they are
generated
Resources:
# Final transcriptions will be added to this bucket
TranscriptionsBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
LifecycleConfiguration:
Rules:
- ExpirationInDays: !Ref TranscriptionsExpirationInDays
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
Tags:
- Key: Project
Value: serverless-transcribe
# Files uploaded through the website will be added to this bucket
MediaBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
CorsConfiguration:
CorsRules:
- AllowedHeaders: ["*"]
AllowedMethods: [POST]
AllowedOrigins: ["*"]
LifecycleConfiguration:
Rules:
- ExpirationInDays: 1
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
Tags:
- Key: Project
Value: serverless-transcribe
Api:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Ref AWS::StackName
Description: Transcription website API
ApiStage:
Type: AWS::ApiGateway::Stage
Properties:
StageName: transcribe
RestApiId: !Ref Api
DeploymentId: !Ref ApiDeployment
ApiDeployment:
DependsOn:
- WebFrontEndMethod
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref Api
# The authorizer's identity source indicates that an Authorization header
# is required. When the header is missing, the API Gateway will return an
# UNAUTHORIZED response. Once a users has completed the HTTP Basic
# authentication, the header will be present on requests, and the authorizer
# function will be invoked as expected.
# The authorizer response is cached, using the Authorization header as a
# cache key. All authenticated users share a single set of credentials, and
# will have the same Authorization header (when using the correct
# credentials), so the authorizers response is generic to all users. The
# authorizer's response is a policy that grants access to the entire API, not
# specific paths.
ApiAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
AuthorizerCredentials: !GetAtt ApiAuthorizerInvokeRole.Arn
AuthorizerResultTtlInSeconds: 3600
AuthorizerUri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ApiAuthorizerFunction.Arn}/invocations
IdentitySource: method.request.header.Authorization
Name: !Sub ${AWS::StackName}_static_website_authorizer
RestApiId: !Ref Api
Type: REQUEST
ApiAuthorizerFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/api-authorizer/
Description: Authorizer for S3 file upload front end
Environment:
Variables:
BASIC_AUTH_USERNAME: !Ref BasicAuthUsername
BASIC_AUTH_PASSWORD: !Ref BasicAuthPassword
Handler: lambda_function.lambda_handler
MemorySize: 128
Runtime: python3.9
Tags:
Project: serverless-transcribe
Timeout: 3
ApiAuthorizerFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${ApiAuthorizerFunction}
RetentionInDays: 14
Tags:
- Key: Project
Value: serverless-transcribe
ApiAuthorizerInvokeRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: apigateway.amazonaws.com
Version: "2012-10-17"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: AuthorizerLambdaInvoke
PolicyDocument:
Statement:
- Action: lambda:invokeFunction
Effect: Allow
Resource: !GetAtt ApiAuthorizerFunction.Arn
Version: "2012-10-17"
# When the API Gateway returns an UNAUTHORIZED response (due to lack of an
# Authorization header in the request, as required by the authorizer's
# identity source), the gateway response will transform the response to
# include a WWW-Authenticate header, which prompts browsers to display an
# HTTP Basic Auth form.
ApiUnauthorizedGatewayReponse:
Type: AWS::ApiGateway::GatewayResponse
Properties:
ResponseParameters:
gatewayresponse.header.WWW-Authenticate: "'Basic'"
ResponseType: UNAUTHORIZED
RestApiId: !Ref Api
StatusCode: "401"
# Web front-end
WebFrontEndFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/web-front-end/
Description: >-
Generates a basic front end for uploading files to S3
Environment:
Variables:
API_ID: !Ref Api
MEDIA_BUCKET: !Ref MediaBucket
MEDIA_BUCKET_DOMAIN_NAME: !GetAtt MediaBucket.RegionalDomainName
Handler: lambda_function.lambda_handler
MemorySize: 128
Policies:
# HTTP POST requests from the form on the upload webpage will be signed
# using the temporary key, secret, and token belonging to the Lambda's
# execution role, so it needs PutObject access to the S3 bucket where
# files are being uploaded.
- S3WritePolicy: { BucketName: !Ref MediaBucket }
Runtime: python3.9
Tags:
Project: serverless-transcribe
Timeout: 3
WebFrontEndFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${WebFrontEndFunction}
RetentionInDays: 14
Tags:
- Key: Project
Value: serverless-transcribe
WebFrontEndMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: CUSTOM
AuthorizerId: !Ref ApiAuthorizer
HttpMethod: GET
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${WebFrontEndFunction.Arn}/invocations
ResourceId: !GetAtt Api.RootResourceId
RestApiId: !Ref Api
WebFrontEndFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt WebFrontEndFunction.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${Api}/${ApiStage}/GET/
# Transcription Job Start
TranscriptionJobStartFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/transcription-job-start/
Description: >-
Starts transcription jobs when triggered by S3 events
Environment:
Variables:
API_ID: !Ref Api
JOB_TAG_KEY: !Ref JobTagKey
JOB_TAG_VALUE: !Ref JobTagValue
NOTIFICATION_SOURCE_EMAIL_ADDRESS: !Ref NotificationSourceEmailAddress
TRANSCRIPTIONS_OUTPUT_BUCKET: !Ref TranscriptionsBucket
Events:
WavCreated:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .wav
Type: S3
FlacCreated:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .flac
Type: S3
AmrCreated:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .amr
Type: S3
3gaCreated:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .3ga
Type: S3
Mp3Created:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .mp3
Type: S3
Mp4Created:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .mp4
Type: S3
M4aCreated:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .m4a
Type: S3
OgaCreated:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .oga
Type: S3
OggCreated:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .ogg
Type: S3
OpusCreated:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .opus
Type: S3
WebmCreated:
Properties:
Bucket: !Ref MediaBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: .webm
Type: S3
Handler: lambda_function.lambda_handler
MemorySize: 128
Role: !GetAtt TranscriptionJobStartFunctionIamRole.Arn
Runtime: python3.9
Tags:
Project: serverless-transcribe
Timeout: 8
TranscriptionJobStartFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${TranscriptionJobStartFunction}
RetentionInDays: 14
Tags:
- Key: Project
Value: serverless-transcribe
TranscriptionJobStartFunctionIamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Version: "2012-10-17"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyDocument:
Statement:
- Action:
- s3:Get*
- s3:List*
- s3:Put*
Effect: Allow
Resource: !GetAtt TranscriptionsBucket.Arn
Sid: AllowBucketActions
- Action:
- s3:Get*
- s3:List*
- s3:Put*
Effect: Allow
Resource: !Sub ${TranscriptionsBucket.Arn}/*
Sid: AllowObjectActions
Version: "2012-10-17"
PolicyName: TranscriptionsBucket
- PolicyDocument:
Statement:
- Action:
- transcribe:GetTranscriptionJob
- transcribe:StartTranscriptionJob
- transcribe:TagResource
Effect: Allow
Resource: "*"
Sid: AllowTranscribeJobStart
Version: "2012-10-17"
PolicyName: Transcribe
- PolicyDocument:
Statement:
- Action: ses:SendEmail
Effect: Allow
Resource: "*"
Sid: AllowSendEmail
Version: "2012-10-17"
PolicyName: SES
TranscriptionJobStartFunctionIamRoleMediaBucketPolicy:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action: s3:GetObject
Effect: Allow
Resource: !Sub ${MediaBucket.Arn}/*
Sid: AllowMediaObjectRead
- Action: s3:ListBucket
Effect: Allow
Resource: !GetAtt MediaBucket.Arn
Sid: AllowMediaBucketList
Version: "2012-10-17"
PolicyName: MediaBucket
Roles:
- !Ref TranscriptionJobStartFunctionIamRole
# Transcription Job Events
TranscriptionJobStateChangeFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/transcription-job-state-change/
Description: Handles transcription job state change events
Environment:
Variables:
MEDIA_BUCKET_NAME: !Ref MediaBucket
NOTIFICATION_SOURCE_EMAIL_ADDRESS: !Ref NotificationSourceEmailAddress
Events:
TranscribeJobs:
Properties:
Pattern:
source:
- aws.transcribe
detail-type:
- Transcribe Job State Change
detail:
TranscriptionJobStatus:
- COMPLETED
- FAILED
TranscriptionJobName:
- prefix: !Sub ${Api}-
Target:
Id: !Sub ${AWS::StackName}.Transcribe.JobState
Type: EventBridgeRule
Handler: lambda_function.lambda_handler
MemorySize: 256
Policies:
- Statement:
- Action: ses:SendEmail
Effect: Allow
Resource: "*"
Sid: AllowSendEmail
Version: "2012-10-17"
- Statement:
- Action: transcribe:GetTranscriptionJob
Effect: Allow
Resource: "*"
Sid: AllowTranscriptionJobRead
Version: "2012-10-17"
- Statement:
- Action: s3:GetObject
Effect: Allow
Resource: !Sub ${TranscriptionsBucket.Arn}/*
Sid: AllowTranscriptionObjectRead
Version: "2012-10-17"
- Statement:
- Action: s3:GetObject
Effect: Allow
Resource: !Sub ${MediaBucket.Arn}/*
Sid: AllowMediaObjectRead
Version: "2012-10-17"
Runtime: python3.9
Tags:
Project: serverless-transcribe
Timeout: 16
TranscriptionJobStateChangeFunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${TranscriptionJobStateChangeFunction}
RetentionInDays: 14
Tags:
- Key: Project
Value: serverless-transcribe
Outputs:
UploadWebpageUrl:
Description: The URL for the webpage used to upload media
Value: !Sub https://${Api}.execute-api.${AWS::Region}.amazonaws.com/${ApiStage}