Skip to content

lambda_http(cargo lambda start): failed to serialize lambda-url event #528

@leny

Description

@leny

Hi, I'm really new into rust & rust with lambda (more into lambda with node.js), and I'm not sure if I encounter a bug or I did something wrong.

I'm unable to test my APIGateway lambda function made with lambda_http.

I have this little about function :

use lambda_http::{run, service_fn, Body, Error, Request, Response};
use serde_json::json;
use chrono::Utc;

async fn function_handler(_: Request) -> Result<Response<Body>, Error> {
    let now: i64 = Utc::now().timestamp_millis();
    let result = json!({
        "name": "test",
        "runtime": "rust",
        "time": now,
    });

    let resp = Response::builder()
        .status(200)
        .header("content-type", "application/json")
        .body(result.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)
        .with_target(false)
        .without_time()
        .init();

    run(service_fn(function_handler)).await
}

Nothing very complex, as you may see. When deployed to AWS using CDK, everythings works as expected, I got the following response as JSON :

{
  "name": "test",
  "runtime": "rust",
  "time": 1662629579658
}

But when I want to test the fonction locally, following what is explained in the readme (using cargo lambda start then hitting the http://localhost:9000/lambda-url/about/ address on my browser), I got the following response in the browser:

{
  "type": "https://httpstatuses.com/500",
  "status": 500,
  "title": "Internal Server Error",
  "detail": "failed to serialize lambda-url event"
}

and the following error in my console:

› cargo lambda start
 INFO invoke server listening on 127.0.0.1:9000
 INFO starting lambda function function="about" manifest="Cargo.toml"
[Running 'cargo run --bin about']
    Finished dev [unoptimized + debuginfo] target(s) in 0.16s
    Running `target/debug/about`
ERROR Error("data did not match any variant of untagged enum LambdaRequest", line: 0, column: 0)
[Finished running. Exit status: 0]    

If it can helps, here's my Cargo.toml file:

[package]
name = "test"
version = "0.0.1"
edition = "2021"

[dependencies]
chrono = "0.4.22"
lambda_http = { version = "0.6.1", default-features = false, features = ["apigw_rest"] }
lambda_runtime = "0.6.1"
serde_json = "1.0.85"
tokio = { version = "1", features = ["macros"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }

[[bin]]
name = "about"
path = "src/handlers/about.rs"

Did I do something wrong?

Thanks in advance,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions