Tesseract OCR Lambda Layer
AWS Lambda layer containing the tesseract OCR libraries and command-line binary for Lambda Runtimes running on Amazon Linux 2023 and 2.
:warning: DEPRECATION NOTICE:
- Amazon Linux 1 (AL1): Removed. No longer supported.
- Amazon Linux 2 (AL2): Deprecated. Will be removed after 6 months. New projects should use Amazon Linux 2023 (AL2023).
- Note: AL2 with Tesseract 5.5+ is not supported in CI due to GCC 7.3.1 lacking C++17 filesystem support. Users can build locally with Tesseract 5.4.x or earlier if AL2 is required.
- Recommended: Use Amazon Linux 2023 (AL2023) for all new projects.
Quickstart
This repo comes with ready-to-use binaries compiled against the AWS Lambda Runtimes (based on Amazon Linux 2023 and 2).
Example Projects in Python 3.12 and Node.js 20 using Serverless Framework and CDK are provided:
## Demo using Serverless Framework and prebuilt layer
cd example/serverless
npm ci
npx sls deploy
## or ..
## Demo using CDK and prebuilt layer
cd example/cdk
npm ci
npx cdk deploy
Ready-to-use binaries
For compiled, ready to use binaries that you can put in your layer see ready-to-use, or check out the latest release.
See examples for some ready-to-use examples.
Use with Serverless Framework
Serverless Framework
Reference the path to the ready-to-use layer contents in your serverless.yml:
service: tesseract-ocr-layer
provider:
name: aws
# define layer
layers:
tesseractAl2:
# and path to contents
path: ready-to-use/amazonlinux-2
compatibleRuntimes:
- python3.8
functions:
tesseract-ocr:
handler: ...
runtime: python3.8
# reference layer in function
layers:
- { Ref: TesseractAl2LambdaLayer }
events:
- http:
path: ocr
method: post
Deploy
npx sls deploy
Use with AWS CDK
AWS CDK
Reference the path to the layer contents in your constructs:
const app = new App();
const stack = new Stack(app, 'tesseract-lambda-ci');
const al2Layer = new lambda.LayerVersion(stack, 'al2-layer', {
// reference the directory containing the ready-to-use layer
code: Code.fromAsset(path.resolve(__dirname, './ready-to-use/amazonlinux-2')),
description: 'AL2 Tesseract Layer',
});
new lambda.Function(stack, 'python38', {
// reference the source code to your function
code: lambda.Code.fromAsset(path.resolve(__dirname, 'lambda-handlers')),
runtime: Runtime.PYTHON_3_8,
// add tesseract layer to function
layers: [al2Layer],
memorySize: 512,
timeout: Duration.seconds(30),
handler: 'handler.main',
});
Build tesseract layer from source using Docker
You can build layer contents manually with the provided Dockerfiles.
Build layer using your preferred Dockerfile:
## build (using AL2023 - recommended)
docker build -t tesseract-lambda-layer -f Dockerfile.al2023 .
## run container
export CONTAINER=$(docker run -d tesseract-lambda-layer false)
## copy tesseract files from container to local folder layer
docker cp $CONTAINER:/opt/build-dist layer
## remove Docker container
docker rm $CONTAINER
unset CONTAINER
available Dockerfiles
| Dockerfile |
Base-Image |
compatible Runtimes |
Status |
Dockerfile.al2023 (recommended) |
Amazon Linux 2023 |
Python 3.12+, Node.js 20+, Ruby 3.2+, Java 17+ |
✅ Active |
Dockerfile.al2 |
Amazon Linux 2 |
Python 3.8-3.11, Node.js 18, Ruby 2.7, Java 8/11 |
⚠️ Deprecated |
Dockerfile.al1 |
Amazon Linux 1 |
Python 2.7/3.6/3.7, Ruby 2.5, Java 8, Go 1.x |
❌ Removed |
Building a different tesseract version and/or language
By default, the build generates Tesseract 5.5.2 OCR libraries with the fast german, english and osd (orientation and script detection) data files included.
The build process can be modified using different build time arguments (defined as ARG in Dockerfile.al2 and Dockerfile.al2023), using the --build-arg option of docker build.
Example of custom build
## Build with French language support (recommended)
docker build --build-arg OCR_LANG=fra -t tesseract-lambda-layer-french -f Dockerfile.al2023 .
## Build with specific Tesseract version and language
docker build --build-arg TESSERACT_VERSION=5.0.0 --build-arg OCR_LANG=fra -t tesseract-lambda-layer -f Dockerfile.al2023 .
## Build with custom compiler optimizations (e.g., for different CPU architectures)
docker build --build-arg COMPILER_FLAGS="-march=native -O3 -std=c++17" -t tesseract-lambda-layer-optimized -f Dockerfile.al2023 .
Deployment size optimization
The library files that are content of the layer are stripped, before deployment to make them more suitable for the lambda environment. See Dockerfiles:
RUN ... \
find ${DIST}/lib -name '*.so*' | xargs strip -s
The stripping can cause issues, when the build runtime and the lambda runtime are different (e.g. if building on Amazon Linux 1 and running on Amazon Linux 2).
Building the layer binaries directly using CDK
You can build the layer directly and get the artifacts (like in ready-to-use). This is done using AWS CDK with the bundling option.
Refer to continous-integration and the corresponding Github Workflow for an example.
Layer contents
The layer contents get deployed to /opt, when used by a function. See here for details.
See ready-to-use for layer contents for Amazon Linux 2023 and Amazon Linux 2.
Migration from AL2 to AL2023
Why Migrate?
- Extended Support: AL2023 receives updates until 2028
- Modern Runtimes: Python 3.12+, Node.js 20+
- Performance: Improved compiler optimizations and newer system libraries
- Security: Latest security patches and cryptographic libraries
Migration Steps
1. Update Runtime
| Current Runtime |
→ |
AL2023 Runtime |
| Python 3.8-3.11 |
→ |
Python 3.12 |
| Node.js 18 |
→ |
Node.js 20 |
| Ruby 2.7 |
→ |
Ruby 3.2 |
2. Update Layer Reference
Serverless Framework:
# Before
layers:
tesseractAl2:
path: ready-to-use/amazonlinux-2
compatibleRuntimes:
- python3.8
# After
layers:
tesseractAl2023:
path: ready-to-use/amazonlinux-2023
compatibleRuntimes:
- python3.12
AWS CDK:
// Before
const layer = new lambda.LayerVersion(stack, 'layer', {
code: Code.fromAsset('ready-to-use/amazonlinux-2'),
});
new lambda.Function(stack, 'fn', {
runtime: Runtime.PYTHON_3_8,
layers: [layer],
});
// After
const layer = new lambda.LayerVersion(stack, 'layer', {
code: Code.fromAsset('ready-to-use/amazonlinux-2023'),
});
new lambda.Function(stack, 'fn', {
runtime: Runtime.PYTHON_3_12,
layers: [layer],
});
3. Test Locally
# Update dependencies for new runtime
pip install --upgrade -r requirements.txt # Python
npm update # Node.js
# Test with SAM CLI
sam local invoke --runtime python3.12 ...
4. Deploy & Monitor
- Deploy to dev/staging environment first
- Check CloudWatch logs for compatibility issues
- Verify OCR functionality works correctly
- Roll out to production gradually
Common Issues
Python 3.12 Compatibility
- Some packages need updates for Python 3.12
- Use
pip install --upgrade for dependencies
- Check for deprecated Python APIs
Node.js Native Modules
- Native modules must be recompiled for AL2023
- Ensure node-gyp is up to date
- Test with
sam local invoke
Library Versions
- AL2023 may have different .so library versions
- Error: "cannot open shared object file"
- Solution: Use the AL2023 layer (not AL2 layer)
Known Issues
Avoiding Pillow library issues
Use cloud9 IDE with AMI linux to deploy example