An AWS Lambda Function to resize images automatically with API Gateway and S3 for imagemagick tasks. When an image is called on AWS Api Gateway, this package will resize it and send it to the S3.
- Node.js - AWS Lambda supports versions of 8.10 or above.
It's AWS Lambda, which is a compute service that lets you run code without provisioning or managing servers.
The combination of API Gateway and Lambda is very powerful. It allows you to build some complex functionalities without maintaining any virtual machines yourself. Lambda can be hooked up to many other (AWS) Services including S3. That's why I decided to build an AWS Lambda Function to resize images automatically with API Gateway and S3 for imagemagick tasks. When an image is called on AWS S3 bucket (via API Gateway), this package will resize it and send it to the S3 before redirecting you to the new path of the image (aws bucket url or CDN).
- Use Serverless Framework
- Use Serverless Webpack
- Use Serverless Offline
- The image conversion endpoint by API Gateway or cloudfront URL.
First, add Serverless globally:
npm install -g serverless
Then, clone the repository into your local environment:
git clone https://github.com/apoca/lambda-resize-image
cd lambda-resize-image
npm install
Modify and set the .env.example to env.yml with your data. (You are supposed to have already configured your local variables)).
To run local development you also might need to configure you aws credentials, or you can set them to what I've shown below.
You can also check if you have everything installed in the correct way:
$ serverless
To deploy from your environment to Amazon AWS, you must:
$ serverless deploy
Then, serverless will package, validate and upload your stack to Amazon AWS. It will probably look like this:
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (1.52 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: resizeS3Image
stage: development
region: eu-west-1
stack: resizeS3Image-development
api keys:
None
endpoints:
GET - https://<api_key_here>.execute-api.eu-west-1.amazonaws.com/development/{any+}
functions:
handler: resizeS3Image-development-handler
Serverless: Removing old service artifacts from S3...
When upload to AWS Lambda, the project will bundle only needed files - no dev dependencies will be included.
To restrict the dimensions, we put a const var
in the handle.js
:
const ALLOWED_DIMENSIONS = {
width: 1800,
height: 1800
};
You can also change the url endpoint https://<api_key_here>.execute-api.eu-west-1.amazonaws.com/development/{any+}
to another one more tiny and cachable (cloudfront), you can also configure in you Api Gateway (lambda service) a Custom Domain Name.
https://api.yourdomain.com/<KEY_S3_IMAGE_HERE>?width=<WIDTH>&height=<HEIGHT>
-
URL - AWS URL S3 bucket or your CDN url to the BUCKET. (required)
-
BUCKET - AWS S3 bucket. (required)
- AWS_ACCESS_KEY_ID - AWS key
- AWS_SECRET_ACCESS_KEY - AWS Secret
$ npm i -g serverless
$ npm i
brew install imagemagick
$ serverless offline start
Note that you will need to be into the root repository. The last command (4.) will spin up an serverless-offline version of an API Gateway, that will simulate the real one. Once it is running, you can see all the requests on your command line.
http://localhost:3000<YOUR_KEYNAME_TO_IMAGE>?width=<WIDTH>&height=<HEIGHT>
Parameter | type | description |
---|---|---|
width | Integer | Required. Will resize image via imagemagic resize. |
height | Integer | Optional. Will resize image via imagemagic crop. |
We'd love to get feedback on how you're using lambda-resize-image and things we could add to make this tool better. Feel free to contact us at [email protected]
If you'd like to contribute to the project, feel free to submit a PR. See more: CODE_OF_CONDUCT.md
This project is licensed under the MIT License - see the LICENSE.md file for details
- Miguel Vieira - Initial work - apoca
See also the list of contributors who participated in this project.