Flask Lambda Lab – Lesson 4

Lesson Overview
In this lesson, we will expand our application by registering a trigger that calls a lambda function each time a file is POSTed to our S3 uploads bucket. The triggered function will create a thumbnail image from the uploaded image and store the thumbnail in the thumbnail bucket. Finally, the function will store the URL to the thumbnail image in the uploaded image’s metadata, so that our gallery application knows where to find the thumbnail.


  1. Checkout `lesson3` branch of our flask app from github

    git checkout lesson3 -f
  2. Install the python Image library into our virtual environment
    • Zappa automatically includes any python libraries installed in the virtualenv into the manifest of packages required for our code to run in lambda. So, by installing the Image library in our virtualenv it will automatically be included when we next `zappa deploy` or `zappa update`
    • 2018-01-15 update: The Python “Image” library requires django. By default, pip installs the latest version of any dependencies. As of this writing, django 2.0 only supports python 3.4+ and this lab happens to be running python 2.7. To ensure that we install a have a python 2.7 compatible version of django, we’ll first specify that before we install the Image library.

      pip install 'Django<2.0'
      pip install Image
  3. The configure the trigger event in the zappa_settings.json
    • To keep this lab speedy, we created a script that modifies the zappa_settings.json file with the “event” (trigger) information.
    • Just run the command below and let it configure the trigger 



    • The add_s3_trigger.py script adds the “events” section to the zappa_settings.json file, which sets up the lambda trigger

        "dev": {
          "app_function": "app.__init__.app",
          "project_name": "flask-lambda-la",
          "s3_bucket": "zappa-vhek65olq",
          "aws_region": "us-east-1",
          "profile_name": "default",
          "runtime": "python2.7",
          "events": [
              "function": "app.events.s3_uploadTrigger",
              "event_source": {
                "events": [
                "arn": "arn:aws:s3:::flasklambdalab-dev-uploads-6e238036"
  4. Re-deploy the application into lambda
    • So, we installed the Image library and configured a trigger in the zappa_settings.py file. Now all that’s left to do is publish the updates to lambda
      zappa update dev
  5. Test access to web application via API gateway – verify
    • Browser: …lambda
    • Upload an image file (or re-upload an image you’ve already uploaded)
    • Check out gallery
    • Confirm that the thumbnail trigger worked
  6. Check out one of the uploaded image’s metadata now!
    • Notice that there are 2 metadata attributes, one is the thumbnailUrl
  7. We set this attribute with our thumbnail lambda function and use it in the web application

Lesson Summary

In this lesson, we:
  1. Updated our application from the lesson 3 branch of code
  2. Installed the required Image library to create thumbnails
  3. Defined an event (aka trigger) in the zappa_settings.json file
  4. Redeployed our code the lambda
  5. Validated that files uploaded now automatically create thumbnails
  6. We looked at the uploaded file metadata and validated thumbnailUrl attribute
If our application were to go mainstream, and we received millions of uploads per minute, guess what, our lambda trigger would effortlessly scale to support creating millions of thumbnails concurrently! Isn’t that cool?
In the Lesson 5, we’ll clean up all these resources and make some suggestions for where to go next