s3-image-optimizer v0.1.0
Optim
Automagitically optimize your images on S3 with the magic of AWS Lambda.
Optim is a super-simple Lambda function that can listen to an S3 bucket for uploads, and runs everything it can through imagemin.
Setup
Clone this repo
Run
npm installFill in
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYin.envto a set of credentials that can create Lambda functions (alternatively have these already in your environment)Create an IAM role for Optim to use. It needs the following permissions on all the S3 buckets you want to use (allowing these operations on ARN
*is easiest to start with):getObjectputObjectputObjectAcl
Find the ARN for this role. It looks something like
arn:aws:iam::1234567890:role/rolename.Fill in
AWS_ROLE_ARNin.envRun
npm run deployHurrah, your Lambda function is now deployed! It'll be created with the name
optim-productionunless you changed values in.envYou can now hook this function up to any S3 bucket you like in the management console. Easiest way is to follow AWS's guide
Configuration
There are two sets of configuration here. The .env file contains configuration related to setup and deployment, and runtime.env is for configuration of how Optim behaves.
In .env:
AWS_ACCESS_KEY_ID: the AWS access key used to deploy the Lambda functionAWS_SECRET_ACCESS_KEY: the corresponding secret access keyAWS_ROLE_ARN: role with which the lambda function will be executedAWS_REGION: which region to deploy toAWS_FUNCTION_NAMEandAWS_ENVIRONMENTcontrol naming of the lambda function createdAWS_MEMORY_SIZEis the amount of memory given to your Lambda. It's also related to how much CPU share it gets. Since optimizing images is fairly intensive, probably best to keep this highAWS_TIMEOUTruntime timeout for the lambda in seconds up to 5 minutes. Again, image optimization is fairly intensive so you'll probably want to leave this at the maximum of 300.
In runtime.env:
UPLOAD_ACL: finalised images will be uploaded with this permission level. Should be one ofprivatepublic-readpublic-read-writeaws-exec-readauthenticated-readbucket-owner-readbucket-owner-full-control. Default ispublic-read.MAX_FILE_SIZE: files over this size in bytes will be skipped (e.g. big PNGs will probably just hit the timeout anyway). Set to-1for no limitPNG_OPTIM_LEVEL: Optimization level to use for PNGs, between 0 and 7. Lower level means faster optimization, higher means better results.
10 years ago