aws 部署python lambda,Python限制中的AWS Lambda部署软件包

I want to run my code on AWS lambda function. To do so, i need to import some python packages (i.e. pandas, numpy, sklearn, scipy)

I have two problems:

First of all, the size of (unzip) packaged python zip files is greater than 250MB.

Secondly, I got some error using scipy as well as sklearn as:

Unable to import module 'lambda_function': cannot import name

'_ccallback_c'

of

Unable to import module 'lambda_function': No module named

'sklearn.check_build._check_build'

___________________________________________________________________________ Contents of /var/task/sklearn/__check_build:

__pycache _check_build.cpython-35m-x86_64-linux-gnu.sosetup.py

init.py

___________________________________________________________________________ It seems that scikit-learn has not been built correctly.

I tried to reinstall many times...

But still problems in sklearn and scipy.

Any idea?

sample code in AWS LambdaFunction:

import json

import numpy

import pandas

import sklearn

import scipy

def lambda_handler(event, context):

# TODO implement

print(event)

return

解决方案

You appear to have two issues.

The first (and easiest to solve) is that you need to install the relevant modules on a Linux distro comparable to Amazon Linux.

You can either do this using EC2 or in a Docker container with Amazon Linux on it.

The second issue (which is a bit trickier if not impossible to solve given the size of the modules you want to use) is that you need to get your deployment size down to under 250MB unzipped and under 50MB zipped.

Using relevant CFLAG when installing may get you some of the way there. See here for an idea of what might work.

If you are still over limit (which I suspect you will be) your only choice left will be to delete some of the files in the modules which you believe will not be used in your particular program. This is risky, often error prone and usually takes many attempts to get right. Using code coverage tools may help you here, as they can indicate which files are actually being used.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将Spring Cloud Function部署AWS Lambda可以分为以下几个步骤: 1. 创建一个Java项目,并添加Spring Cloud Function和AWS Lambda的依赖。 2. 编写一个Lambda函数处理器类,实现AWS Lambda的RequestHandler接口。这个处理器类负责接收和处理来自AWS Lambda的事件请求,并将请求转发给Spring Cloud Function的函数接口。 3. 将项目打包成一个Jar文件,并上传到AWS Lambda。 4. 配置AWS Lambda的触发器,让它在某个事件发生时执行。 下面是一个简单的示例,演示如何将Spring Cloud Function部署AWS Lambda: 1. 创建一个Maven项目,并添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-adapter-aws</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-aws-autoconfigure</artifactId> <version>2.3.2.RELEASE</version> </dependency> ``` 2. 编写一个Lambda函数处理器类: ```java import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; import org.springframework.cloud.function.adapter.aws.SpringBootRequestHandler; public class MyLambdaFunctionHandler implements RequestHandler<String, String> { private final SpringBootRequestHandler<String, String> handler; public MyLambdaFunctionHandler() { handler = new SpringBootRequestHandler<>(MyFunction.class); } @Override public String handleRequest(String input, Context context) { return handler.handleRequest(input, context); } } ``` 这个类实现了AWS Lambda的RequestHandler接口,并将请求转发给Spring Boot Request Handler。MyFunction是你自己编写的函数接口实现类。 3. 将项目打包成一个Jar文件,并上传到AWS Lambda。 你可以使用Maven命令打包项目: ``` mvn package ``` 然后,将生成的Jar文件上传到AWS Lambda。 4. 配置AWS Lambda的触发器,让它在某个事件发生时执行。 你可以在AWS Lambda控制台上配置触发器,例如API Gateway、S3事件等。 以上就是将Spring Cloud Function部署AWS Lambda的基本步骤。你可以根据自己的需求对Lambda函数进行配置和优化,以获得更好的性能和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值