Serverless Image Handler
Serverless Image Handler

Appendix B: Rewrite Feature

This feature allows customers to migrate their current image request model to the Serverless Image Handler solution, without changing their applications to accommodate new image URLs. To use this feature, modify the solution’s rewrite feature settings.


If you have already deployed the Serverless Image Handler solution and want to use the rewrite feature, you must redeploy the AWS CloudFormation stack.

In the AWS Lambda console, select the solution’s primary Lambda function and set the REWRITE_ENABLED environment variable to YES. Then, in the REWRITE_PATTERNS environment variable, add your rewrite rules. The list of rules must be comma separated and wrapped in square brackets. Each rule must be wrapped in parentheses and contain regular expressions composed of two strings: one for the match condition and one for substitution. Note that the regex must be compatible with Python. For example:

[(r'^/string_to_match$', r'rewritten_string')]

The solution checks to see if the rewrite feature is enabled before it sends the request to Thumbor. If it is enabled, the rewrite feature parses any customer defined rules in order until it finds a match, and uses the provided patterns to transform the image while preserving the originally requested URL. If no match is found, the original unmodified image request is sent to Thumbor.

Below are REWRITE_PATTERNS examples:

  • This example matches a string (‘-zoom’) in a requested image name in the /s/ folder, and returns a 630x630 resized image of the original file located in the /s/ folder (notice that the original file name is appended with the string ‘-zoom’). This example demonstrates the ability to create identifiers in the requested URL that map to different image filters.

Original Path /s/0002fe146de274f90ae005346834e40c-zoom.jpg

Match pattern r'^/([s])/([0-9a-f]+)-(zoom)\.([a-zA-Z0-9]+)$'

Replace pattern r'/fit-in/630x630/s/\2.\4'

Rewritten request /fit-in/630x630/s/0002fe146de274f90ae005346834e40c.jpg

This example would be implemented by modifying the following Lambda function environment variables:


REWRITE_PATTERNS [(r'^/([s])/([0-9a-f]+)-(zoom)\.([a-zA-Z0-9]+)$', r'/fit-in/630x630/s/\2.\4')]

  • This example builds on the previous example; however, it also matches requests from the /p/ folder and rewrites the request to retrieve the original image from the S3 origin’s /product/ folder.

Original Path /p/0002fe146de274f90ae005346834e40c-zoom.jpg

Match pattern r'^/([p])/([0-9a-f]+)-(zoom)\.([a-zA-Z0-9]+)$'

Replace pattern r'/fit-in/630x630/product/\2.\4'

Rewritten request /fit-in/630x630/product/0002fe146de274f90ae005346834e40c.jpg

This example would be implemented by modifying the following Lambda function environment variables:


REWRITE_PATTERNS [(r'^/([p])/([0-9a-f]+)-(zoom)\.([a-zA-Z0-9]+)$', r'/fit-in/630x630/product/\2.\4')]

  • This example shows how you can create a comma-separated list of patterns to specify multiple rewrite rules. Notice how the REWRITE_PATTERNS environment variable value begins and ends with brackets, and each rule is wrapped in parentheses. Additionally, we recommended placing your most frequently used patterns at the beginning of the list to increase rule processing performance.

[(r'^/([s])/([0-9a-f]+)-(zoom)\.([a-zA-Z0-9]+)$', r'/fit-in/630x630/sample/\2.\4'),(r'^/([s])/([0-9a-f]+)-(product)\.([a-zA-Z0-9]+)$', r'/fit-in/220x220/sample/\2.\4'),(r'^/([s])/([0-9a-f]+)-(cart)\.([a-zA-Z0-9]+)$', r'/fit-in/53x53/sample/\2.\4'),(r'^/([s])/([0-9a-f]+)-(catalog)\.([a-zA-Z0-9]+)$', r'/fit-in/120x120/sample/\2.\4'),(r'^/([s])/([0-9a-f]+)-(gallery)\.([a-zA-Z0-9]+)$', r'/fit-in/33x33/sample/\2.\4'),(r'^/([s])/([0-9a-f]+)-(list)\.([a-zA-Z0-9]+)$', r'/fit-in/92x92/sample/\2.\4')]