Cargo lambda 构建找不到 OpenSSL 开发标头

J S*_*ach 1 openssl rust aws-lambda cargo-lambda

我正在尝试使用 Cargo-lambda 在 Rust 中为 AWS 编写一个 lambda 函数。cargo lambda new当我调用 时,生成的示例函数构建得很好cargo lambda build --release --arm64。但是,当我尝试构建代码时,我按照https://github.com/awslabs/aws-lambda-rust-runtimehttps://www.cargo-lambda.info/guide/getting上的说明和示例进行编写-started.html,构建无法找到 OpenSSL 的标头。我已经安装了 OpenSSL 并pacman -S openssl运行了pacman -S linux-headers良好的措施,并尝试在我的 Fish 配置文件中运行export OPENSSL_LIB_DIR=/usr/lib/export OPENSSL_INCLUDE_DIR=/usr/include/openssl/设置它们,但两者都不起作用。我无法过去:

cargo:warning=build/expando.c:1:10: fatal error: 'openssl/opensslv.h' file not found
cargo:warning=#include <openssl/opensslv.h>
Run Code Online (Sandbox Code Playgroud)

我已目视验证该文件存在于目录中,但由于某种原因openssl-sys-0.9.87无法找到它并出现恐慌。有其他人遇到过这种情况,或者有人有任何我可以尝试的想法吗?我不确定问题是否出在我的 Cargo-lambda 设置、我的 OpenSSL 设置、我的代码上,或者完全是其他什么地方。

这段代码是用以下代码生成的cargo lambda new project_name并且构建得很好:

use lambda_runtime::{run, service_fn, Error, LambdaEvent};
use serde::{Deserialize, Serialize};

/// Main function generated by cargo-lambda
#[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();

    lambda_runtime::run(service_fn(function_handler)).await
}

/// This is a made-up example.
#[derive(Deserialize)]
struct Request {
    command: String,
}

/// This is a made-up example of what a response structure may look like.
#[derive(Serialize)]
struct Response {
    req_id: String,
    msg: String,
}

/// This is the main body for the function.
/// Write your code inside it.
async fn function_handler(event: LambdaEvent<Request>) -> Result<Response, Error> {
    // Extract some useful info from the request
    let command = event.payload.command;

    // Prepare the response
    let resp = Response {
        req_id: event.context.request_id,
        msg: format!("Command {}.", command),
    };

    // Return `Response` (it will be serialized to JSON automatically by the runtime)
    Ok(resp)
}
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试构建的代码,但openssl-sys-0.9.87即使它是在同一项目中编写的并遵循在线文档中的示例,也会出现错误:

use lambda_runtime::{run, service_fn, Error, LambdaEvent};
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

#[tokio::main]
async fn main() -> Result<(), lambda_runtime::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();

    let query = service_fn(query_handler);
    run(query).await
}

async fn query_handler(event: LambdaEvent<Value>) -> Result<Value, Error> {
    let (event, _context) = event.into_parts();
    let symbol = event["symbol"].to_string();
    let message = query_price(symbol)?;
    Ok(json!({ "message": format!("{}", message) }))
}

#[derive(Deserialize, Debug)]
#[allow(non_snake_case)]
struct PriceQuote {
    pub s: String,
    pub symbol: Vec<String>,
    pub ask: Vec<f32>,
    pub askSize: Vec<u32>,
    pub bid: Vec<f32>,
    pub bidSize: Vec<u32>,
    pub mid: Vec<f32>,
    pub last: Vec<f32>,
    pub volume: Vec<u32>,
    pub updated: Vec<u32>,
}

fn query_price(symbol: String) -> Result<String, reqwest::Error> {
    //let symbol = "AAPL";

    let url = format!("https://api.marketdata.app/v1/stocks/quotes/{}", symbol);
    let client = Client::new();
    let response = client.get(url).send()?;
    let price_quote: PriceQuote = response.json()?;

    let symbol: &String = &price_quote.symbol[0];
    let last_price: &f32 = &price_quote.last[0];

    Ok(format!("Last price for {} is {}", symbol, last_price).to_string())
}
Run Code Online (Sandbox Code Playgroud)

为什么其中一个会建造而不是另一个?我添加的依赖项是否会扰乱它?我的 OpenSSL 版本是否错误?我做错了吗?我缺少什么?抱歉,如果这些都是愚蠢的问题,我是 AWS 新手,我的头在旋转。

J S*_*ach 5

不确定到底出了什么问题,但似乎 reqwest 板条箱是问题的根源。我从来没有遇到过这个问题,除了通过 Cargo-lambda 构建时,所以我想它与 Zig 有关?

无论哪种方式,都可以通过直接在 Cargo.toml 中添加 openssl 来解决:

openssl = { version = "0.10.35", features = ["vendored"] }
Run Code Online (Sandbox Code Playgroud)

这似乎为 reqwest 提供了正确的链接,并允许构建货物 lambda。还没有测试 reqwest 是否仍然有效,但我认为没有理由不应该。

更新:该功能部署并且 reqwest 工作正常