Skip to content

Commit b4d372c

Browse files
committed
Make Lambda Rust Runtime work - see #1
1 parent b6122c1 commit b4d372c

File tree

7 files changed

+56
-26
lines changed

7 files changed

+56
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ aws-config = { version = "0.53.0" }
1818
deno = "1.29.4"
1919
deno_core = "0.167.0"
2020
deno_runtime = "0.93.0"
21+
serde = { version = "=1.0.149", features = ["derive"] }

build_lambda_release.sh

100644100755
File mode changed.

cloud/lib/rust-runtime/Readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
- https://dev.to/ryands17/rust-on-lambda-using-the-cdk-3ccm
44
- https://words.filippo.io/easy-windows-and-linux-cross-compilers-for-macos/
55
- https://burgers.io/cross-compile-rust-from-arm-to-x86-64
6+
- https://github.com/denoland/deno/issues/13458
7+
- https://choubey.gitbook.io/internals-of-deno/import-and-ops/5.6-registration-of-ops
8+
- https://github.com/ddprrt/lambda-hyper-example-workspace/blob/main/lambda/src/
9+
- https://github.com/awslabs/aws-lambda-rust-runtime/discussions/405
10+
- https://github.com/awslabs/aws-lambda-rust-runtime/issues/317#issuecomment-916226693
11+
- https://github.com/awslabs/aws-lambda-rust-runtime/issues/310
12+
lambda_event_layer.rs
613

714
```
815
rustup target add x86_64-unknown-linux-musl

cloud/lib/rust-runtime/cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ byt_runtime.workspace = true
1717
deno_core.workspace = true
1818
tracing = { version = "0.1", features = ["log"] }
1919
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
20+
serde.workspace = true
2021

2122
[[bin]]
2223
name = "bootstrap"

cloud/lib/rust-runtime/src/main.rs

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
use std::path::Path;
2-
use lambda_http::{run, service_fn, Body, Error, Request, Response};
3-
use byt_runtime::myworker::execute_module;
2+
use lambda_runtime::{service_fn, Error, LambdaEvent};
3+
use serde::{Deserialize, Serialize};
44

5+
/// This is also a made-up example. Requests come into the runtime as unicode
6+
/// strings in json format, which can map to any structure that implements `serde::Deserialize`
7+
/// The runtime pays no attention to the contents of the request payload.
8+
#[derive(Deserialize)]
9+
struct Request {
10+
command: String,
11+
}
512

6-
/// This is the main body for the function.
7-
/// Write your code inside it.
8-
/// There are some code examples in the Runtime repository:
9-
/// - https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples
10-
async fn function_handler(_event: Request) -> Result<Response<Body>, Error> {
11-
// Extract some useful information from the request
12-
let sdk_config = aws_config::load_from_env().await;
13-
let js_path = Path::new(env!("CARGO_MANIFEST_DIR"))
14-
.join("examples")
15-
.join("hello.js");
16-
let main_module = deno_core::resolve_path(&js_path.to_string_lossy()).unwrap();
17-
execute_module(main_module, sdk_config, Default::default()).await?;
18-
19-
// Return something that implements IntoResponse.
20-
// It will be serialized to the right response event automatically by the runtime
21-
let resp = Response::builder()
22-
.status(200)
23-
.header("content-type", "text/html")
24-
.body("Hello AWS Lambda HTTP request".into())
25-
.map_err(Box::new)?;
26-
Ok(resp)
13+
/// This is a made-up example of what a response structure may look like.
14+
/// There is no restriction on what it can be. The runtime requires responses
15+
/// to be serialized into json. The runtime pays no attention
16+
/// to the contents of the response payload.
17+
#[derive(Serialize)]
18+
struct Response {
19+
req_id: String,
20+
msg: String,
2721
}
2822

23+
use byt_runtime::myworker::execute_module;
24+
2925
#[tokio::main]
3026
async fn main() -> Result<(), Error> {
3127
tracing_subscriber::fmt()
@@ -34,5 +30,29 @@ async fn main() -> Result<(), Error> {
3430
.without_time()
3531
.init();
3632

37-
run(service_fn(function_handler)).await
38-
}
33+
let func = service_fn(my_handler);
34+
lambda_runtime::run(func).await?;
35+
Ok(())
36+
}
37+
38+
39+
pub(crate) async fn my_handler(event: LambdaEvent<Request>) -> Result<Response, Error> {
40+
// extract some useful info from the request
41+
let command = event.payload.command;
42+
43+
let sdk_config = aws_config::load_from_env().await;
44+
let js_path = Path::new(env!("CARGO_MANIFEST_DIR"))
45+
.join("examples")
46+
.join("hello.js");
47+
let main_module = deno_core::resolve_path(&js_path.to_string_lossy()).unwrap();
48+
execute_module(main_module, sdk_config, Default::default()).await.unwrap();
49+
50+
// prepare the response
51+
let resp = Response {
52+
req_id: event.context.request_id,
53+
msg: format!("Command {} executed.", command),
54+
};
55+
56+
// return `Response` (it will be serialized to JSON automatically by the runtime)
57+
Ok(resp)
58+
}

runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ deno_runtime.workspace = true
2424
aws-config.workspace = true
2525
aws-sdk-s3 = "0.23.0"
2626
tokio.workspace = true
27-
serde = { version = "=1.0.149", features = ["derive"] }
27+
serde.workspace = true

0 commit comments

Comments
 (0)