-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.yaml
231 lines (222 loc) · 8.2 KB
/
setup.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
AWSTemplateFormatVersion: 2010-09-09
Description: Voice Crawler Template
Parameters:
RawAudioBucketName:
Type: String
Description: S3 Bucket name for raw audio files
Default: raw-audio-voice-crawler
TranscribedAudioBucketName:
Type: String
Description: S3 Bucket name for transcribed audio files
Default: transcribed-audio-voice-crawler
LogRetentionInDays:
Type: String
Description: Number of log retention days
Default: 1
AudioLanguageCode:
Type: String
Description: Language code of translated audio files
Default: en-US
Resources:
RawAudioS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref RawAudioBucketName
NotificationConfiguration:
LambdaConfigurations:
- Event: s3:ObjectCreated:*
Function: !GetAtt TranscribeAudioLambdaFunction.Arn
Filter:
S3Key:
Rules:
- Name: suffix
Value: .mp3
S3InvokeTranscribeAudioLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt TranscribeAudioLambdaFunction.Arn
SourceArn: !Sub "arn:aws:s3:::${RawAudioBucketName}"
SourceAccount: !Ref "AWS::AccountId"
Principal: s3.amazonaws.com
TranscribeAudioLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: TranscribeAudioLambdaFunctionPolicies
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:ListBucket
Resource: !Sub "arn:aws:s3:::${RawAudioBucketName}"
- Effect: Allow
Action:
- s3:GetObject
Resource: !Sub "arn:aws:s3:::${RawAudioBucketName}/*"
- Effect: Allow
Action:
- s3:PutObject
Resource: !Sub "arn:aws:s3:::${TranscribedAudioBucketName}/*"
- Effect: Allow
Action:
- "transcribe:StartTranscriptionJob"
Resource: "*"
TranscribeAudioLambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${TranscribeAudioLambdaFunction}"
RetentionInDays: !Ref LogRetentionInDays
TranscribeAudioLambdaLogPermissions:
Type: AWS::IAM::Policy
Properties:
Roles:
- !Ref TranscribeAudioLambdaRole
PolicyName: !Sub "${AWS::Region}-TranscribeAudioLambdaLogGroup"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${TranscribeAudioLambdaFunction}"
- !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${TranscribeAudioLambdaFunction}:*"
- !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${TranscribeAudioLambdaFunction}:*:*"
TranscribeAudioLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Runtime: python3.9
Timeout: 60
Handler: index.lambda_handler
Role: !GetAtt TranscribeAudioLambdaRole.Arn
Environment:
Variables:
RAW_AUDIO_BUCKET_NAME: !Ref RawAudioBucketName
TRANSCRIBED_AUDIO_BUCKET_NAME: !Ref TranscribedAudioBucketName
AUDIO_LANGUAGE_CODE: !Ref AudioLanguageCode
Code:
ZipFile: |
import os.path
import urllib.parse
import boto3
RAW_AUDIO_BUCKET_NAME = os.environ['RAW_AUDIO_BUCKET_NAME']
TRANSCRIBED_AUDIO_BUCKET_NAME = os.environ['TRANSCRIBED_AUDIO_BUCKET_NAME']
AUDIO_LANGUAGE_CODE = os.environ['AUDIO_LANGUAGE_CODE']
transcribe_client = boto3.client('transcribe')
def transcribe_audio(event):
fileName = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'])
objectUrl = 'https://s3.amazonaws.com/{0}/{1}'.format(RAW_AUDIO_BUCKET_NAME, fileName)
print("Audio to transcribe: " + fileName)
response = transcribe_client.start_transcription_job(
TranscriptionJobName=fileName,
LanguageCode=AUDIO_LANGUAGE_CODE,
MediaFormat='mp3',
Media={
'MediaFileUri': objectUrl
},
OutputKey=fileName.replace(".mp3", ".json"),
OutputBucketName=TRANSCRIBED_AUDIO_BUCKET_NAME
)
print("Successfully transcribed audio to bucket: " + TRANSCRIBED_AUDIO_BUCKET_NAME)
def lambda_handler(event, context):
try:
transcribe_audio(event)
except Exception as e:
print('Exception when transcribing audio')
print(e)
TranscribedAudioS3Bucket:
Type: AWS::S3::Bucket
DependsOn: RawAudioS3Bucket
Properties:
BucketName: !Ref TranscribedAudioBucketName
S3BucketCleanerLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: S3BucketCleanerLambdaFunctionPolicies
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:ListBucket
Resource: !Sub "arn:aws:s3:::${RawAudioBucketName}"
- Effect: Allow
Action:
- s3:DeleteObject
Resource: !Sub "arn:aws:s3:::${RawAudioBucketName}/*"
- Effect: Allow
Action:
- s3:ListBucket
Resource: !Sub "arn:aws:s3:::${TranscribedAudioBucketName}"
- Effect: Allow
Action:
- s3:DeleteObject
Resource: !Sub "arn:aws:s3:::${TranscribedAudioBucketName}/*"
BucketCleanerLambdaInvoke:
Type: AWS::CloudFormation::CustomResource
Version: "1.0"
Properties:
ServiceToken: !GetAtt S3BucketCleanerLambdaFunction.Arn
S3BucketCleanerLambdaFunction:
Type: AWS::Lambda::Function
DependsOn:
- RawAudioS3Bucket
- TranscribedAudioS3Bucket
Properties:
Runtime: python3.9
Timeout: 60
Handler: index.lambda_handler
Role: !GetAtt S3BucketCleanerLambdaRole.Arn
Environment:
Variables:
BUCKETS_TO_CLEAN:
!Join [
",",
[!Ref RawAudioBucketName, !Ref TranscribedAudioBucketName],
]
Code:
ZipFile: |
import os.path
import boto3
import cfnresponse
BUCKETS_TO_CLEAN = os.environ['BUCKETS_TO_CLEAN'].split(",")
s3 = boto3.resource('s3')
def clear_bucket(bucket):
s3.Bucket(bucket).objects.all().delete()
print("Successfully cleared bucket: " + bucket)
def lambda_handler(event, context):
responseData = {}
if event['RequestType'] == 'Delete':
for bucket in BUCKETS_TO_CLEAN:
try:
clear_bucket(bucket)
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
except Exception as e:
print('Exception when cleaning bucket: ' + bucket)
print(e)
cfnresponse.send(event, context, cfnresponse.FAILED, responseData)
else:
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)