Skip to content

AWS Lambda function that automatically resize and create thumbnail

License

Notifications You must be signed in to change notification settings

DnSu/aws-s3-lambda-crop-n-resize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aws-s3-lambda-crop-n-resize

Lambda function when triggered by an S3 PUT (configured in lambda triggers in AWS) automatically resizes images to "standar sizes" and place them in destination bucket (dsBucket).

Why? Cause S3 + lambda is cheaper than image processor like imgix. Best for small projects that don't really generate revenue.

Dependencies

$ npm install async gm

Configure

$ cp config.json.sample config.json

Sample config.json

{
	"dstBucket": "dslambdaresize",
	"thumbs":[
		{"folder":"square", "type":"thumbnail", "geometry":"500x500"         },
		{"folder":"large" , "type":"resize",    "width":"900", "height":"900"},
		{"folder":"medium", "type":"resize",    "width":"600"                },
		{"folder":"small" , "type":"resize",                   "height":"300"}
	]
}
  • dstBucket: destination bucket (source bucket is determined by trigger in lambda)
  • thumbs: various size and shares
    • folder: each entry must be unique, or you'll be overwriting the files
    • type: processing mode
      • thumbnail: resize and center-crops the image
      • resize: simple reduce in size preserving aspect ratio of original
    • geometry: required for thumbnail mode
    • height and width: at least one is required for resize mode

Deploy

  1. zip content of the folder
  2. upload as lambda function
    • Lambda Config
      • Runtime: Node.js 4.3
      • Handler: index.handler
      • Memory: 1024MB
      • Timeout: 3 min
  3. maker sure dstBucket exists

Notes

  • Large files (3+ MB) might cause problems. Try allocating more memory in Lambda.
  • Start with 1024mb of ram and 2 minutes timeout, and read log to adjust
  • Code based on AWS tutorial
  • Only handles jpg and png

About

AWS Lambda function that automatically resize and create thumbnail

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published