Skip to content

Commit 09cff27

Browse files
committed
init
yep
1 parent 6385bc0 commit 09cff27

19 files changed

+7709
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
layers/sharp/out/sharp-0.23.0-aws-lambda-linux-x64-node-10.16.3.zip filter=lfs diff=lfs merge=lfs -text

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# package directories
2+
node_modules
3+
# Serverless directories
4+
.serverless
5+
.DS_Store
6+
.env

deploy.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
stage=${STAGE}
2+
region=${REGION}
3+
bucket=${BUCKET}
4+
5+
sls config credentials \
6+
--provider aws \
7+
--key ${SLS_KEY} \
8+
--secret ${SLS_SECRET} \
9+
--profile ${PROFILE}
10+
11+
echo 'Deploying'
12+
sls deploy -s ${STAGE}

docker-compose.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: '3'
2+
services:
3+
sharp:
4+
image: woss/aws-sharp-layer
5+
build:
6+
context: ./layers/sharp
7+
dockerfile: Dockerfile
8+
volumes:
9+
- ./layers/sharp:/app
10+
deploy:
11+
image: woss/aws-sharp-layer-test
12+
build:
13+
context: ./example
14+
dockerfile: Dockerfile
15+
volumes:
16+
- ./example:/app
17+
env_file:
18+
- ./.env

example/.editorconfig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = false
8+
insert_final_newline = false

example/Dockerfile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM amazonlinux as baseBox
2+
WORKDIR /app
3+
4+
RUN yum -y install make gcc*
5+
RUN curl --silent --location https://rpm.nodesource.com/setup_10.x | bash -
6+
RUN yum -y install nodejs
7+
8+
9+
####################
10+
## DEPLOYMENT BOX ##
11+
####################
12+
FROM baseBox as deployBox
13+
COPY . .
14+
RUN yarn
15+
16+
RUN ["chmod", "+x", "deploy.sh"]
17+
CMD ./deploy.sh ; sleep 2m

example/package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "aws-lambda-layer-sharp",
3+
"version": "1.0.0",
4+
"main": "src/handler.js",
5+
"license": "MIT",
6+
"devDependencies": {
7+
"nodemon": "^1.19.2",
8+
"serverless": "^1.52.0",
9+
"serverless-dotenv-plugin": "^2.1.1",
10+
"serverless-offline": "^6.0.0-alpha.40",
11+
"serverless-webpack": "^5.3.1",
12+
"source-map-support": "^0.5.11",
13+
"webpack": "^4.5.0",
14+
"webpack-cli": "^3.3.7",
15+
"webpack-node-externals": "^1.6.0",
16+
"sharp": "^0.23.0"
17+
},
18+
"scripts": {
19+
"start": "nodemon -w ./src/**/* --exec \"yarn offline\" ",
20+
"offline": "sls offline start"
21+
},
22+
"keywords": [],
23+
"author": "",
24+
"description": "",
25+
"dependencies": {
26+
"axios-debug-log": "^0.6.2",
27+
"axios": "^0.19.0",
28+
"mime-types": "^2.1.24"
29+
}
30+
}

example/sample.env

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SLS_KEY=
2+
SLS_SECRET=
3+
PROFILE=testSls
4+
STAGE=dev
5+
REGION=eu-east-1
6+
DEBUG=axios
7+
# SLS_DEBUG=*

example/serverless.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
service: aws-lambda-layer-sharp
2+
frameworkVersion: '>=1.34.0 <2.0.0'
3+
publish: false
4+
provider:
5+
name: aws
6+
runtime: nodejs10.x
7+
profile: testSls
8+
region: eu-west-1
9+
apiGateway:
10+
minimumCompressionSize: 1024
11+
binaryMediaTypes:
12+
- '*/*'
13+
functions:
14+
test:
15+
handler: ./example/src/handler.handler
16+
events:
17+
- http:
18+
path: sharp
19+
method: get
20+
layers:
21+
- { Ref: SharpLambdaLayer }
22+
custom:
23+
webpack:
24+
webpackConfig: 'webpack.config.js' # Name of webpack configuration file
25+
# includeModules: true # Node modules configuration for packaging
26+
includeModules:
27+
forceExclude:
28+
- sharp
29+
packager: 'yarn' # Packager that will be used to package your external modules
30+
excludeFiles: src/**/*.test.js # Provide a glob for files to ignore
31+
32+
serverless-offline:
33+
port: 4000
34+
host: 0.0.0.0
35+
dontPrintOutput: true
36+
useSeparateProcesses: true
37+
38+
plugins:
39+
- serverless-dotenv-plugin
40+
- serverless-webpack
41+
- serverless-offline
42+
43+
package:
44+
individually: true

example/source-map-install.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('source-map-support').install();

example/src/handler.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require('axios-debug-log')
2+
const axios = require('axios')
3+
const sharp = require('sharp')
4+
const mime = require('mime-types')
5+
6+
const imagePath =
7+
'https://s3-eu-west-1.amazonaws.com/sensio.photo/public-assets/20160903-_MG_5320.jpg'
8+
9+
exports.handler = async event => {
10+
try {
11+
const { data: imageData } = await axios.get(imagePath, {
12+
responseType: 'arraybuffer'
13+
})
14+
15+
const img = await sharp(imageData)
16+
.withMetadata()
17+
.resize(800)
18+
.convolve({
19+
width: 3,
20+
height: 3,
21+
kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1]
22+
})
23+
.webp()
24+
.toBuffer()
25+
26+
const ContentType = mime.contentType('webp')
27+
const response = {
28+
statusCode: 200,
29+
headers: {
30+
'Content-Type': ContentType
31+
},
32+
body: img.toString('base64'),
33+
isBase64Encoded: true
34+
}
35+
return response
36+
} catch (error) {
37+
console.error(error)
38+
}
39+
}

example/webpack.config.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const path = require('path')
2+
const nodeExternals = require('webpack-node-externals')
3+
const slsw = require('serverless-webpack')
4+
const entries = {}
5+
6+
Object.keys(slsw.lib.entries).forEach(
7+
key => (entries[key] = ['./source-map-install.js', slsw.lib.entries[key]])
8+
)
9+
module.exports = {
10+
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
11+
devtool: 'source-map',
12+
// devtool: 'inline-source-map',
13+
// entry: entries,
14+
entry: slsw.lib.entries,
15+
target: 'node',
16+
externals: [nodeExternals(), 'sharp'],
17+
module: {
18+
rules: [{ test: /\.ts(x?)$/, loader: 'ts-loader' }]
19+
},
20+
resolve: {
21+
extensions: ['.ts', '.js', '.tsx', '.jsx']
22+
},
23+
// output: {
24+
// libraryTarget: 'commonjs',
25+
// path: path.join(__dirname, './../.dist'),
26+
// filename: 'handler.js',
27+
// devtoolModuleFilenameTemplate: '../../[resource-path]',
28+
// },
29+
output: {
30+
libraryTarget: 'commonjs',
31+
path: path.join(__dirname, 'dist'),
32+
filename: '[name].js',
33+
sourceMapFilename: '[file].map',
34+
devtoolModuleFilenameTemplate: '[absolute-resource-path]'
35+
},
36+
optimization: { minimize: false },
37+
plugins: [],
38+
performance: {
39+
// Turn on size warnings for entry points
40+
hints: 'warning'
41+
}
42+
}

0 commit comments

Comments
 (0)