[英]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 watch和cargo lambda invoke在本地進行測試時,響應主體中充滿了解析為 json 的myProgram stdout。
非常感謝任何關於實際雲中出現問題的想法或想法!
我的問題是二進制文件中的動態鏈接庫。 它實際上是 python 二進制文件,並且缺少特定版本的 GLIBC。 就我而言,最簡單的解決方案是在 Amazon Linux 2 上編譯myProgram
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.