Skip to content

Commit 0f94b12

Browse files
committed
initial version for the example
1 parent b5b3985 commit 0f94b12

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

example/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
STACK_NAME ?= imagemagick-layer-example
2+
IMAGE_MAGICK_STACK_NAME ?= imagemagick-layer
3+
4+
IMAGEMAGICK_LAYER ?=(shell aws cloudformation describe-stacks --stack-name $(IMAGE_MAGICK_STACK_NAME) --query Stacks[].Outputs[].OutputValue --output text)
5+
SOURCES=$(shell find src/)
6+
7+
clean:
8+
rm -rf build
9+
10+
output.yml: template.yml $(SOURCES)
11+
mkdir -p build
12+
aws cloudformation package --template-file $< --output-template-file $@ --s3-bucket $(DEPLOYMENT_BUCKET_NAME)
13+
14+
deploy: output.yml
15+
aws cloudformation deploy --template-file $< --stack-name $(STACK_NAME) --capabilities CAPABILITY_IAM --parameter-overrides ImageMagickLayer=$(IMAGEMAGICK_LAYER)
16+
aws cloudformation describe-stacks --stack-name $(STACK_NAME) --query Stacks[].Outputs --output table
17+

example/src/child-process-promise.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*global module, require, console, Promise */
2+
'use strict';
3+
const childProcess = require('child_process'),
4+
spawnPromise = function (command, argsarray, envOptions) {
5+
return new Promise((resolve, reject) => {
6+
console.log('executing', command, argsarray.join(' '));
7+
const childProc = childProcess.spawn(command, argsarray, envOptions || {env: process.env, cwd: process.cwd()}),
8+
resultBuffers = [];
9+
childProc.stdout.on('data', buffer => {
10+
console.log(buffer.toString());
11+
resultBuffers.push(buffer);
12+
});
13+
childProc.stderr.on('data', buffer => console.error(buffer.toString()));
14+
childProc.on('exit', (code, signal) => {
15+
console.log(`${command} completed with ${code}:${signal}`);
16+
if (code || signal) {
17+
reject(`${command} failed with ${code || signal}`);
18+
} else {
19+
resolve(Buffer.concat(resultBuffers).toString().trim());
20+
}
21+
});
22+
});
23+
};
24+
module.exports = {
25+
spawn: spawnPromise
26+
};

example/src/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
const s3Util = require('./s3-util'),
3+
childProcessPromise = require('./child-process-promise'),
4+
path = require('path'),
5+
os = require('os'),
6+
EXTENSION = process.env.EXTENSION,
7+
THUMB_WIDTH = process.env.THUMB_WIDTH,
8+
OUTPUT_BUCKET = process.env.OUTPUT_BUCKET,
9+
MIME_TYPE = process.env.MIME_TYPE;
10+
11+
exports.handler = function (eventObject, context) {
12+
const eventRecord = eventObject.Records && eventObject.Records[0],
13+
inputBucket = eventRecord.s3.bucket.name,
14+
key = eventRecord.s3.object.key,
15+
id = context.awsRequestId,
16+
resultKey = key.replace(/\.[^.]+$/, EXTENSION),
17+
workdir = os.tmpdir(),
18+
inputFile = path.join(workdir, id + path.extname(key)),
19+
outputFile = path.join(workdir, 'converted-' + id + EXTENSION);
20+
21+
22+
console.log('converting', inputBucket, key, 'using', inputFile);
23+
return s3Util.downloadFileFromS3(inputBucket, key, inputFile)
24+
.then(() => childProcessPromise.spawn(
25+
'/opt/bin/convert',
26+
[inputFile, '-resize', `${THUMB_WIDTH}x`, outputFile],
27+
{env: process.env, cwd: workdir}
28+
))
29+
.then(() => s3Util.uploadFileToS3(OUTPUT_BUCKET, resultKey, outputFile, MIME_TYPE));
30+
};

example/src/s3-util.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*global module, require, Promise, console */
2+
3+
const aws = require('aws-sdk'),
4+
fs = require('fs'),
5+
s3 = new aws.S3(),
6+
downloadFileFromS3 = function (bucket, fileKey, filePath) {
7+
'use strict';
8+
console.log('downloading', bucket, fileKey, filePath);
9+
return new Promise(function (resolve, reject) {
10+
const file = fs.createWriteStream(filePath),
11+
stream = s3.getObject({
12+
Bucket: bucket,
13+
Key: fileKey
14+
}).createReadStream();
15+
stream.on('error', reject);
16+
file.on('error', reject);
17+
file.on('finish', function () {
18+
console.log('downloaded', bucket, fileKey);
19+
resolve(filePath);
20+
});
21+
stream.pipe(file);
22+
});
23+
}, uploadFileToS3 = function (bucket, fileKey, filePath, contentType) {
24+
'use strict';
25+
console.log('uploading', bucket, fileKey, filePath);
26+
return s3.upload({
27+
Bucket: bucket,
28+
Key: fileKey,
29+
Body: fs.createReadStream(filePath),
30+
ACL: 'private',
31+
ContentType: contentType
32+
}).promise();
33+
};
34+
35+
module.exports = {
36+
downloadFileFromS3: downloadFileFromS3,
37+
uploadFileToS3: uploadFileToS3
38+
};

example/template.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
AWSTemplateFormatVersion: 2010-09-09
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
Example project demonstrating the usage of the ImageMagic Layer for AWS Linux 2 runtimes.
5+
6+
Parameters:
7+
ImageMagickLayer:
8+
Type: String
9+
ConversionFileType:
10+
Type: String
11+
Default: png
12+
ConversionMimeType:
13+
Type: String
14+
Default: image/png
15+
ThumbnailWidth:
16+
Type: Number
17+
Default: 500
18+
Resources:
19+
UploadBucket:
20+
Type: AWS::S3::Bucket
21+
22+
ResultsBucket:
23+
Type: AWS::S3::Bucket
24+
25+
ConvertFileFunction:
26+
Type: AWS::Serverless::Function
27+
Properties:
28+
Handler: index.handler
29+
Timeout: 180
30+
MemorySize: 1024
31+
Runtime: nodejs10.x
32+
CodeUri: src
33+
Layers:
34+
- !Ref ImageMagickLayer
35+
Policies:
36+
- S3CrudPolicy:
37+
BucketName: !Sub "${AWS::StackName}-*"
38+
Environment:
39+
Variables:
40+
OUTPUT_BUCKET: !Ref ResultsBucket
41+
EXTENSION: !Sub '.${ConversionFileType}'
42+
MIME_TYPE: !Ref ConversionMimeType
43+
THUMB_WIDTH: !Ref ThumbnailWidth
44+
Events:
45+
FileUpload:
46+
Type: S3
47+
Properties:
48+
Bucket: !Ref UploadBucket
49+
Events: s3:ObjectCreated:*
50+
51+
Outputs:
52+
UploadBucket:
53+
Description: "Upload S3 bucket"
54+
Value: !Ref UploadBucket
55+
ResultsBucket:
56+
Description: "Results S3 bucket"
57+
Value: !Ref ResultsBucket
58+

0 commit comments

Comments
 (0)