Cloud Computing is where the market is migrating. Therefore, we’re here to build an application by leveraging the power of cloud services and see how efficiently we can monitor it.
What Is AWS Lambda?
AWS Lambda is a serverless compute service that triggers the execution of code in response to events. The code that executes on AWS Lambda is called a lambda function and allows the user to focus on code without having to worry about the backend, infrastructure, and scalability. It can also extend the capability of various AWS services, meaning, for example, it can work as an API backend serving incoming requests from API Gateways.
What Is Flask?
Flask is the popular microweb framework based on Werkzeug and Jinja 2, particularly written in Python. It is also known as a lightweight WSGI (Web Server Gateway Interface) web framework and is used to deploy a number of applications ranging from simple to complex. It is relatively simple, easy to use, and well documented. Available under a BSD license, it’s also actively developed and maintained. Flask is aimed at building web applications as well as web APIs and is extensible, hence we can plug in any ORM.
We will build a simple Python Flask application by leveraging the power of AWS cloud services such as Lambda function, Layers, and EC2 instances. The goal of the application is to convert color images (RGB) to grayscale images using a web API and serverless compute. So we will use Flask to create the web API endpoint, and our logic will reside in a lambda function. Finally, we will monitor our API as well as the serverless Lambda function using Epsagon’s insightful dashboard. Below is the architecture diagram of the application that we are going to build with AWS Lambda Flask.
While developing this application, you will interact with AWS services such as S3 bucket and AWS Lambda.
Keeping the architecture diagram in mind, create an S3 bucket with two directories: colorImage and grayscaleImage. You can then set a trigger on colorImage, and the output will be stored in grayscaleImage. Let’s name the bucket epsagon-image-process.
Deploying an OpenCV Layer
Your first step will be to create layers since we will require OpenCV as an external package for image processing. So, go ahead and create Lambda layer for OpenCV. You use an EC2 instance to create a package for OpenCV and upload it to S3 bucket. For this, you’ll have to create an IAM role. Navigate to IAM management console, and create a new role for EC2 service with AmazonS3FullAccess permissions. Give an appropriate name (e.g. ec2_s3_access). We will use the name “ec2_s3_access” for the purpose of this article.
To create Lambda Layers, you’ll need a package in a zip file, which you will create in the next step. Before moving on to the next step, you can create the S3 bucket or use an existing bucket (e.g. epsagon-opencv-layer) to store the package.
After creating the S3 bucket, navigate to EC2 Management Console and spin up a t2.micro instancewith Ubuntu Server 18.04 LTS operating system. In step three of creating an instance, make sure to assign the IAM role (ec2_s3_access) that we created in the first step of Deploying OpenCV layer. The rest of the configuration for spinning up an instance remains unchanged.
After spinning up the instance, SSH to connect to the instance. You can then execute a series of commands to create a package and upload it to the S3 bucket (epsagon-opencv-layer).
sudo apt-get update python3 -V # to check the version of Python since we are creating the layers for Python 3.6 runtime sudo apt-get install python3-pip # installing Python package manager mkdir -p build/python/lib/python3.6/sites-packages # creating the directory structure pip3 install opencv-python -t build/python/lib/python3.6/sites-packages/ –system # installing OpenCV package/module in the custom directory, i.e. sites-packages cd build zip -r package.zip . # creating package in the form of zip file aws s3 cp package.zip s3://epsagon-opencv-layer # uploading package to S3 bucket
Once the upload is finished, you will navigate to the Lambda Management Console -> Layers -> Create Layers. The configuration looks like this.
Click on Create Layer, and now you can terminate the instance.
Setting Up a Lambda Function
A lambda function is where our logic resides. But before creating the lambda function, you have to create an IAM role. So, navigate to the IAM management console, and create a new role for Lambda service with AWSLambdaExecute permissions. Make sure to give an appropriate name (e.g. lambda_role). We will use the name “lambda_role” for the purpose of this article.
To create the function, navigate to Lambda Management console -> Functions -> Create Function. You will select “Author from scratch” and fill in the basic information.
Note: Make sure to increase the Memory of your lambda function to 1024 MB and the timeout field to five minutes.
For the next step, you’ll need to bind opencv-layer with our lambda function. From the designer pane, click on Layers -> “Add layer to function.” Select the layer and its version and click on Add.