Grid Solutions
Published in

Grid Solutions

Deploy Container image on AWS lambda

Lambda is a serverless, Event-driven computing service created by Amazon that lets us run code for virtually any type of application. And many companies have started considering lambda as we can use it for small and large-scale applications.

Here, I will explain how to deploy a simple OCR application on AWS lambda. We will be using a very well-known open-source library (Tesseract).

Lets write python code to download file from s3 to container or upload to s3. And save as

import tempfile
import boto3
import os
import logging
import json

s3_client = boto3.client('s3')

def download_from_s3(bucket, object_name):
tempfile_path = os.path.join(tempfile.gettempdir(), object_name.split('/')[-1])

s3_client.download_fileobj(bucket, object_name, open(tempfile_path, 'wb'))
return tempfile_path
except Exception as e:
return None

def upload_to_s3(bucket, key, filename):
print(f"Uploading bucket {bucket} file {filename} as {key}")
s3_client.upload_fileobj(open(filename, "rb"), bucket, key)
return True
except Exception as e:
return False

Now we need to write logic, which would take image as input and extract characters.

def main(event, context):
BUCKET_NAME = event["Records"][0]["s3"]["bucket"]["name"]
query_key = event["Records"][0]["s3"]["object"]["key"]

image_path = download_from_s3(BUCKET_NAME, query_key)
if image_path is None:
raise Exception(f"Image {query_key} doesn't exist in bucket {BUCKET_NAME}")
text = pytesseract.image_to_string(
print(text) #replace with your business logic

Lets create a docker container. Use the following code to create Dockerfile.

LABEL problem="OCR"

RUN yum upgrade -y
RUN yum install mesa-libGL -y
RUN pip install pip --upgrade
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
RUN rpm -Uvh
RUN yum -y update

RUN yum -y install tesseract


CMD ["handler.main"]

Now lets create container repository on AWS to store docker images. To create repository you need to follow following steps

  1. Search “Elastic Container registry” and click on repository
  2. Click on “create repository” or you can use existing repository you have
  3. Give the proper name and click create repository

After creating you can open it by clicking it. And you will be able to view push commands. By following the command you will be able to push your docker image to the repository.

Now you can create a function, while creating function, click in Container Image section.

Now you can pass function name and choose container images by clicking Browse images button. After you are done, next thing we need to do is to select trigger to run lambda when we need. In this implementation we need to run the process after file is uploaded to s3.

After creating trigger, we need to give permission to lambda to read s3 contents. For this you need to go to configuration and then permissions. Under the section execution role, click on “Role name”.

Now you need to give permission to lambda, for simplicity I have given full access, which should look like this.

Its time to test our application, while adding trigger, you might or not have selected prefix and suffix. If you haven’t, upload image anywhere in bucket and you will see result as your business logic. I will also upload Terraform code for this implementaton in future.




Grid Solutions Tech Blog

Recommended from Medium

Face Tracking With the Ryze Tello, Part 2: Control System for Tracking Objects

TWA (Trusted Web Activities) at QuintoAndar

A screenshot of a device executing QuintoAndar’s application

Java Concepts: What everyone knows Vs What you should know? — Part 3B

Introduction to Python for Testers

The Unreasonable Effectiveness of Loading Test Data Automatically

The story of my personal Site.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sapkota Bikash

Sapkota Bikash

More from Medium

Autoscale on AWS with EC2, Python, Flask, and Nginx Part2

How to run AWS Lambda on your computer using Docker containers

Deploy Containerized Serverless Flask to AWS Lambda

Deploy your FastAPI API to AWS EC2 using Nginx