Video Thumbnailer Using AWS Lambda
A video thumbnail extractor using serverless functions packaged with containers
This example lives in the pulumi/examples repository. Check out just this directory to use it:
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examplesgit -C pulumi-examples sparse-checkout set aws-ts-lambda-thumbnailercd pulumi-examples/aws-ts-lambda-thumbnailerA video thumbnail extractor using serverless functions. The video processing function is packaged as a Docker container.
Navigate to Running Container Images in AWS Lambda for a full walkthrough.
Prerequisites#
To run this example, make sure Docker is installed and running.
Running the App#
-
Create a new stack:
pulumi stack init dev -
Configure Pulumi to use an AWS region of your choice, for example:
pulumi config set aws:region us-west-2 -
Restore NPM modules via
npm installoryarn install. -
Preview and deploy the app via
pulumi up. The preview will take some time, as it builds a Docker container. A total of 16 resources are created.$ pulumi upPreviewing update (dev)...Do you want to perform this update? yesUpdating (dev)Type Name Status+ pulumi:pulumi:Stack video-thumbnailer-lambda-dev created+ ├─ awsx:ecr:Repository sampleapp created+ │ ├─ aws:ecr:Repository sampleapp created+ │ └─ aws:ecr:LifecyclePolicy sampleapp created+ ├─ aws:s3:Bucket bucket created+ │ ├─ aws:s3:BucketEventSubscription onNewThumbnail created+ │ │ └─ aws:lambda:Permission onNewThumbnail created+ │ ├─ aws:s3:BucketEventSubscription onNewVideo created+ │ │ └─ aws:lambda:Permission onNewVideo created+ │ └─ aws:s3:BucketNotification onNewVideo created+ ├─ aws:iam:Role onNewThumbnail created+ ├─ aws:iam:Role thumbnailerRole created+ ├─ aws:lambda:Function onNewThumbnail created+ ├─ aws:iam:RolePolicyAttachment onNewThumbnail-32be53a2 created+ ├─ aws:iam:RolePolicyAttachment lambdaFullAccess created+ └─ aws:lambda:Function thumbnailer createdOutputs:bucketName: "bucket-7c6b55a"Resources:+ 16 createdDuration: 1m41s -
View the stack outputs:
$ pulumi stack outputCurrent stack outputs (1):OUTPUT VALUEbucketName bucket-7c6b55a -
Upload a video, embedding the timestamp in the filename:
$ aws s3 cp ./sample/cat.mp4 s3://$(pulumi stack output bucketName)/cat_00-01.mp4upload: sample/cat.mp4 to s3://***/cat_00-01.mp4 -
View the logs from both Lambda functions:
$ pulumi logs -fCollecting logs for stack dev since 2020-12-02T08:58:43.000+01:00.2020-12-02T09:58:39.747+01:00[ thumbnailer-dbb2a35] START RequestId: 3ec2886e-e739-4764-be3b-a8e5a48a4986 Version: $LATEST2020-12-02T09:58:39.750+01:00[ thumbnailer-dbb2a35] 2020-12-02T08:58:39.748Z 3ec2886e-e739-4764-be3b-a8e5a48a4986 INFO Video handler called2020-12-02T09:58:39.750+01:00[ thumbnailer-dbb2a35] 2020-12-02T08:58:39.750Z 3ec2886e-e739-4764-be3b-a8e5a48a4986 INFO aws s3 cp s3://bucket-33b87c2/cat_00-01.mp4 /tmp/cat_00-01.mp4download: s3://bucket-33b87c2/cat_00-01.mp4 to ../../tmp/cat_00-01.mp4ed 256.0 KiB/666.5 KiB (1.2 MiB/s) with 1 file(s) remaining2020-12-02T09:58:53.068+01:00[ thumbnailer-dbb2a35] 2020-12-02T08:58:53.068Z 3ec2886e-e739-4764-be3b-a8e5a48a4986 INFO ffmpeg -v error -i /tmp/cat_00-01.mp4 -ss 00:01 -vframes 1 -f image2 -an -y /tmp/cat.jpg2020-12-02T09:59:01.701+01:00[ thumbnailer-dbb2a35] 2020-12-02T08:59:01.701Z 3ec2886e-e739-4764-be3b-a8e5a48a4986 INFO aws s3 cp /tmp/cat.jpg s3://bucket-33b87c2/cat.jpgupload: ../../tmp/cat.jpg to s3://bucket-33b87c2/cat.jpg pleted 86.6 KiB/86.6 KiB (315.8 KiB/s) with 1 file(s) remaining2020-12-02T09:59:11.628+01:00[ thumbnailer-dbb2a35] 2020-12-02T08:59:11.627Z 3ec2886e-e739-4764-be3b-a8e5a48a4986 INFO *** New thumbnail: file cat_00-01.mp4 was saved at 2020-12-02T08:58:33.845Z.2020-12-02T09:59:11.668+01:00[ thumbnailer-dbb2a35] END RequestId: 3ec2886e-e739-4764-be3b-a8e5a48a49862020-12-02T09:59:11.668+01:00[ thumbnailer-dbb2a35] REPORT RequestId: 3ec2886e-e739-4764-be3b-a8e5a48a4986 Duration: 31920.84 ms Billed Duration: 32733 ms Memory Size: 128 MB Max Memory Used: 128 MB Init Duration: 811.55 ms2020-12-02T09:59:11.777+01:00[ onNewThumbnail-2f969e0] START RequestId: 07c13039-eccb-4e38-a3cf-c7fa11982b84 Version: $LATEST2020-12-02T09:59:11.788+01:00[ onNewThumbnail-2f969e0] 2020-12-02T08:59:11.782Z 07c13039-eccb-4e38-a3cf-c7fa11982b84 INFO onNewThumbnail called2020-12-02T09:59:11.788+01:00[ onNewThumbnail-2f969e0] 2020-12-02T08:59:11.788Z 07c13039-eccb-4e38-a3cf-c7fa11982b84 INFO *** New thumbnail: file cat.jpg was saved at 2020-12-02T08:59:06.333Z.2020-12-02T09:59:11.809+01:00[ onNewThumbnail-2f969e0] END RequestId: 07c13039-eccb-4e38-a3cf-c7fa11982b842020-12-02T09:59:11.809+01:00[ onNewThumbnail-2f969e0] REPORT RequestId: 07c13039-eccb-4e38-a3cf-c7fa11982b84 Duration: 31.96 ms Billed Duration: 32 ms Memory Size: 128 MB Max Memory Used: 65 MB Init Duration: 171.22 ms``` -
Download the key frame:
$ aws s3 cp s3://$(pulumi stack output bucketName)/cat.jpg .download: s3://***/cat.jpg to ./cat.jpg
Clean up#
To clean up the resources, you will first need to clear the contents of the bucket.
aws s3 rm s3://$(pulumi stack output bucketName) --recursiveThen, run pulumi destroy and answer the confirmation question at the prompt.