-
Notifications
You must be signed in to change notification settings - Fork 15
/
aws_content_workflow.yaml
200 lines (187 loc) · 6.15 KB
/
aws_content_workflow.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
AWSTemplateFormatVersion: 2010-09-09
Description: "This is the initial design, currently deprecated in favour of AWS VOD Template!!"
Parameters:
BucketName:
Type: String
Default: 'transcodekit'
Resources:
TranscodekitLambdaPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
Description: Provides necessary access to MediaConvert and CloudWatch logs
ManagedPolicyName: !Join
- '-'
- - !Ref 'AWS::Region'
- TranscodekitLambdaExecutor
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- mediaconvert:CreateJob
- mediaconvert:DescribeEndpoints
Resource:
- '*'
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource:
- '*'
- Effect: Allow
Action:
- 'iam:PassRole'
Resource:
- !GetAtt MediaConvertRole.Arn
LambdaExecutionRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: LambdaExecution
Description: Allows Transcodekit lambda function to start MediaConvert job
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
ManagedPolicyArns:
- !Ref TranscodekitLambdaPolicy
TranscodeVideoFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: TranscodeVideo
Description: Sends uploaded S3 object to MediaConvert for transcoding
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
Environment:
Variables:
ROLE: !GetAtt MediaConvertRole.Arn
Code:
ZipFile: |
const path = require('path')
const MediaConvert = require('aws-sdk/clients/mediaconvert')
exports.handler = async function(event, context, cb) {
const mediaConvert = new MediaConvert({
apiVersion: '2017-08-29'
})
const s3Record = event.Records[0].s3
const { base: fileName, name: title } = path.parse(s3Record.object.key)
try {
const { Endpoints: [{ Url: endpoint }]} = await mediaConvert.describeEndpoints().promise()
mediaConvert.endpoint = endpoint
const hlsResponse = await mediaConvert.createJob({
Role: process.env.ROLE,
JobTemplate: 'System-Ott_Hls_Ts_Avc_Aac',
Settings: {
Inputs: [{
FileInput: `s3://${s3Record.bucket.name}/${s3Record.object.key}`,
AudioSelectors: {
'Audio Selector 1': {
Offset: 0
}
},
}],
OutputGroups: [{
OutputGroupSettings: {
Type: 'HLS_GROUP_SETTINGS',
HlsGroupSettings: {
Destination: `s3://${s3Record.bucket.name}/assets/${title}/hls/`
}
}
}]
},
}).promise()
const dashResponse = await mediaConvert.createJob({
Role: process.env.ROLE,
JobTemplate: 'System-Ott_Dash_Mp4_Avc_Aac',
Settings: {
Inputs: [{
FileInput: `s3://${s3Record.bucket.name}/${s3Record.object.key}`,
AudioSelectors: {
'Audio Selector 1': {
Offset: 0
}
},
}],
OutputGroups: [{
OutputGroupSettings: {
Type: 'DASH_ISO_GROUP_SETTINGS',
DashIsoGroupSettings: {
Destination: `s3://${s3Record.bucket.name}/assets/${title}/dash/`
}
}
}]
},
}).promise()
cb(null, [hlsResponse, dashResponse])
} catch (e) {
cb(e.message)
}
}
Runtime: nodejs12.x
S3Bucket:
Type: 'AWS::S3::Bucket'
DependsOn:
- S3ExecutionPermission
Properties:
BucketName: !Ref BucketName
NotificationConfiguration:
LambdaConfigurations:
- Event: 's3:ObjectCreated:*'
Function: !GetAtt TranscodeVideoFunction.Arn
Filter:
S3Key:
Rules:
- Name: prefix
Value: 'originals/'
S3ExecutionPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt TranscodeVideoFunction.Arn
Action: lambda:InvokeFunction
Principal: s3.amazonaws.com
SourceAccount: !Ref 'AWS::AccountId'
SourceArn: !Sub 'arn:aws:s3:::${BucketName}'
TranscodekitMediaConvertPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: !Join
- '-'
- - !Ref 'AWS::Region'
- TranscodekitMediaConverter
Description: Provides access to S3 for MediaConvert transcode jobs
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 's3:PutObject'
Resource:
- !Sub 'arn:aws:s3:::${BucketName}/assets/*'
- Effect: Allow
Action:
- 's3:GetObject'
Resource:
- !Sub 'arn:aws:s3:::${BucketName}/originals/*'
MediaConvertRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: MediaConvertExecution
Description: Allows MediaConvert to gain access to S3
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- mediaconvert.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
ManagedPolicyArns:
- !Ref TranscodekitMediaConvertPolicy