This is just a proof of concept of handling images using S3 + Lambda + serverless.
-
Install the required node modules:
npm install
-
Start the app (replace
YOUR_AWS_*
accordingly):BUCKET=YOUR_AWS_BUCKET_NAME AWS_KEY=YOUR_AWS_KEY AWS_SECRET=YOUR_AWS_SECRET node app.js
-
Install serverless:
npm i -g serverless
-
Configure serverless (replace AWS_KEY and AWS_SECRET accordingly):
serverless config credentials --provider aws --key AWS_KEY --secret AWS_SECRET
-
Copy
serverless_dist.yml
toserverless.yml
and adjust accordingly. -
Copy
config/serverless/environment/dev_dist.yml
toconfig/serverless/environment/dev.yml
and adjust accordingly. -
Build node_modules using an AWS Lambda environment with Docker (some packages like Sharp require compiled files in an environment similar to AWS Lambda - by executing this command, the local version will stop working, so in order to make it work locally again you have to delete node_modules and
npm install
):docker run --rm -v "$PWD":/var/task lambci/lambda:build-nodejs6.10
-
Deploy:
serverless deploy
-
Make sure that the environment variables (
BUCKET
,AWS_KEY
andAWS_SECRET
) exist at https://console.aws.amazon.com/lambda/home?region=YOUR_REGION#/functions/YOUR_LAMBDA_FUNCTION
Endpoint | Status | Comment |
---|---|---|
/add-images | Done | |
/delete-image | Done | |
/fetch-image | Done | |
/list-images | Done | |
/resize-image | Done | |
/zip-image | Done | |
/zip-images | Done |
sls
is a shortcut forserverless
. So you can usesls deploy
instead ofserverless deploy
etc.- In case of local development, consider installing
nodemon
(npm i -g nodemon
) to make the development process easier (usingnodemon app.js
will restart the server every time a file changes). - When adding new routes/functions remember to create the environment variables (
BUCKET
,AWS_KEY
andAWS_SECRET
).
In case you face problems you can try one of the followings:
- Make sure the two config files exist and contain the correct values.
- If already deployed code stopped working right after a deploy, try to remove (
sls remove
) and re-deploy. In that case remember to create the environment variables again (BUCKET
,AWS_KEY
andAWS_SECRET
).
Just follow the default guidelines from http://standardjs.com.
If you want to automate your code formatting and be able to detect unused and deprecated code:
-
Install
standard
globally:npm install -g standard
-
Execute this periodically:
standard --fix
-
Add this in the pre-commit hook:
# Ensure all JavaScript files staged for commit pass standard code style function xargs-r() { # Portable version of "xargs -r". The -r flag is a GNU extension that # prevents xargs from running if there are no input files. if IFS= read -r -d $'\n' path; then { echo "$path"; cat; } | xargs $@ fi } git diff --name-only --cached --relative | grep '\.jsx\?$' | sed 's/[^[:alnum:]]/\\&/g' | xargs-r -E '' -t standard if [[ $? -ne 0 ]]; then echo 'JavaScript Standard Style errors were detected. Aborting commit.' exit 1 fi
- https://read.acloud.guru/serverless-image-optimization-and-delivery-510b6c311fe5
- https://iiaku.com/2017/06/06/aws-lambda-part-ii-how-to-create-zip-from-files/
- https://github.com/iiAku/zip-aws-lambda
- https://github.com/amazon-archives/serverless-image-resizing
- https://medium.freecodecamp.org/express-js-and-aws-lambda-a-serverless-love-story-7c77ba0eaa35