Skip to content
Mohammed edited this page Jun 12, 2019 · 2 revisions

Welcome to the serverless-imageCompression wiki!

The compression of the images is done using the tinify API. You will require an API key which can be obtained from their website.

How to use:

  1. Assuming you already have an AWS account, create two S3 buckets:
    • One that will be used for uploading your unoptimized files to S3.
    • The other that will be the destination bucket ie. the bucket where you want your optimized files to be stored.
  2. Download the package 'imgPackage' from this repo and unzip it.
  3. After unzip, find and open the file 'imgtinify'. Replace the value of the variables destination_bucket with your bucket name you created earlier, and tinify.key with your key (maintain quotes for both values). Ensure not to add/remove unintended spaces as you may leave the script unexecutable.
  4. Re-zip the file as before.
  5. Head to AWS IAM at the AWS console. (if you already have an IAM role that has full S3 access then skip to step 7)
  6. Create a new role and attach the policy having full S3 access to it.
  7. Go to AWS Lambda via console. Create function -> (With 'Author from scratch' already selected) Enter Function name, choose Run time as Python 3.7, choose IAM role having S3 access -> Hit Create function.
  8. Scroll down to 'Function code'. Choose upload zip from drop-down and select the packaged file from step 4. Hit save on top right corner.
  9. Under 'Designer' panel, from the list of triggers select S3, scroll down to 'Configure triggers'. Choose the bucket name you created at step 1 (this is the bucket where you will upload the files for optimizing). Other settings will remain as is.
  10. Hit Add. That's it!

PS: Tinify allows optimization for the first 500 images per month. Pricing for beyond that number is shown at their website. Also, the API provided by tinify allows other features such as smart cropping, image resizing and cut out imaging which is quite good if such features required. These things can be quite easily incorporated into the script if needed.

Clone this wiki locally