所以我有下面的rust aws lambda函数:
use std::io::Read;
use std::process::{Command, Stdio};
use lambda_http::{run, service_fn, Body, Error, Request, RequestExt, Response};
use lambda_http::aws_lambda_events::serde_json::json;
/// This is the main body for the function.
/// Write your code inside it.
/// There are some code example in the following URLs:
/// - https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples
async fn function_handler(_event: Request) -> Result<Response<Body>, Error> {
// Extract some useful information from the request
let program = Command::new("./myProggram")
.stdout(Stdio::piped())
.output()
.expect("failed to execute process");
let data = String::from_utf8(program.stdout).unwrap();
let parsed = data.split("\n").filter(|x| !x.is_empty()).collect::<Vec<&str>>();
// Return something that implements IntoResponse.
// It will be serialized to the right response event automatically by the runtime
let resp = Response::builder()
.status(200)
.header("content-type", "application/json")
.body(json!(parsed).to_string().into())
.map_err(Box::new)?;
Ok(resp)
}
#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
// disable printing the name of the module in every log line.
.with_target(false)
// disabling time is handy because CloudWatch will add the ingestion time.
.without_time()
.init();
run(service_fn(function_handler)).await
}
这里的想法是,我希望以JSON格式从二进制文件返回响应。
我用cargo lambda编译函数,生成bootstrap文件,然后手动压缩它,包括bootstrap二进制文件和myProgram二进制文件。
当我在lambda面板中通过向其发送事件来测试我的函数时,我得到了带有正确头等的响应,但响应主体为空。我正在通过AWS面板部署我的函数,在Amazon Linux 2上的自定义运行时上上传zip文件。
当我使用cargo lambda watch和cargo lambda invoke进行本地测试时,响应体中填充了解析为json的myProgramstdout。
任何关于实际云计算中出现问题的想法或想法都非常感谢!
1条答案
按热度按时间fkvaft9z1#
我的问题是二进制文件中的动态链接库。它实际上是python二进制文件,缺少特定版本的GLIBC。在我的情况下,最简单的解决方案是在Amazon Linux 2上编译myProgram