{"id":22229715,"url":"https://github.com/boycce/s3-lambda-thumbnail-generator","last_synced_at":"2025-08-31T03:33:22.957Z","repository":{"id":77207738,"uuid":"343570405","full_name":"boycce/s3-lambda-thumbnail-generator","owner":"boycce","description":"An Amazon Web Services Lambda function that generates thumbnails from images uploaded to AWS S3","archived":false,"fork":false,"pushed_at":"2024-06-09T10:38:21.000Z","size":22235,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-22T07:50:11.456Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/boycce.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-01T22:07:19.000Z","updated_at":"2024-06-09T10:38:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"6f9bf287-0b4c-4fc8-960e-e12c9ab84d78","html_url":"https://github.com/boycce/s3-lambda-thumbnail-generator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/boycce/s3-lambda-thumbnail-generator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boycce%2Fs3-lambda-thumbnail-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boycce%2Fs3-lambda-thumbnail-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boycce%2Fs3-lambda-thumbnail-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boycce%2Fs3-lambda-thumbnail-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boycce","download_url":"https://codeload.github.com/boycce/s3-lambda-thumbnail-generator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boycce%2Fs3-lambda-thumbnail-generator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272934859,"owners_count":25017961,"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","status":"online","status_checked_at":"2025-08-31T02:00:09.071Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-12-03T01:12:03.664Z","updated_at":"2025-08-31T03:33:22.935Z","avatar_url":"https://github.com/boycce.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# S3 Lambda Thumbnail Generator\n\nAn Amazon Web Services Lambda function that generates thumbnails for images uploaded to s3 under `full/`, any predefined ACLs are also maintained.\n\n## Setup S3 Bucket\n\n1. Create a S3 bucket\n2. Create an IAM policy, e.g. `MyProjectBucketsOnly` [See policy example below](#s3-bucket-policy-example)\n1. Create an IAM user, e.g. `MyProject`. Select attach policies direclty, and select your new policy. You'll be writting to this bucket via the user's API credentials\n\n## Setup Function\n\n1. Log in to your AWS account, and goto Lambda\n2. Click \"Create function\"\n    - Click \"Author from scratch\"\n    - Add function name, e.g. \"MyProjectThumbnailGenerator\"\n    - Select the correct runtime, see package.json\n    - Select \"Create a new role with basic Lambda permissions\"\n    - Hit \"Create function\"\n    \n3. Click \"Upload From\" \u003e and select `./bundle.zip`\n3. Click \"Function overview\" \u003e \"Add trigger\"\n    - Select **S3** trigger\n    - Choose your bucket\n    - Choose **PUT** for the event type (and **CompleteMultipartUpload** depending on your requirements)\n    - Choose what prefix-path triggers the function, i.e. full/\n    - (optional) Choose what filetypes triggers the function, e.g. .png\n    - Hit \"Add\"\n4. Click \"Add a layer\" (bottom of page)\n    - Click \"Specify an ARN\"\n    - (Once per account) open [`image-magick-lambda-layer`](https://serverlessrepo.aws.amazon.com/applications/us-east-1/145266761615/image-magick-lambda-layer) (2019-05-17) in a new tab, and click deploy. Or create a new layer manually by using `./image-magick-lambda-layer.zip` (2019-11-15)\n    - Open \"Lambda\" \u003e \"Layers\" on another tab, and copy your \"image magick\" layer ARN\n    - Now paste in your layer ARN, e.g. \"arn:aws:lambda:ap-southeast-2:349844946466:layer:image-magick-custom:3\"\n    - Hit \"Add\"\n5. Click \"Configuration\" \u003e \"General configuration\" \u003e \"Edit\"\n    - Set timeout to 30 sec\n    - Set memory to 512MB (see [lambda memory](#lambda-memory))\n5. Click \"Configuration\" \u003e \"Permissions\" \u003e \"Execution Role\" \u003e \"Role name\" Link (e.g. MyProjectThumbnailGenerator-role-*)\n    - Click \"Add Permissions\" \u003e \"Attach Policies\"\n    - Search for your s3 bucket policy, e.g. MyProjectBucketsOnly\n    - Hit \"Attach Policies\"\n6. Done\n\n## Image Settings\n\nYou can override the default settings per file by adding the following file user-defined metadata (which is easily defined via [monastery options](https://boycce.github.io/monastery/image-plugin.html)). Note that if you define only one size, the other two default sizes will be skipped. `*` = `null`, i.e. any size.\n```\nx-amz-meta-small = *x300\nx-amz-meta-medium = *x800\nx-amz-meta-large = *x1200\n```\n\n## Default Settings\n\nYou easily can customise the setting defaults in `index.js` on Lambda\n```js\nvar settings = {\n  // Allowed filetypes\n  filetypes: ['png', 'jpg', 'jpeg',  'bmp', 'tiff', 'gif'],\n  // Thumbnail sizes (excluding 'full')\n  sizes: [\n    { name: \"small\", width: null, height: 300 },\n    { name: \"medium\", width: null, height: 800 },\n    { name: \"large\", width: null, height: 1200 },\n  ]\n}\n```\n\n## Lambda Testing\n\nTo test this function, you can create a new Lambda test event with the following JSON object. Replace `{YOUR-BUCKET}`\n with your bucket name and upload `./test/test.jpg` to `{YOUR-BUCKET}/full/test.jpg`.\n \nIf something is going wrong, you can dig into the CloudWatch logs (\"Monitor\" \u003e \"View CloudWatch Logs\")\n\n```json\n{\n  \"Records\": [\n    {\n      \"eventVersion\": \"2.0\",\n      \"eventSource\": \"aws:s3\",\n      \"awsRegion\": \"us-east-1\",\n      \"eventTime\": \"1970-01-01T00:00:00.000Z\",\n      \"eventName\": \"ObjectCreated:Put\",\n      \"userIdentity\": {\n        \"principalId\": \"EXAMPLE\"\n      },\n      \"requestParameters\": {\n        \"sourceIPAddress\": \"127.0.0.1\"\n      },\n      \"responseElements\": {\n        \"x-amz-request-id\": \"EXAMPLE123456789\",\n        \"x-amz-id-2\": \"EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH\"\n      },\n      \"s3\": {\n        \"s3SchemaVersion\": \"1.0\",\n        \"configurationId\": \"testConfigRule\",\n        \"bucket\": {\n          \"name\": \"{YOUR-BUCKET}\",\n          \"ownerIdentity\": {\n            \"principalId\": \"EXAMPLE\"\n          },\n          \"arn\": \"arn:aws:s3:::{YOUR-BUCKET}\"\n        },\n        \"object\": {\n          \"key\": \"full/test.jpg\",\n          \"size\": 483813,\n          \"eTag\": \"0123456789abcdef0123456789abcdef\"\n        }\n      }\n    }\n  ]\n}\n```\n\n## Lambda Memory\n\n  If the lambda function doesn't have enough CPU power, large images take too long to process and store in memory, which fails to generate the thumbnail(s). You will need to increase the memory of the lambda function, which increases the CPU output proportionality.\n  \n  If there is not enough memory you may see the following CloudWatch error:\n  \n  `ERROR Unable to generate images for '{bucket-name}/test.jpg', error: Error: Stream yields empty buffer`\n  \n  Here are some good lambda memory guidelines:\n  \n  - 1.0MB (2560x1440) images requires at least 256MB of lambda memory\n  - 3.7MB (3626x4532) images requires at least 512MB of lambda memory\n  - 4.5MB (7008x4672) images requires at least 750MB of lambda memory\n\n## Building\n\nYou can run `npm run zip` to re-generate the compressed file\n\nIf you are receiving \"module not found\" errors in the lambda console, this is probably due the zip file having the incorrect permissions set before zipping. (linux: you cannot use a umask'd ntfs partition)\n\n## S3 Bucket Policy Example\n```json\n{\n    \"Version\": \"2012-10-17\",\n    \"Statement\": [\n        {\n            \"Action\": \"s3:ListAllMyBuckets\",\n            \"Effect\": \"Allow\",\n            \"Resource\": \"arn:aws:s3:::*\"\n        },\n        {\n            \"Action\": \"s3:*\",\n            \"Effect\": \"Allow\",\n            \"Resource\": [\n                \"arn:aws:s3:::my-project-dev\",\n                \"arn:aws:s3:::my-project-dev/*\",\n                \"arn:aws:s3:::my-project-staging\",\n                \"arn:aws:s3:::my-project-staging/*\",\n                \"arn:aws:s3:::my-project-prod\",\n                \"arn:aws:s3:::my-project-prod/*\"\n            ]\n        }\n    ]\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboycce%2Fs3-lambda-thumbnail-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboycce%2Fs3-lambda-thumbnail-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboycce%2Fs3-lambda-thumbnail-generator/lists"}