Make HLS Stream using AWS (S3 Bucket, Cloud Front, Elastic Transcoder ,Lambda function ) | Make your own Netflix or YouTube
1. Create source (input) S3 bucket
2. Create destination (output) s3 bucket
3. Create cloud-front for Serve data
4. Setup Elastic transcoder (Create pipeline, Create job, Job execute)
5. Create role for lambda function
6. Create lambda function and create trigger and write program add access role
7. Check Lambda function logs in cloud-watch
8. Generate Key Management Service for transcoder pipeline
9. Configure transcoder pipeline
10. Configure transcoder job with aes-128 encryption
11. Modify lambda function
1. Create source (input) S3 bucket
Create new S3 bucket
Create Folder
2. Create destination (output) s3 bucket
Create new s3 bucket
Set block all public access
Set CORS configuration
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>http://localhost:4200</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
3. Create cloud-front for Serve data
Go to cloud-front Distributions
Create Distributions
Select WEB click on get started
Select Origin Domain Name s3 bucket name
Origin Path is is folder name start with ‘/’
Restrict bucket access => yes
Origin access identity => create new identity
Grant read permissions on bucket => yes update bucket policy
Viewer protocol policy => HTTP and HTTPS
Allowed HTTP methods => GET,HEAD,OPTION
Cached HTTP Method => check OPTIONS
Object caching => select use origin cache header
Smooth streaming => No
Restrict viewer access => no
Compress object automatically => No
Click on create distribution
If status in Deployed click on id and check again
Check setting







4. Setup Elastic transcoder (Create pipeline, Create job, Job execute)
Go to Elastic Transcoder
Click on create new pipeline
Write pipeline name
Select input bucket
Select default role
Configuration for Amazon S3 Bucket for Transcoded Files and Playlists
Select output bucket
Storage class => standard
Add permission =>
Grantee type => Amazon s3 group
Grantee => all user
Access => check Open/Download
Configuration for Amazon S3 Bucket for Thumbnails
Select output bucket for thumb
Storage class => standard
Add permission =>
Grantee type => amazon s3 group
Grantee => all users
Access => checked open/Download
Click on save

Create job
Click on Create new job
Select pipeline
Output key prefix is basically folder name start with ‘/’
Select input detail
Output Details
Select Preset = > system preset: HLS 400k
Segment duration => 10
Output key (file name)=> hls_out_400k1
If you want multiple output click on “add another output”
Click on add playlist
Write playlist name
Select format=> HLSv3
Add output master playlist
Then click create new job
After your job status is complete go on s3 output bucket and check output files are created or not
5. Create role for lambda function
Create new role for assign lambda function
Simply click on create role

select lambda and go next
Add this three policy's
1. AmazonElasticTranscoder_FullAccess
2. AmazonS3FullAccess
3. AWSLambdaBasicExecutionRole
And go to next
Write role name and click on create role
6. Create lambda function and create trigger and write program
Simply click on “create function “
Write function name
Select node.js for programming
Select => use existing role
Then
click on create function
Select S3
Select input bucket
Select event type => all object create event
Write prefix (folder name)
Finally click on add
click on your function name
and write this program
***************start
var aws = require('aws-sdk');
var elastictranscoder = new
aws.ElasticTranscoder();
// return filename without
extension
function baseName(path) {
return path.split('/').reverse()[0].split('.')[0];
}
exports.handler = function(event,
context) {
console.log('Received event:', JSON.stringify(event, null, 2));
var key = event.Records[0].s3.object.key;
var outputPrefix = baseName(key);
var params = {
Input: {
Key: key
},
PipelineId: '1577189858990-pxg05d',
/*Your Elastic Transcoder Pipeline Id*/
OutputKeyPrefix: 'videocontent/',
/*output folder name*/
Outputs: [
{
Key: outputPrefix +
'_hls_out_600k', /*write output prefix filename*/
PresetId: '1351620000001-200040', /*select
your preset and and write this preset id*/
SegmentDuration: '10',
}
],
Playlists: [
{
Format: 'HLSv3', /*this for formate*/
Name: outputPrefix, /*this playlist
name is same as file name*/
OutputKeys: [outputPrefix +
'_hls_out_600k'] /*add all output prefix file name*/
}
]
};
elastictranscoder.createJob(params, function(err, data) {
if (err) console.log(err, err.stack); //
an error occurred
else
console.log(data); //
successful response
});
};
*****************end
If you want to test , configure test event
Select event template => Amazon S3 put
Write event name => tests3
************************start
{
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "ap-south-1",
"eventTime": "1970-01-01T00:00:00.000Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"responseElements": {
"x-amz-request-id": "EXAMPLE123456789",
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "videostreamingapp",
"ownerIdentity": {
"principalId": "AHDSKDKDF876"
},
"arn": "arn:aws:s3:::videostreamingapp"
},
"object": {
"key": "videocontent/3211lambda.mp4",
"size": 1024,
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901"
}
}
}
]
}
*********************end
Finish this all work and click on save and click on test for fire test event if your output is proper and check transcoder job count is increase and your output file are generated properly mines your lambda function working fine
7. Check Lambda function logs in cloud-watch
If you want to see lambda function logs simply go to cloud-watch
Click on “logs” then click on “let’s get started”
Select your function name
And select task for view detail logs
8. Generate “Key Management Service(KMS)” for transcoder pipeline
Go to key management service and simply click on “create key”
In Key administrators ADD “Elastic_Transcoder_Default_Role”
In Key users ADD “Elastic_Transcoder_Default_Role”
And
Add tags Key: transpipe , Value: transpipejob
9. Configure transcoder pipeline
Simply copy ARN in Your KMS and goto transcoder pipeline and edit your pipeline and
Encryption panel AWS KMS Key ARN select custom and paste this ARN in this textbox and click on save
10. Configure transcoder job with aes-128 encryption
Copy you old HLS working jobs go on Playlist section in content protection select HLS AES
Key Storage Policy: select With Variant Playlists
Checked Auto generate encryption key
Finally click on create job
11. Modify lambda function
Configure playlist section in lambda function
Playlists: [
{
Format: 'HLSv3',
Name: outputPrefix,
OutputKeys: [outputPrefix + '_hls_out_600k'],
HlsContentProtection: {
Method: 'aes-128',
KeyStoragePolicy: "WithVariantPlaylists"
}
}
]

**********************start
var aws = require('aws-sdk');
var elastictranscoder = new aws.ElasticTranscoder();
// return filename without extension
function baseName(path) {
return path.split('/').reverse()[0].split('.')[0];
}
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
var key = event.Records[0].s3.object.key;
var outputPrefix = baseName(key);
var params = {
Input: {
Key: key
},
PipelineId: '1579157724628-1vsykj', /*Your Elastic Transcoder Pipeline Id*/
OutputKeyPrefix: 'videocontent/',
Outputs: [
{
Key: outputPrefix + '_hls_out_600k',
PresetId: '1351620000001-200040', // HLS v3 and v4 (Apple HTTP Live Streaming), 600 kilobits/second, Video-only
SegmentDuration: '10',
}
],
Playlists: [
{
Format: 'HLSv3',
Name: outputPrefix,
OutputKeys: [outputPrefix + '_hls_out_600k'],
HlsContentProtection: {
Method: 'aes-128',
KeyStoragePolicy: "WithVariantPlaylists"
}
}
]
};
elastictranscoder.createJob(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
};
**********************end
If you like this blog so pls share and Write Comments about Your experience,
Thank You.
Comments
Post a Comment