繁体   English   中英

使用 rust 在 aws lambda 中调用二进制文件

[英]Invoking binary in aws lambda with rust

所以我有以下 rust aws lambda function:

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 格式从二进制文件返回响应。

我正在编译 function 和cargo lambda生成引导程序文件,然后我通过包含引导程序二进制文件和myProgram二进制文件手动压缩它。

当我通过向它发送事件在 lambda 面板中测试我的 function 时,我得到了带有正确标头等的响应,但响应正文为空。 我正在通过 aws 面板在 Amazon Linux 2 上的自定义运行时通过上传 zip 文件来部署我的 function。

当我使用cargo lambda watchcargo lambda invoke在本地进行测试时,响应主体中充满了解析为 json 的myProgram stdout。

非常感谢任何关于实际云中出现问题的想法或想法!

我的问题是二进制文件中的动态链接库。 它实际上是 python 二进制文件,并且缺少特定版本的 GLIBC。 就我而言,最简单的解决方案是在 Amazon Linux 2 上编译myProgram

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM