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.
- User requests an image from the API Gateway.
- API Gateway triggers the Lambda Function.
- Lambda function runs basic validations on user input.
- The function checks if a resized image exists on S3 Bucket.
- If a resized image exists, image returned as a response to the API Gateway.
- If no resized image found, a new resized option is created on the fly and saved to S3 Bucket.
- Resized image is returned as a response to the API Gateway.
- AWS Console root access.
- OR, AWS Console full access to the four AWS Services above.
-
Setup S3 Bucket.
- Open AWS Console and go to S3 Home.
- Click on
Create bucket
button. - Choose a name and a region for your new bucket.
- Keep a note of the bucket name, we will need it later.
- Uncheck
Block all public access
option. - Click on
Create bucket
.
-
Setup IAM Role.
- Go to IAM Home, then under
Access management
click onRoles
. - Click on
Create role
button. - Under
Select type of trusted entity
selectAWS Service (EC2, Lambda and others)
. - Under
Choose a use case
selectLambda
. - Click on
Next: Permissions
. - Click on
Create policy
. - Switch to JSON tab, then copy and paste the below.
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Resource": "arn:aws:s3:::YOUR_S3_BUCKET_NAME/*", "Action": [ "s3:GetObject", "s3:PutObject" ] } ] }
- Don't forget to replace
YOUR_S3_BUCKET_NAME
with your created S3 Bucket Name. - Click on
Next: Tags
. - Click on
Next: Review
. - Click on
Review ploicy
. - Set a name for the new ploicy and click
Create policy
. - Go back to Create IAM Role page, hit the refresh button, search for and select the policy you just created.
- Click on
Next: Tags
. - Click on
Next: Review
. - Enter a name for the new role and click
Create role
. - (Optional) if you would like to log API Gateway calls to Amazon Cloudwatch
- Click to edit the role you just created.
- Click on
Attach policies
. - Type and select
AmazonAPIGatewayPushToCloudWatchLogs
from the Filter policies search box. - Click
Attach policy
.
- Go to IAM Home, then under
-
Setup Lambda Function
- Go to Lambda Home.
- Click on
Create function
. - Under
Choose one of the following options to create your function
chooseAuthor from scratch
. - For Runtime, choose
Node.js 12.x
. - Under permissions, choose
Use an existing role
, then from roles list select the role you just created. - Click on
Create function
. - Under
Function code
section, click to expandActions
dropdown the clickUpload a .zip file
. - Download the Build file and upload it as your Lambda function source code.
- Under
Environment variables
section, clickEdit
. - Click
Add environment variable
. - For
Key
typeBUCKET
and forValue
paste inYOUR_S3_BUCKET_NAME
. - (Optional) If you would like to restrict resizing images to predefined dimensions, click
Add environment variable
.- For
Key
typeWHITELISTED_DIMENSIONS
and forValue
type your predefined dimensions separated by a space, for example320x280 640x480 1200x800
.
- For
- Click
Save
.
-
Setup API Gateway
- Go to API-Gateway APIs.
- Under
Choose an API type
spotHTTP API
block and click onBuild
. - Type your API name under
API name
. - Click on
Review and Create
. (don't worry, you will come back later) - Click on
Create
. - From the menu on the left, under
Develop
, click onRoutes
. - Click on
Create
button. - For Method choose
GET
and for Route Path/{proxy+}
. - Click on
Create
. - Now you should see the route you created under Routes Block. Click on the Method you just created
GET
. Route details
should show, click onAttach integration
.- Click on
Create and attach integration
. - Under
Integration target
,Integration type
selectLambda function
. - Under
Integration details
,Integration target
select your Lambda function region underAWS Region
and your Lambda function underLambda function
. - Click on
Create
. - From the menu on the left, under
Deploy
, click onStages
. - Click on
Create
. - Under
Stage details
, enter a stage name. For exampleproduction
. - Click on
Create
. - Click on
Deploy
button on the top right. - Select the stage you just created from under
Select a stage
. - Click on
Deploy to stage
. - API URL should now appear under
Invoke URL
. Copy it as that's the URL to be used to resize images.
-
Finally, resize images using AWS Lambda!
- Resized image URL structure:
# Resize using Width and Height. {api_gatewau_url}/{width}x{height}/{s3_object_path} # example: https://dh12oluo25.execute-api.us-east-1.amazonaws.com/production/180x120/image.png # example: https://dh12oluo25.execute-api.us-east-1.amazonaws.com/production/640x480/path/to/image.jpeg
OR
# Resize using Width, Height and Fit. {api_gatewau_url}/{width}x{height}_{fit}/{s3_object_path} # example: https://dh12oluo25.execute-api.us-east-1.amazonaws.com/production/180x120_fill/image.png # example: https://dh12oluo25.execute-api.us-east-1.amazonaws.com/production/640x480_inside/another/path/to/image.jpg
- URL variables explained:
{api_gateway_url}
: The API Gateway URL. Examplehttps://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. 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.
- SagidM's s3-resizer.