An AWS Lambda Function to resize S3 images using Node.js on the fly.
A detailed, screenshot-ed, step-by-step guide can be found here.
Setup S3 Bucket.
Create bucket
button.Block all public access
option.Create bucket
.Setup IAM Role.
Access management
click on Roles
.Create role
button.Select type of trusted entity
select AWS Service (EC2, Lambda and others)
.Choose a use case
select Lambda
.Next: Permissions
.Create policy
.{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Resource": "arn:aws:s3:::YOUR_S3_BUCKET_NAME/*",
"Action": [
"s3:GetObject",
"s3:PutObject"
]
}
]
}
YOUR_S3_BUCKET_NAME
with your created S3 Bucket Name.Next: Tags
.Next: Review
.Review ploicy
.Create policy
.Next: Tags
.Next: Review
.Create role
.Attach policies
.AmazonAPIGatewayPushToCloudWatchLogs
from the Filter policies search box.Attach policy
.Setup Lambda Function
Create function
.Choose one of the following options to create your function
choose Author from scratch
.Node.js 12.x
.Use an existing role
, then from roles list select the role you just created.Create function
.Function code
section, click to expand Actions
dropdown the click Upload a .zip file
.Environment variables
section, click Edit
.Add environment variable
.Key
type BUCKET
and for Value
paste in YOUR_S3_BUCKET_NAME
.Add environment variable
.
Key
type WHITELISTED_DIMENSIONS
and for Value
type your predefined dimensions separated by a space, for example 320x280 640x480 1200x800
. Save
.Setup API Gateway
Choose an API type
spot HTTP API
block and click on Build
.API name
.Review and Create
. (don't worry, you will come back later)Create
.Develop
, click on Routes
.Create
button.GET
and for Route Path /{proxy+}
.Create
.GET
.Route details
should show, click on Attach integration
.Create and attach integration
.Integration target
, Integration type
select Lambda function
.Integration details
, Integration target
select your Lambda function region under AWS Region
and your Lambda function under Lambda function
.Create
.Deploy
, click on Stages
.Create
.Stage details
, enter a stage name. For example production
.Create
.Deploy
button on the top right.Select a stage
.Deploy to stage
.Invoke URL
. Copy it as that's the URL to be used to resize images.Finally, resize images using AWS Lambda!
# Resize using Width and Height.
{api_gatewau_url}/{width}x{height}/{s3_object_path}
OR
{api_gatewauurl}/{width}x{height}{fit}/{s3_object_path}
- URL variables explained:
- `{api_gateway_url}`: The API Gateway URL. Example `https://dh12oluo25.execute-api.us-east-1.amazonaws.com/production`.
- `{width}`: Image width in pixels or 'auto'.
- `{height}`: Image height in pixels or 'auto'.
- `{fit}`: [Sharp.js image fit](https://sharp.pixelplumbing.com/api-resize#resize). Defaults to 'cover'.
- `{s3_object_path}`: Path to image inside S3 Bucket.
Because requirements always differ and there's no one magic size fits all, this simple resizer is designed so you can modify the original script to match your requirements and then rebuild it in order to be published to your own AWS Lambda Function.
This resizer is making use of Sharp to resize image,
and due to Sharp's OS dependencies, you cannot just run npm install
on your local environment
then package and deploy the code to AWS Lambda, unless you are using
Amazon Linux.
That's why the resizer comes packed with Docker, so you can build and deploy to AWS Lambda whenever needed following the below steps:
Download and install Docker and Docker Compose.
Make needed changes inside ./app
folder.
Run (first time only)
docker-compose build
Everytime you want to build your version of the app, run
docker-compose up --build
Note that after the build is complete the container sleeps for 10 minutes for you to copy the build from inside the container to your machine.
Copy the build.
docker cp aws-lambda-image-resizer:/build/build.zip path/to/be/copied/to
Let the container terminate or press ctrl+c to kill it.
Upload .zip build file to your AWS Lambda function.