Skip to content

Commit 8cf6dbf

Browse files
authored
Merge pull request #335 from serverless/layers
lambda layers example!
2 parents bfdabe6 + 123959c commit 8cf6dbf

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ serverless install -u https://github.com/serverless/examples/tree/master/folder-
4343
| Example | Runtime |
4444
|:--------------------------- |:-----|
4545
| [Aws Dotnet Rest Api With Dynamodb](https://github.com/serverless/examples/tree/master/aws-dotnet-rest-api-with-dynamodb/src/DotNetServerless.Lambda) <br/> Reading/Writing operations using .NET Core and DynamoDB | dotnet |
46+
| [Aws Lambda Layer](https://github.com/serverless/examples/tree/master/aws-ffmpeg-layer) | nodeJS |
4647
| [Aws Golang Simple Http Endpoint](https://github.com/serverless/examples/tree/master/aws-golang-simple-http-endpoint) <br/> Example demonstrates how to setup a simple HTTP GET endpoint with golang | golang |
4748
| [Aws Golang Stream Kinesis To Elasticsearch](https://github.com/serverless/examples/tree/master/aws-golang-stream-kinesis-to-elasticsearch) <br/> Pull data from AWS Kinesis streams and forward to elasticsearch | golang |
4849
| [Aws Alexa Skill](https://github.com/serverless/examples/tree/master/aws-node-alexa-skill) <br/> This example demonstrates how to use an AWS Lambdas for your custom Alexa skill. | nodeJS |

Diff for: aws-ffmpeg-layer/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.serverless/
2+
node_modules/
3+
layer/

Diff for: aws-ffmpeg-layer/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# AWS FFmepg Layer & a service using it to create GIFs
2+
```
3+
git clone https://github.com/serverless/examples
4+
cd examples/aws-ffmpeg-layer
5+
sls deploy
6+
```
7+
8+
See the blog post about this example for more details:
9+
https://serverless.com/blog/publish-aws-lambda-layers-serverless-framework/

Diff for: aws-ffmpeg-layer/build.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
mkdir -p layer
3+
cd layer
4+
rm -rf *
5+
curl -O https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz
6+
tar xf ffmpeg-git-amd64-static.tar.xz
7+
mv ffmpeg-git-*-amd64-static ffmpeg
8+
rm ffmpeg-git-amd64-static.tar.xz

Diff for: aws-ffmpeg-layer/handler.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const { spawnSync } = require("child_process");
2+
const { readFileSync, writeFileSync, unlinkSync } = require("fs");
3+
const AWS = require("aws-sdk");
4+
5+
const s3 = new AWS.S3();
6+
7+
module.exports.mkgif = async (event, context) => {
8+
if (!event.Records) {
9+
console.log("not an s3 invocation!");
10+
return;
11+
}
12+
for (const record of event.Records) {
13+
if (!record.s3) {
14+
console.log("not an s3 invocation!");
15+
continue;
16+
}
17+
if (record.s3.object.key.endsWith(".gif")) {
18+
console.log("already a gif");
19+
continue;
20+
}
21+
// get the file
22+
const s3Object = await s3
23+
.getObject({
24+
Bucket: record.s3.bucket.name,
25+
Key: record.s3.object.key
26+
})
27+
.promise();
28+
// write file to disk
29+
writeFileSync(`/tmp/${record.s3.object.key}`, s3Object.Body);
30+
// convert to gif!
31+
spawnSync(
32+
"/opt/ffmpeg/ffmpeg",
33+
[
34+
"-i",
35+
`/tmp/${record.s3.object.key}`,
36+
"-f",
37+
"gif",
38+
`/tmp/${record.s3.object.key}.gif`
39+
],
40+
{ stdio: "inherit" }
41+
);
42+
// read gif from disk
43+
const gifFile = readFileSync(`/tmp/${record.s3.object.key}.gif`);
44+
// delete the temp files
45+
unlinkSync(`/tmp/${record.s3.object.key}.gif`);
46+
unlinkSync(`/tmp/${record.s3.object.key}`);
47+
// upload gif to s3
48+
await s3
49+
.putObject({
50+
Bucket: record.s3.bucket.name,
51+
Key: `${record.s3.object.key}.gif`,
52+
Body: gifFile
53+
})
54+
.promise();
55+
}
56+
};

Diff for: aws-ffmpeg-layer/package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "aws-lambda-layer",
3+
"description": "",
4+
"version": "0.1.0",
5+
"dependencies": {},
6+
"devDependencies": {}
7+
}

Diff for: aws-ffmpeg-layer/serverless.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
service: gifmaker
2+
frameworkVersion: ">=1.34.0 <2.0.0"
3+
4+
provider:
5+
name: aws
6+
runtime: nodejs8.10
7+
iamRoleStatements:
8+
- Effect: Allow
9+
Action:
10+
- s3:PutObject
11+
- s3:GetObject
12+
Resource: "arn:aws:s3:::${self:custom.bucket}/*"
13+
14+
functions:
15+
mkgif:
16+
handler: handler.mkgif
17+
events:
18+
- s3: ${self:custom.bucket}
19+
layers:
20+
- {Ref: FfmpegLambdaLayer}
21+
22+
layers:
23+
ffmpeg:
24+
path: layer
25+
26+
custom:
27+
bucket: ${env:BUCKET, 'ffmpeg-layer-gif-maker'}

0 commit comments

Comments
 (0)