-
Notifications
You must be signed in to change notification settings - Fork 1
/
forest_transcoder.yaml
181 lines (166 loc) · 5.43 KB
/
forest_transcoder.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
AWSTemplateFormatVersion: 2010-09-09
Parameters:
BucketName:
Type: String
Default: 'my-existing-app-bucket-name'
Resources:
ForestTranscoderLambdaPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
Description: Provides necessary access to MediaConvert and CloudWatch logs
ManagedPolicyName: !Join
- '-'
- - !Ref 'AWS::Region'
- ForestTranscoderLambdaExecutor
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 ForestTranscoder 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 ForestTranscoderLambdaPolicy
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 { MediaConvertClient, CreateJobCommand, DescribeEndpointsCommand } = require("@aws-sdk/client-mediaconvert");
exports.handler = async function(event, context, cb) {
const objectPath = event.object_path;
const objectDirectory = objectPath.split('/').slice(0, -1).join('/');
const region = event.region;
const bucket = event.bucket;
let client = new MediaConvertClient({
region: region
})
try {
const describeCommand = new DescribeEndpointsCommand({});
const describeResponse = await client.send(describeCommand);
const endpoint = describeResponse['Endpoints'][0]['Url'];
client = new MediaConvertClient({
region: region,
endpoint: endpoint
})
const input = {
Role: process.env.ROLE,
JobTemplate: 'forest-transcode-video',
Settings: {
Inputs: [{
FileInput: `s3://${bucket}/${objectPath}`,
AudioSelectors: {
'Audio Selector 1': {
Offset: 0
}
},
}],
OutputGroups: [{
OutputGroupSettings: {
Type: 'FILE_GROUP_SETTINGS',
FileGroupSettings: {
Destination: `s3://${bucket}/${objectDirectory}/transcoded/`
}
}
}]
},
};
const command = new CreateJobCommand(input);
const createJobResponse = await client.send(command);
let responseBody = {
input: event,
createJobResponse: createJobResponse
};
let lambdaResponse = {
statusCode: 200,
body: JSON.stringify(responseBody)
};
cb(null, lambdaResponse)
} catch (e) {
cb(e.message)
}
}
Runtime: nodejs18.x
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}'
ForestTranscoderMediaConvertPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: !Join
- '-'
- - !Ref 'AWS::Region'
- ForestTranscoderMediaConverter
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}/*'
- Effect: Allow
Action:
- 's3:GetObject'
Resource:
- !Sub 'arn:aws:s3:::${BucketName}/*'
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 ForestTranscoderMediaConvertPolicy