This repository contains sample code for constructing a Content Delivery Network (CDN) to deliver images from S3. By utilizing S3 Object Lambda, you can transform aspects such as the image size, quality, and format. The implementation of the Lambda function uses Sharp.
Install the npm modules.
npm install
This project uses the AWS CDK. If you haven't completed the bootstrap process, you can do so using the following command:
npx cdk bootstrap
Deploy using CDK.
npx cdk deploy
The URL of the ImageTransformerEndpoint displayed in the output will serve as the CDN's endpoint.
The image files placed in /imgs
will be uploaded to S3. By default, a file named test.png
is uploaded. You can verify the image display by accessing the URL below:
https://xxxxxxxxxxxxxx.cloudfront.net/test.png
(Please replace 'xxxxxxxxxxxxxx' with the appropriate value)
width
Specifies the widthheight
Specifies the heightfit
Specifies how to resize if both width and height are specified- For more details, see https://sharp.pixelplumbing.com/api-resize
quality
Specifies image quality (default=85)format
Specifies the format (jpeg, png, gif, webp, avif)auto
Convert to specified format by looking at the Accept header (webp, avif)
Example: Set the width to 300px https://xxxxxxxxxxxxxx.cloudfront.net/test.png?width=300
Example: Convert png to jpeg https://xxxxxxxxxxxxxx.cloudfront.net/test.png?format=jpeg
Example: Check the Accept header and convert to webp if possible https://xxxxxxxxxxxxxx.cloudfront.net/test.png?auto=webp
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.