Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

python - Deploy containerized lambda with layer using CDK

I'm working on an ML project that utilizes AWS Lambda for building models and generating predictions. Lambdas are written in python and use several ML libraries like pandas, sklearn, numpy, and scikit-learn. These lambdas use shared code that packaged by Lambda's layer. I use AWS CDK for project deployment. CDK code is written in TypeScript, don't ask why I mix Python and Typescript, it's not relevant in this situation.

The size of the package (lambda code + layer) exceeds the maximum allowed size of 250MB because of ML libraries.

After AWS announcement of containerized lambdas support, I decided to try it out to overcome the 250MB limit. However, I didn't find any good example that fit my situation, so I'm trying to build it myself.

The CDK code looks like this:

...
// Create a lambda layer from code
// Code is located in lambda-code/ml directory and it looks 
// like any Python package with main ML and DB connection functions
const mlLayer = new PythonLayerVersion(this, 'mlLayer', {
      entry: './lambda-code/ml/',
})
...
// Lambda function is specified like
const classifyTransactionLambda = new DockerImageFunction(this, 'classifyTransactionLambda', {      
      code: DockerImageCode.fromImageAsset('./lambda-code/classify'),      
      memorySize: 512,      
      layers: [mlLayer],
      tracing: Tracing.ACTIVE,
      environment: {
        BUCKET_NAME: mlModelsBucket.bucketName,
        ENV: env
      }
});
...

The structure of the code looks like this:

enter image description here

Dockerfile in classify lambda:

# Use the python lambda image from AWS ECR
FROM public.ecr.aws/lambda/python:3.7

COPY requirements.txt ./

RUN pip3 install -r requirements.txt

COPY index.py ./

CMD ["index.classify_transaction_handler"]

When I run cdk deploy I'm getting the following error:

This lambda function uses a runtime that is incompatible with this layer (FROM_IMAGE is not in [python3.7])

Does anyone run into problem like this? Is this error mean that mlLayer version is not compatible with lambda classifyTransactionLambda?

Any help would be very appreciated!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...