{"id":13527125,"url":"https://github.com/ysugimoto/aws-lambda-image","last_synced_at":"2025-04-08T09:09:12.444Z","repository":{"id":2102982,"uuid":"45705613","full_name":"ysugimoto/aws-lambda-image","owner":"ysugimoto","description":"Automatic image resize/reduce on AWS Lambda","archived":false,"fork":false,"pushed_at":"2023-06-09T09:53:49.000Z","size":3367,"stargazers_count":824,"open_issues_count":62,"forks_count":218,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-01T08:41:49.287Z","etag":null,"topics":["aws","aws-lambda","image","nodejs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ysugimoto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2015-11-06T20:26:13.000Z","updated_at":"2025-03-10T10:34:59.000Z","dependencies_parsed_at":"2023-07-06T16:01:45.194Z","dependency_job_id":null,"html_url":"https://github.com/ysugimoto/aws-lambda-image","commit_stats":{"total_commits":214,"total_committers":29,"mean_commits":7.379310344827586,"dds":0.5420560747663552,"last_synced_commit":"b35bdf8ed61a3dc84013786569a84f1baafb6086"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysugimoto%2Faws-lambda-image","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysugimoto%2Faws-lambda-image/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysugimoto%2Faws-lambda-image/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysugimoto%2Faws-lambda-image/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ysugimoto","download_url":"https://codeload.github.com/ysugimoto/aws-lambda-image/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247809964,"owners_count":20999816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-lambda","image","nodejs"],"created_at":"2024-08-01T06:01:41.701Z","updated_at":"2025-04-08T09:09:12.416Z","avatar_url":"https://github.com/ysugimoto.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# aws-lambda-image\n\n[![Build Status](https://travis-ci.org/ysugimoto/aws-lambda-image.svg?branch=master)](https://travis-ci.org/ysugimoto/aws-lambda-image)\n[![Code Climate](https://codeclimate.com/github/ysugimoto/aws-lambda-image/badges/gpa.svg)](https://codeclimate.com/github/ysugimoto/aws-lambda-image)\n[![Coverage Status](https://coveralls.io/repos/github/ysugimoto/aws-lambda-image/badge.svg?branch=master)](https://coveralls.io/github/ysugimoto/aws-lambda-image?branch=master)\n[![npm version](https://badge.fury.io/js/aws-lambda-image.svg)](https://badge.fury.io/js/aws-lambda-image)\n[![Join the chat at https://gitter.im/aws-lambda-image](https://img.shields.io/badge/GITTER-join%20chat-green.svg)](https://gitter.im/aws-lambda-image?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nAn AWS Lambda Function to resize/reduce images automatically. When an image is\nput on AWS S3 bucket, this package will resize/reduce it and put to S3.\n\n## Requirements\n\n- Node.js ( AWS Lambda supports versions of **8.10** or later )\n\n### Important Notice\n\nFrom `nodejs10.x`, AWS Lambda doesn't bundle `ImageMagick` and image related libraries.\n\nhttps://forums.aws.amazon.com/thread.jspa?messageID=906619\u0026tstart=0\n\nTherefore, if you'd deploy with `nodejs10.x` runtime (but we prefer and default as it), it needs to install AWS Lambda Layer with this function.\nThis project can support it automatically, see [LAYERS](https://github.com/ysugimoto/aws-lambda-image/blob/master/doc/LAYERS.md) in detail.\n\n## Preparation\n\nClone this repository and install dependencies:\n\n```bash\ngit clone git@github.com:ysugimoto/aws-lambda-image.git\ncd aws-lambda-image\nnpm install .\n```\n\nWhen upload to AWS Lambda, the project will bundle only needed files - no dev\ndependencies will be included.\n\n## Configuration\n\nConfiguration file you will find under the name `config.json` in project root.\nIt's copy of our example file `config.json.sample`. More or less it looks like:\n\n```json\n{\n  \"bucket\": \"your-destination-bucket\",\n  \"backup\": {\n      \"directory\": \"./original\"\n  },\n  \"reduce\": {\n      \"directory\": \"./reduced\",\n      \"prefix\": \"reduced-\",\n      \"quality\": 90,\n      \"acl\": \"public-read\",\n      \"cacheControl\": \"public, max-age=31536000\"\n  },\n  \"resizes\": [\n    {\n      \"size\": 300,\n      \"directory\": \"./resized/small\",\n      \"prefix\": \"resized-\",\n      \"cacheControl\": null\n    },\n    {\n      \"size\": 450,\n      \"directory\": \"./resized/medium\",\n      \"suffix\": \"_medium\"\n    },\n    {\n      \"size\": \"600x600^\",\n      \"gravity\": \"Center\",\n      \"crop\": \"600x600\",\n      \"directory\": \"./resized/cropped-to-square\"\n    },\n    {\n      \"size\": 600,\n      \"directory\": \"./resized/600-jpeg\",\n      \"format\": \"jpg\",\n      \"background\": \"white\"\n    },\n    {\n      \"size\": 900,\n      \"directory\": \"./resized/large\",\n      \"quality\": 90\n    }\n  ]\n}\n```\n\n### Configuration Parameters\n\n| name          | field         | type    | description                                                                                                                                      |\n|:-------------:|:-------------:|:-------:|--------------------------------------------------------------------------------------------------------------------------------------------------|\n| bucket        | -             | String  | Destination bucket name at S3 to put processed image. If not supplied, it will use same bucket of event source.                                  |\n| jpegOptimizer | -             | String  | Determine optimiser that should be used `mozjpeg` (default) or `jpegoptim` ( only `JPG` ).                                                       |\n| acl           | -             | String  | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property).         |\n| cacheControl  | -             | String  | Cache-Control of S3 object. If not specified, defaults to original image's Cache-Control.                                                        |\n| keepExtension | -             | Boolean | Global setting fo keeping original extension. If `true`, program keeps orignal file extension. otherwise use strict extension eg JPG,jpeg -\u003e jpg |\n| backup        | -             | Object  | Backup original file setting.                                                                                                                    |\n|               | bucket        | String  | Destination bucket to override. If not supplied, it will use `bucket` setting.                                                                   |\n|               | directory     | String  | Image directory path. Supports relative and absolute paths. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#directory)                          |\n|               | template      | Object  | Map representing pattern substitution pair. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#template)                                           |\n|               | prefix        | String  | Prepend filename prefix if supplied.                                                                                                             |\n|               | suffix        | String  | Append filename suffix if supplied.                                                                                                              |\n|               | acl           | String  | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property).         |\n|               | cacheControl  | String  | Cache-Control of S3 object. If not specified, defaults to original image's Cache-Control.                                                        |\n|               | keepExtension | Boolean | If `true`, program keeps orignal file extension. otherwise, use strict extension eg JPG,jpeg -\u003e jpg                                              |\n|               | move          | Boolean | If `true`, an original uploaded file will delete from Bucket after completion.                                                                   |\n| reduce        | -             | Object  | Reduce setting following fields.                                                                                                                 |\n|               | quality       | Number  | Determine reduced image quality ( only `JPG` ).                                                                                                  |\n|               | jpegOptimizer | String  | Determine optimiser that should be used `mozjpeg` (default) or `jpegoptim` ( only `JPG` ).                                                       |\n|               | bucket        | String  | Destination bucket to override. If not supplied, it will use `bucket` setting.                                                                   |\n|               | directory     | String  | Image directory path. Supports relative and absolute paths. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#directory)                          |\n|               | template      | Object  | Map representing pattern substitution pair. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#template)                                           |\n|               | prefix        | String  | Prepend filename prefix if supplied.                                                                                                             |\n|               | suffix        | String  | Append filename suffix if supplied.                                                                                                              |\n|               | acl           | String  | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property).         |\n|               | cacheControl  | String  | Cache-Control of S3 object. If not specified, defaults to original image's Cache-Control.                                                        |\n|               | keepExtension | Boolean | If `true`, program keeps orignal file extension. otherwise, use strict extension eg JPG,jpeg -\u003e jpg                                              |\n| resize        | -             | Array   | Resize setting list of following fields.                                                                                                         |\n|               | size          | String  | Image dimensions. [See ImageMagick geometry documentation](http://imagemagick.org/script/command-line-processing.php#geometry).                  |\n|               | format        | String  | Image format override. If not supplied, it will leave the image in original format.                                                              |\n|               | crop          | String  | Dimensions to crop the image. [See ImageMagick crop documentation](http://imagemagick.org/script/command-line-options.php#crop).                 |\n|               | gravity       | String  | Changes how `size` and `crop`. [See ImageMagick gravity documentation](http://imagemagick.org/script/command-line-options.php#gravity).          |\n|               | quality       | Number  | Determine reduced image quality ( forces format `JPG` ).                                                                                         |\n|               | jpegOptimizer | String  | Determine optimiser that should be used `mozjpeg` (default) or `jpegoptim` ( only `JPG` ).                                                       |\n|               | orientation   | Boolean | Auto orientation if value is `true`.                                                                                                             |\n|               | bucket        | String  | Destination bucket to override. If not supplied, it will use `bucket` setting.                                                                   |\n|               | directory     | String  | Image directory path. Supports relative and absolute paths. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#directory)                          |\n|               | template      | Object  | Map representing pattern substitution pair. Mode details in [DIRECTORY.md](doc/DIRECTORY.md/#template)                                           |\n|               | prefix        | String  | Prepend filename prefix if supplied.                                                                                                             |\n|               | suffix        | String  | Append filename suffix if supplied.                                                                                                              |\n|               | acl           | String  | Permission of S3 object. [See AWS ACL documentation](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property).         |\n|               | cacheControl  | String  | Cache-Control of S3 object. If not specified, defaults to original image's Cache-Control.                                                        |\n|               | keepExtension | Boolean | If `true`, program keeps orignal file extension. otherwise, use strict extension eg JPG,jpeg -\u003e jpg                                              |\n| optimizers    | -             | Object  | Definitions for override the each Optimizers command arguments.                                                                                  |\n|               | pngquant      | Array   | `Pngquant` command arguments. Default is `[\"--speed=1\", \"256\"]`.                                                                                 |\n|               | jpegoptim     | Array   | `Jpegoptim` command arguments. Default is `[\"-s\", \"--all-progressive\"]`.                                                                         |\n|               | mozjpeg       | Array   | `Mozjpeg` command arguments. Default is `[\"-optimize\", \"-progressive\"]`.                                                                         |\n|               | gifsicle      | Array   | `Gifsicle` command arguments. Default is `[\"--optimize\"]`.                                                                                       |\n\nNote that the `optmizers` option will **force** override its command arguments, so if you define these configurations, we don't care any more about how optimizer works.\n\n### Testing Configuration\n\nIf you want to check how your configuration will work, you can use:\n\n```bash\nnpm run test-config\n```\n\n## Installation\n\n### Setup\n\nTo use the automated deployment scripts you will need to have\n[aws-cli installed and configured](http://docs.aws.amazon.com/cli/latest/userguide/installing.html).\n\nDeployment scripts are pre-configured to use some default values for the Lambda\nconfiguration. I you want to change any of those just use:\n\n```bash\nnpm config set aws-lambda-image:profile default\nnpm config set aws-lambda-image:region eu-west-1\nnpm config set aws-lambda-image:memory 1280\nnpm config set aws-lambda-image:timeout 5\nnpm config set aws-lambda-image:name lambda-function-name\nnpm config set aws-lambda-image:role lambda-execution-role\n```\n\nNote that `aws-lambda-image:name` and `aws-lambda-image:role` are optional.\nIf you want to change lambda function name or execution role, type above commands before deploy.\n\nAnd make sure AWS Lambda Layer has installed in your account/region.\nSee [LAYERS](https://github.com/ysugimoto/aws-lambda-image/blob/master/doc/LAYERS.md) for instructions.\n\n### Deployment\n\nCommand below will deploy the Lambda function on AWS, together with setting up\nroles and policies.\n\n```bash\nnpm run deploy\n```\n\n*Notice*: Because there are some limitations in `Claudia.js` support for\npolicies, which could lead to issues with `Access Denied` when processing\nimages from one bucket and saving them to another, we have decided to introduce\nsupport for custom policies.\n\n#### Custom policies\n\nPolicies which should be installed together with our Lambda function are stored\nin `policies/` directory. We keep there policy that grants access to all\nbuckets, which is preventing possible errors with `Access Denied` described\nabove. If you have any security-related concerns, feel free to change the:\n\n```json\n\"Resource\": [\n    \"*\"\n]\n```\n\nin the `policies/s3-bucket-full-access.json` to something more restrictive,\nlike:\n\n```json\n\"Resource\": [\n    \"arn:aws:s3:::destination-bucket-name/*\"\n]\n```\n\nJust keep in mind, that you need to make those changes before you do the\ndeployment.\n\n### Adding S3 event handlers\n\nTo complete installation process you will need to take one more action. It will\nallow you to install S3 Bucket event handler, which will send information about\nall uploaded images directly to your Lambda function.\n\n```bash\nnpm run add-s3-handler --s3_bucket=\"your-bucket-name\" --s3_prefix=\"directory/\" --s3_suffix=\".jpg\"\n```\n\nYou are able to install multiple handlers per Bucket. So, to add handler for PNG\nfiles you just need to re-run above command with different _suffix_, ie:\n\n```bash\nnpm run add-s3-handler --s3_bucket=\"your-bucket-name\" --s3_prefix=\"directory/\" --s3_suffix=\".png\"\n```\n\n### Adding SNS message handlers\n\nAs an addition, you can also setup and SNS message handler in case you would\nlike to process S3 events over an SNS topic.\n\n```bash\nnpm run add-sns-handler --sns_topic=\"arn:of:SNS:topic\"\n```\n\n### Updating\n\nTo update Lambda with you latest code just use command below. Script will build\nnew package and automatically publish it on AWS.\n\n```bash\nnpm run update\n```\n\n### More\n\nFor more scripts look into [package.json](package.json).\n\n## Complete / Failed hooks\n\nYou can handle resize/reduce/backup process on success/error result on\n`index.js`. `ImageProcessor::run` will return `Promise` object, run your\noriginal code:\n\n```javascript\nprocessor.run(config)\n.then(function(proceedImages)) {\n\n    // Success case:\n    // proceedImages is list of ImageData instance on you configuration\n\n    /* your code here */\n\n    // notify lambda\n    context.succeed(\"OK, numbers of \" + proceedImages.length + \" images has proceeded.\");\n})\n.catch(function(messages) {\n\n    // Failed case:\n    // messages is list of string on error messages\n\n    /* your code here */\n\n    // notify lambda\n    context.fail(\"Woops, image process failed: \" + messages);\n});\n```\n\n## Image resize\n\n- `ImageMagick` (installed on AWS Lambda)\n\n## Image reduce\n\n- [cjpeg](https://github.com/mozilla/mozjpeg)\n- [jpegoptim](https://github.com/tjko/jpegoptim)\n- [pngquant](https://pngquant.org/)\n- [gifsicle](https://github.com/kohler/gifsicle)\n\n## License\n\nMIT License.\n\n## Author\n\nYoshiaki Sugimoto\n\n## Image credits\n\nThanks for testing fixture images:\n\n- [pngimg](http://pngimg.com/)\n- [pakutaso](https://www.pakutaso.com/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fysugimoto%2Faws-lambda-image","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fysugimoto%2Faws-lambda-image","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fysugimoto%2Faws-lambda-image/lists"}