From 8c07560290ba7c33e9f0bd506eb09e55e889122e Mon Sep 17 00:00:00 2001 From: juliankmazo Date: Tue, 22 Jan 2019 11:52:28 -0500 Subject: [PATCH 1/4] Update `tornado` and `tornado_botocore` to address #awslabs/serverless-image-handler/issues/75 --- source/image-handler/setup.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/source/image-handler/setup.py b/source/image-handler/setup.py index 87e07c532..316f8f3ae 100644 --- a/source/image-handler/setup.py +++ b/source/image-handler/setup.py @@ -3,9 +3,9 @@ from setuptools import setup, find_packages # SO-SIH-156 - 07/16/2018 - Pip version # pip version handling -try: # for pip >= 10 +try: # for pip >= 10 from pip._internal.req import parse_requirements -except ImportError: # for pip <= 9.0.3 +except ImportError: # for pip <= 9.0.3 from pip.req import parse_requirements tests_require = [ @@ -20,7 +20,7 @@ author='Ian Hartz', license='ASL', zip_safe=False, - test_suite = 'tests', + test_suite='tests', packages=['image_handler'], package_dir={'image_handler': '.'}, include_package_data=True, @@ -31,9 +31,9 @@ 'botocore==1.8.0', # SO-SIH-159 - 07/18/2018 - Version and dependencies fix # Locking botocore, pycurl version and moving dependencies from requirements - 'tornado==5.0.1', + 'tornado==5.1.1', 'pycurl==7.43.0.1', - 'tornado_botocore==1.3.0', + 'tornado_botocore==1.5.0', 'requests_unixsocket==0.1.5', 'thumbor==6.5.1', 'thumbor-plugins==0.2.0', @@ -44,8 +44,8 @@ 'opencv-python==3.2.0.6' ], extras_require={ - 'tests': tests_require, - }, + 'tests': tests_require, + }, classifiers=[ 'Programming Language :: Python :: 2.7', ], From fbdc8a65c222908bb0c0c9392ae721bf0d8b63ba Mon Sep 17 00:00:00 2001 From: juliankmazo Date: Tue, 12 Feb 2019 11:13:17 -0500 Subject: [PATCH 2/4] Add docker for making builds --- Dockerfile | 18 +++ deployment/build-s3-dist-docker.sh | 201 +++++++++++++++++++++++++++++ docker-compose.yml | 9 ++ 3 files changed, 228 insertions(+) create mode 100644 Dockerfile create mode 100644 deployment/build-s3-dist-docker.sh create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..21460433c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM centos/python-27-centos7 + +USER root +ENV LD_LIBRARY_PATH=/opt/rh/python27/root/usr/lib64 + +RUN yum-config-manager --enable epel && \ + yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y && \ + yum update -y && \ + yum install git libpng-devel libcurl-devel gcc python-devel libjpeg-devel -y && \ + pip install --upgrade pip==9.0.3 && \ + pip install --upgrade setuptools==39.0.1 && \ + pip install --upgrade virtualenv==15.2.0 && \ + yum install optipng pngcrush gifsicle libjpeg* pngquant ImageMagick-devel \ + nasm autoconf automake libtool -y + +VOLUME /app + +WORKDIR /app/deployment \ No newline at end of file diff --git a/deployment/build-s3-dist-docker.sh b/deployment/build-s3-dist-docker.sh new file mode 100644 index 000000000..b278c242f --- /dev/null +++ b/deployment/build-s3-dist-docker.sh @@ -0,0 +1,201 @@ +#!/bin/bash + + # ./build-s3-dist.sh source-bucket-base-name +# source-bucket-base-name should be the base name for the S3 bucket location where the template will source the Lambda code from. +# The template will append '-[region_name]' to this bucket name. +# For example: ./build-s3-dist.sh solutions +# The template will then expect the source code to be located in the solutions-[region_name] bucket + + # Check to see if input has been provided: +if [ -z "$1" ]; then + echo "Please provide the base source bucket name where the lambda code will eventually reside.\nFor example: ./build-s3-dist.sh solutions" + exit 1 +fi + + err_report() { + echo "Error on line $1" + exit 1 +} + + trap 'err_report $LINENO' ERR + + # Build source +echo "Starting to build distribution" +echo "export deployment_dir=`pwd`" +export deployment_dir=`pwd` +echo "mkdir -p dist" +mkdir -p dist +echo "cp -f serverless-image-handler.template dist" +cp -f serverless-image-handler.template dist +echo "Updating code source bucket in template with $1" +replace="s/%%BUCKET_NAME%%/$1/g" +echo "sed -i -e $replace dist/serverless-image-handler.template" +sed -i -e $replace dist/serverless-image-handler.template + + # SO-SIH-154 - 07/16/2018 - Build fixes +# Adding variable for artifact version +replace="s/%%VERSION%%/$2/g" +echo "sed -i -e $replace dist/serverless-image-handler.template" +sed -i -e $replace dist/serverless-image-handler.template + + echo "Creating UI ZIP file" +cd $deployment_dir/../source/ui +zip -q -r9 $deployment_dir/dist/serverless-image-handler-ui.zip * +echo "Building custom resource package ZIP file" +cd $deployment_dir/dist +pwd +echo "virtualenv --no-site-packages env" +virtualenv --no-site-packages env +echo "source env/bin/activate" +source env/bin/activate +echo "python -m pip install pip==9.0.3" +python -m pip install pip==9.0.3 +# SO-SIH-157 - 07/17/2018 - Pip version +# Checking pip version inside virtualenv for debugging +echo "which python pip virtualenv, version" +which python && python --version +which pip && pip --version +which virtualenv && virtualenv --version + + # Building custom resource zip +echo "pip install -q $deployment_dir/../source/image-handler-custom-resource/. --target=$deployment_dir/dist/env/lib/python2.7/site-packages/" +pip install -q $deployment_dir/../source/image-handler-custom-resource/. --target=$deployment_dir/dist/env/lib/python2.7/site-packages/ +cd $deployment_dir/dist/env/lib/python2.7/site-packages/ +zip -r9 $deployment_dir/dist/serverless-image-handler-custom-resource.zip * +cd $deployment_dir/dist +zip -q -d serverless-image-handler-custom-resource.zip pip* +zip -q -d serverless-image-handler-custom-resource.zip easy* +rm -rf env + + # Building image handler zip +echo "Building Image Handler package ZIP file" +cd $deployment_dir/dist +pwd +echo "virtualenv --no-site-packages env" +virtualenv --no-site-packages env +echo "source env/bin/activate" +source env/bin/activate +echo "python -m pip install pip==9.0.3" +python -m pip install pip==9.0.3 +echo "which python pip virtualenv, version" +which python && python --version +which pip && pip --version +which virtualenv && virtualenv --version + + cd ../.. +pwd + + # SO-SIH-159 - 07/25/2018 - Pycurl ssl backend +# Configuring compile time ssl backend +# https://stackoverflow.com/questions/21096436/ssl-backend-error-when-using-openssl +export PYCURL_SSL_LIBRARY=nss + + # to help with debugging +echo "which curl && curl --version" +which curl && curl --version +echo "which curl-config && curl-config --version" +which curl-config && curl-config --version + + cd $VIRTUAL_ENV +pwd + + # SO-SIH-159 - 07/25/2018 - Curl 7.51.0 +# Installing curl 7.51.0 to keep libcurl link-time and compile-time version same +# Building pycurl against libcurl 7.51.0 resolves the issue +echo "installing curl 7.51.0" +wget https://curl.haxx.se/download/curl-7.51.0.tar.gz +tar -zxvf curl-7.51.0.tar.gz +cd curl-7.51.0 +./configure +make +make install +which curl && curl --version +which curl-config && curl-config --version + + cd $VIRTUAL_ENV +cd ../../.. +pwd +echo "pip install source/image-handler/. --target=$VIRTUAL_ENV/lib/python2.7/site-packages/" +pip install source/image-handler/. --target=$VIRTUAL_ENV/lib/python2.7/site-packages/ + + cd $VIRTUAL_ENV + + + mkdir $VIRTUAL_ENV/bin/lib +cp -f /usr/bin/jpegtran $VIRTUAL_ENV +cp -f /usr/bin/optipng $VIRTUAL_ENV +cp -f /usr/bin/pngcrush $VIRTUAL_ENV +cp -f /usr/bin/gifsicle $VIRTUAL_ENV +cp -f /usr/bin/pngquant $VIRTUAL_ENV +cp -f /usr/lib64/libimagequant.so* $VIRTUAL_ENV/bin/lib + + #building mozjpeg +cd $VIRTUAL_ENV +pwd + + echo 'wget https://github.com/mozilla/mozjpeg/releases/download/v3.2/mozjpeg-3.2-release-source.tar.gz' +wget https://github.com/mozilla/mozjpeg/releases/download/v3.2/mozjpeg-3.2-release-source.tar.gz +tar -zxvf mozjpeg-3.2-release-source.tar.gz +cd mozjpeg +autoreconf -fiv +mkdir build && cd build +sh ../configure --disable-shared --enable-static +make install prefix=/var/task libdir=/var/task +cp -f /usr/lib64/libjpeg.so* $VIRTUAL_ENV/bin/lib +# SO-SIH-170 - 08/15/2018 - mozjpeg path +# mozjpeg executable becomes cjpeg, rectifying path +echo "cp -f /var/task/bin/cjpeg $VIRTUAL_ENV" +cp -f /var/task/bin/cjpeg $VIRTUAL_ENV +#building imgmin +cd $VIRTUAL_ENV +pwd +echo 'git clone https://github.com/rflynn/imgmin.git' +git clone https://github.com/rflynn/imgmin.git +cd imgmin +autoreconf -fi +./configure +make +make install +cd $VIRTUAL_ENV +rm -rf imgmin +cp -f "/usr/local/bin/imgmin" $VIRTUAL_ENV +cp -f /usr/lib64/libMagickWand.so* $VIRTUAL_ENV/bin/lib +cp -f /usr/lib64/libMagickCore.so* $VIRTUAL_ENV/bin/lib +cp -f /usr/lib64/libgomp.so* $VIRTUAL_ENV/bin/lib +cp -f /usr/lib64/libtiff.so* $VIRTUAL_ENV/bin/lib +cp -f /usr/lib64/libXt.so* $VIRTUAL_ENV/bin/lib +cp -f /usr/lib64/libltdl.so* $VIRTUAL_ENV/bin/lib +cp -f /usr/lib64/libjbig.so* $VIRTUAL_ENV/bin/lib +#packing all +cd $VIRTUAL_ENV/lib/python2.7/site-packages +pwd +echo "zip -q -r9 $VIRTUAL_ENV/../serverless-image-handler.zip *" +zip -q -r9 $VIRTUAL_ENV/../serverless-image-handler.zip * +cd $VIRTUAL_ENV +pwd +echo "zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip pngquant" +zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip pngquant +echo "zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip jpegtran" +zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip jpegtran +echo "zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip optipng" +zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip optipng +echo "zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip pngcrush" +zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip pngcrush +echo "zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip gifsicle" +zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip gifsicle +echo "zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip mozjpeg/cjpeg" +zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip cjpeg +echo "zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip imgmin" +zip -q -g $VIRTUAL_ENV/../serverless-image-handler.zip imgmin +cd $VIRTUAL_ENV/bin +pwd +echo "zip -r -q -g $VIRTUAL_ENV/../serverless-image-handler.zip lib" +zip -r -q -g $VIRTUAL_ENV/../serverless-image-handler.zip lib +cd $VIRTUAL_ENV +pwd +cd .. +zip -q -d serverless-image-handler.zip pip* +zip -q -d serverless-image-handler.zip easy* +echo "Clean up build material" +rm -rf $VIRTUAL_ENV +echo "Completed building distribution" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..f49366f71 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3' + +services: + py: + build: + context: . + dockerfile: Dockerfile + volumes: + - .:/app From cff11500ee416a116b5bc05fea144be6bb3e45cb Mon Sep 17 00:00:00 2001 From: juliankmazo Date: Tue, 12 Feb 2019 16:43:57 -0500 Subject: [PATCH 3/4] build the image from the amazon oficial image - [Lambda supported versions](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html) - [Docker repo](https://hub.docker.com/_/amazonlinux/) Also modified the docker file to fix some libraries. A lot of help from https://github.com/awslabs/serverless-image-handler/pull/45 --- .github/.gitignore | 1 + Dockerfile | 64 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 .github/.gitignore diff --git a/.github/.gitignore b/.github/.gitignore new file mode 100644 index 000000000..5c6c64f3d --- /dev/null +++ b/.github/.gitignore @@ -0,0 +1 @@ +deployment/dist \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 21460433c..25be545b3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,53 @@ -FROM centos/python-27-centos7 - -USER root -ENV LD_LIBRARY_PATH=/opt/rh/python27/root/usr/lib64 - -RUN yum-config-manager --enable epel && \ - yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y && \ - yum update -y && \ - yum install git libpng-devel libcurl-devel gcc python-devel libjpeg-devel -y && \ - pip install --upgrade pip==9.0.3 && \ - pip install --upgrade setuptools==39.0.1 && \ - pip install --upgrade virtualenv==15.2.0 && \ - yum install optipng pngcrush gifsicle libjpeg* pngquant ImageMagick-devel \ - nasm autoconf automake libtool -y +# FROM centos/python-27-centos7 +FROM amazonlinux:2017.03.1.20170812 + +# lock yum to the same repository version +RUN sed -i 's/releasever=.*/releasever=2017.03/g' /etc/yum.conf +# base requirements +RUN yum install yum-utils zip -y && \ + yum-config-manager --enable epel && \ + yum install wget git libpng-devel libcurl-devel gcc python27-devel libjpeg-devel -y + +# enable epel on Amazon Linux 2 +RUN yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm + +# ImageMagick +RUN yum install -y ImageMagick-devel + +# Other libraries +RUN yum install -y pngcrush libjpeg* gifsicle + +# optipng +RUN wget http://dl.fedoraproject.org/pub/epel/6/x86_64/Packages/o/optipng-0.7.6-6.el6.x86_64.rpm && \ + yum localinstall optipng-0.7.6-6.el6.x86_64.rpm -y && rm optipng*rpm + +# pngquant +RUN wget http://dl.fedoraproject.org/pub/epel/6/x86_64/Packages/l/libimagequant-2.5.2-5.el6.x86_64.rpm && \ + yum localinstall libimagequant-2.5.2-5.el6.x86_64.rpm -y && rm libimagequant*rpm && \ + wget http://dl.fedoraproject.org/pub/epel/6/x86_64/Packages/p/pngquant-2.5.2-5.el6.x86_64.rpm && \ + yum localinstall pngquant-2.5.2-5.el6.x86_64.rpm -y && rm pngquant*rpm +# pip +RUN alias sudo='env PATH=$PATH' && \ + curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \ + python get-pip.py && rm get-pip.py && \ + pip install --upgrade setuptools && \ + pip install --upgrade virtualenv + +# pycurl +RUN yum install -y nss-devel +ENV PYCURL_SSL_LIBRARY=nss +# USER root +# ENV LD_LIBRARY_PATH=/opt/rh/python27/root/usr/lib64 + +# RUN yum-config-manager --enable epel && \ +# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y && \ +# yum update -y && \ +# yum install git libpng-devel libcurl-devel gcc python-devel libjpeg-devel -y && \ +# pip install --upgrade pip==9.0.3 && \ +# pip install --upgrade setuptools==39.0.1 && \ +# pip install --upgrade virtualenv==15.2.0 && \ +# yum install optipng pngcrush gifsicle libjpeg* pngquant ImageMagick-devel \ +# nasm autoconf automake libtool -y VOLUME /app From 108e8e0af8f603c392601259ec7f17c4c2d0b311 Mon Sep 17 00:00:00 2001 From: juliankmazo Date: Thu, 9 May 2019 20:27:17 -0500 Subject: [PATCH 4/4] Fix metadata endpoint error when auto_webp --- source/image-handler/lambda_function.py | 136 ++++++++++++++---------- 1 file changed, 79 insertions(+), 57 deletions(-) diff --git a/source/image-handler/lambda_function.py b/source/image-handler/lambda_function.py index af0c22e76..debecc38f 100755 --- a/source/image-handler/lambda_function.py +++ b/source/image-handler/lambda_function.py @@ -54,6 +54,8 @@ ############################################################################## # helper methods # + + def response_formater(status_code='400', body={'message': 'error, please check lambda logs'}, cache_control='max-age=120,public', @@ -72,7 +74,8 @@ def response_formater(status_code='400', } if str(os.environ.get('ENABLE_CORS')).upper() == "YES": - api_response['headers']['Access-Control-Allow-Origin'] = os.environ.get('CORS_ORIGIN') + api_response['headers']['Access-Control-Allow-Origin'] = os.environ.get( + 'CORS_ORIGIN') # SO-SIH-175 - 08/28/2018 - Missing header # Adding missing header to response @@ -94,8 +97,9 @@ def response_formater(status_code='400', logging.debug('api response: %s' % (api_response)) return api_response + def auto_webp(original_request, request_headers): - headers = {'Accept':'*/*'} + headers = {'Accept': '*/*'} vary = bool(strtobool(str(config.AUTO_WEBP))) if vary: if original_request.get('headers'): @@ -105,17 +109,20 @@ def auto_webp(original_request, request_headers): # SO-SIH-166 - 08/08/2018 - Enabling safe url # Encoding url and hashing with security key + + def encoding_string(string): """ Encoding URL per RFC 3986. """ - return(quote(string,safe='')) + return(quote(string, safe='')) + def signed_url(secret_key, string_to_sign): """ Signing URL with security key """ - hashed = hmac.new(secret_key,string_to_sign, sha1) + hashed = hmac.new(secret_key, string_to_sign, sha1) return base64.b64encode(hashed.digest()) @@ -133,6 +140,7 @@ def rewrite(http_path): http_path = lambda_rewrite.match_patterns(http_path) return http_path + def gen_body(ctype, content): """ Convert image to base64 to be sent as body response. @@ -148,6 +156,7 @@ def gen_body(ctype, content): logging.error('gen_body trace: %s' % traceback.format_exc()) return None + def send_metrics(event, result, start_time): """ Send anonymous usage metrics to AWS. @@ -162,6 +171,8 @@ def send_metrics(event, result, start_time): ############################################################################## # server methods # + + def run_server(application, context): server = HTTPServer(application) define( @@ -173,6 +184,7 @@ def run_server(application, context): server.add_socket(socket) server.start(1) + def stop_thumbor(): return None tornado.ioloop.IOLoop.instance().stop() @@ -181,6 +193,7 @@ def stop_thumbor(): except OSError as error: logging.error('stop_thumbor error: %s' % (error)) + def start_thumbor(): """ Runs thumbor server with the specified arguments. @@ -197,7 +210,8 @@ def start_thumbor(): global config # SO-SIH-167 - 08/08/2018 - Allowing environment variable # Passing environment variable flag to server parameters - config = get_config(thumbor_config_path, server_parameters.use_environment) + config = get_config(thumbor_config_path, + server_parameters.use_environment) configure_log(config, server_parameters.log_level) importer = get_importer(config) os.environ["PATH"] += os.pathsep + '/var/task' @@ -207,9 +221,9 @@ def start_thumbor(): run_server(application, thumbor_context) tornado.ioloop.IOLoop.instance().start() logging.info( - 'thumbor running at %s:%d' % + 'thumbor running at %s:%d' % (thumbor_context.server.ip, thumbor_context.server.port) - ) + ) return config except RuntimeError as error: if str(error) != "IOLoop is already running": @@ -220,12 +234,14 @@ def start_thumbor(): logging.error('start_thumbor error: %s' % (error)) logging.error('start_thumbor trace: %s' % traceback.format_exc()) + def start_server(): t = threading.Thread(target=start_thumbor) t.daemon = True t.start() return t + def restart_server(): threads = threading.enumerate() main_thread = threading.current_thread() @@ -238,29 +254,32 @@ def restart_server(): ############################################################################## # request processing methods # + + def is_thumbor_down(): - if not os.path.exists(thumbor_socket): - start_server() - session = requests_unixsocket.Session() - http_health = '/healthcheck' - retries = 10 - while(retries > 0): - try: - response = session.get(unix_path + http_health) - if (response.status_code == 200): - break - except Exception as error: - time.sleep(0.03) - retries -= 1 - continue - if retries <= 0: - logging.error( - 'call_thumbor error: tornado server unavailable,\ + if not os.path.exists(thumbor_socket): + start_server() + session = requests_unixsocket.Session() + http_health = '/healthcheck' + retries = 10 + while(retries > 0): + try: + response = session.get(unix_path + http_health) + if (response.status_code == 200): + break + except Exception as error: + time.sleep(0.03) + retries -= 1 + continue + if retries <= 0: + logging.error( + 'call_thumbor error: tornado server unavailable,\ proceeding with tornado server restart' - ) - restart_server() - return response_formater(status_code='502') - return False, session + ) + restart_server() + return response_formater(status_code='502') + return False, session + def request_thumbor(original_request, session): """ @@ -271,7 +290,7 @@ def request_thumbor(original_request, session): http_path = original_request['path'] logging.debug('original_request path: %s' % (http_path)) try: - http_path = rewrite(http_path); + http_path = rewrite(http_path) logging.debug('http path after rewrite: %s' % (http_path)) http_path = true_url(http_path) except Exception as error: @@ -280,34 +299,36 @@ def request_thumbor(original_request, session): vary, request_headers = auto_webp(original_request, request_headers) return session.get(unix_path + http_path, headers=request_headers), vary + def process_thumbor_responde(thumbor_response, vary): - if thumbor_response.status_code != 200: - return response_formater(status_code=thumbor_response.status_code) - if vary: - vary = thumbor_response.headers['vary'] - content_type = thumbor_response.headers['content-type'] - body = gen_body(content_type, thumbor_response.content) - # SO-SIH-173 - 08/20/2018 - Lambda payload limit - # Lambda limits to 6MB of response payload - # https://docs.aws.amazon.com/lambda/latest/dg/limits.html - content_length = int(thumbor_response.headers['content-length']) - logging.debug('content length: %s' % (content_length)) - if (content_length > 6000000): + if thumbor_response.status_code != 200: + return response_formater(status_code=thumbor_response.status_code) + if vary and 'vary' in thumbor_response.headers: + vary = thumbor_response.headers['vary'] + content_type = thumbor_response.headers['content-type'] + body = gen_body(content_type, thumbor_response.content) + # SO-SIH-173 - 08/20/2018 - Lambda payload limit + # Lambda limits to 6MB of response payload + # https://docs.aws.amazon.com/lambda/latest/dg/limits.html + content_length = int(thumbor_response.headers['content-length']) + logging.debug('content length: %s' % (content_length)) + if (content_length > 6000000): return response_formater(status_code='500', body={'message': 'body size is too long'}, ) - if body is None: - return response_formater(status_code='500', - cache_control='no-cache,no-store') - return response_formater(status_code='200', - body=body, - cache_control=thumbor_response.headers['Cache-Control'], - content_type=content_type, - expires=thumbor_response.headers['Expires'], - etag=thumbor_response.headers['Etag'], - date=thumbor_response.headers['Date'], - vary=vary - ) + if body is None: + return response_formater(status_code='500', + cache_control='no-cache,no-store') + return response_formater(status_code='200', + body=body, + cache_control=thumbor_response.headers['Cache-Control'], + content_type=content_type, + expires=thumbor_response.headers['Expires'], + etag=thumbor_response.headers['Etag'], + date=thumbor_response.headers['Date'], + vary=vary + ) + def call_thumbor(original_request): thumbor_down, session = is_thumbor_down() @@ -316,6 +337,7 @@ def call_thumbor(original_request): thumbor_response, vary = request_thumbor(original_request, session) return process_thumbor_responde(thumbor_response, vary) + def lambda_handler(event, context): """ Main event handler, calls thumbor with received event. @@ -325,10 +347,10 @@ def lambda_handler(event, context): global log_level log_level = str(os.environ.get('LOG_LEVEL')).upper() if log_level not in [ - 'DEBUG', 'INFO', - 'WARNING', 'ERROR', - 'CRITICAL' - ]: + 'DEBUG', 'INFO', + 'WARNING', 'ERROR', + 'CRITICAL' + ]: log_level = 'ERROR' logging.getLogger().setLevel(log_level)