skip to Main Content

enter image description here

using following function code:


'use strict';
//import { AWS } from "@aws-sdk/client-s3"
var AWS = require('aws-sdk');
//import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"
var s3 = new AWS.S3({

apiVersion: '2012–09–25'

});

var eltr = new AWS.ElasticTranscoder({

apiVersion: '2012–09–25',

region: 'ap-south-1'

});

exports.handler = function(event, context) {

console.log('Executing Elastic Transcoder');

var bucket = event.Records[0].s3.bucket.name;

var key = event.Records[0].s3.object.key;

var pipelineId = '1****************';

if (bucket !== '*****') {

context.fail('Incorrect Input Bucket');

return;

}

var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/+/g, " "));

//the object may have spaces

var newKey = key.split('.')[0];

var fk= "400K";

var sk= "600K";

var om= "1.5M";

var tm= "2M";

var th= "Thumb"

var params = {

PipelineId: pipelineId,

OutputKeyPrefix: newKey + '/',

Input: {

Key: srcKey,

FrameRate: 'auto',

Resolution: 'auto',

AspectRatio: 'auto',

Interlaced: 'auto',

Container: 'auto'

},

Outputs: [{

Key: fk + '/' +'hls4K-' + newKey + '.ts',

ThumbnailPattern: th +'/'+'thumbs4K-{count}',

PresetId: '***************',//HLS v3 400K/s

SegmentDuration:"5"

},

{

Key: sk + '/' +'hls6K-' + newKey + '.ts',

ThumbnailPattern: th +'/'+'thumbs6K-{count}',

PresetId: '**********', //HLS v3 600K/s

SegmentDuration:"5"

},

{

Key: om + '/' +'hls1.5M-' + newKey + '.ts',

ThumbnailPattern: th +'/'+'thumbs1.5M-{count}',

PresetId: '***************', //HLS v3 1.5M/s

SegmentDuration:"5"

},

,

{

Key: tm + '/' +'hls2M-' + newKey + '.ts',

ThumbnailPattern: th +'/'+'thumbs2M-{count}',

PresetId: '*****************', //HLS v3 2M/s

SegmentDuration:"5"

}

],

Playlists: [{

Format: 'HLSv3',

Name: 'hls-'+ newKey,

OutputKeys: [ fk+ '/' +'hls4K-'+ newKey + '.ts',sk + '/' +'hls6K-' + newKey + '.ts',om + '/'

+'hls1.5M-' + newKey + '.ts',,tm + '/' +'hls2M-' + newKey + '.ts']

}]

};

console.log('Starting Job');

eltr.createJob(params, function(err, data){

if (err){

console.log(err);

} else {

console.log(data);

}

context.succeed('Job well done');

});

};

```

Configuration Template:

```
# This AWS SAM template has been generated from your function's configuration. If
# your function has one or more triggers, note that the AWS resources associated
# with these triggers aren't fully specified in this template and include
# placeholder values. Open this template in AWS Application Composer or your
# favorite IDE and modify it to specify a serverless application with other AWS
# resources.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Application Model template describing your function.
Resources:
  videocomporession:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Description: ''
      MemorySize: 128
      Timeout: 3
      Handler: index.handler
      Runtime: nodejs20.x
      Architectures:
        - x86_64
      EphemeralStorage:
        Size: 512
      EventInvokeConfig:
        MaximumEventAgeInSeconds: 21600
        MaximumRetryAttempts: 2
      PackageType: Zip
      Policies:
        - Statement:
            - Effect: Allow
              Action:
                - logs:CreateLogGroup
              Resource: arn:aws:logs:us-east-2:158624315941:*
            - Effect: Allow
              Action:
                - logs:CreateLogStream
                - logs:PutLogEvents
              Resource:
                - >-
                  arn:aws:logs:us-east-2:158624315941:log-group:/aws/lambda/videocomporession:*
      SnapStart:
        ApplyOn: None
      Events:
        BucketEvent1:
          Type: S3
          Properties:
            Bucket:
              Ref: Bucket1
            Events:
              - s3:ObjectCreated:*
      RuntimeManagementConfig:
        UpdateRuntimeOn: Auto
  Bucket1:
    Type: AWS::S3::Bucket
    Properties:
      VersioningConfiguration:
        Status: Enabled
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256
  BucketPolicy1:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: Bucket1
      PolicyDocument:
        Statement:
          - Action: s3:*
            Effect: Deny
            Principal: '*'
            Resource:
              - arn:aws:s3:::Bucket1/*
              - arn:aws:s3:::Bucket1
            Condition:
              Bool:
                aws:SecureTransport: false

```

2

Answers


  1. Node.js 18 and later runtimes include only AWS SDK for JavaScript v3. Earlier runtimes only include AWS SDK for JavaScript v2.

    I think your options are:

    1. migrate to, and use, SDK v3
    2. configure your Lambda function with a nodejs16.x runtime and use SDK v2
    3. deploy SDK v2 with your Lambda function deployment package
    4. configure a custom Lambda Layer that includes AWS SDK v2
    Login or Signup to reply.
  2. Like jarmod said, create a Lambda layer.
    With a lambda layer you can "upload" the package to your lambda function. As a result you can import a package in your lambda code.

    Good Video that worked for me: https://youtu.be/jyuZDkiHe2Q?si=EqoSUknkVK9NElOD

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search