diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 8f914433..00000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,18 +0,0 @@ -# next (unreleased) - -- **New**: The `lambda_http` crate now exposes mock helper methods for `RequestExt` under `cfg(test)` builds to facilitate straight forward unit testability of handlers. -- **New**: The `lambda_http` crate now exposes two new functions for deserializing requests from text and raw IO: `lambda_http::request::{from_str,from_reader}`. - -# 0.2.0 - -- **New**: We created lambda_runtime_core crate that implements the runtime's main loop and supports handlers that accept and return Vec. ([#53](https://github.com/awslabs/aws-lambda-rust-runtime/issues/53)) -- **New**: The primary lambda_runtime crate is a wrapper on top of the lambda_runtime_core handler ([#53](https://github.com/awslabs/aws-lambda-rust-runtime/issues/53)) -- **New**: The lambda_http crate, which enables support for API Gateway or ALB requests, treating them as Request structs from the http crate ([#18 by @softprops](https://github.com/awslabs/aws-lambda-rust-runtime/issues/18)). -- **New**: The lambda_runtime_errors crate introduces the LambdaErrorExt trait that enables the ability to specify custom errorType values for the Lambda Runtime API. The change also includes a companion derive crate that makes it easy to automatically generate LambdaErrorExt implementations for crate-local error types ([#63](https://github.com/awslabs/aws-lambda-rust-runtime/issues/63)). -- **Fix**: Handlers can now return any error type ([#54](https://github.com/awslabs/aws-lambda-rust-runtime/issues/54)) -- **Fix**: Support for closures as handlers ([#19 by @srijs](https://github.com/awslabs/aws-lambda-rust-runtime/issues/19)). -- **Fix**: Multiple bug fixes and performance improvements (thanks @Sh4rK). - -# 0.1.0 - -- Initial Release diff --git a/Cargo.toml b/Cargo.toml index 4c4ad33d..291345b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] members = [ - "lambda", - "lambda-http" + "lambda-http", + "lambda-runtime" ] \ No newline at end of file diff --git a/README.md b/README.md index e624a692..b748c586 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This package makes it easy to run AWS Lambda Functions written in Rust. This workspace includes multiple crates: -- [![Docs](https://docs.rs/lambda/badge.svg)](https://docs.rs/lambda) **`lambda`** is a library that provides a Lambda runtime for applications written in Rust. +- [![Docs](https://docs.rs/lambda_runtime/badge.svg)](https://docs.rs/lambda_runtime) **`lambda-runtime`** is a library that provides a Lambda runtime for applications written in Rust. - [![Docs](https://docs.rs/lambda_http/badge.svg)](https://docs.rs/lambda_http) **`lambda-http`** is a library that makes it easy to write API Gateway proxy event focused Lambda functions in Rust. ## Example function @@ -12,7 +12,7 @@ This package makes it easy to run AWS Lambda Functions written in Rust. This wor The code below creates a simple function that receives an event with a `firstName` field and returns a message to the caller. Notice: this crate is tested against latest stable Rust. ```rust,no_run -use lambda::{handler_fn, Context}; +use lambda_runtime::{handler_fn, Context}; use serde_json::{json, Value}; type Error = Box; @@ -20,7 +20,7 @@ type Error = Box; #[tokio::main] async fn main() -> Result<(), Error> { let func = handler_fn(func); - lambda::run(func).await?; + lambda_runtime::run(func).await?; Ok(()) } @@ -31,7 +31,7 @@ async fn func(event: Value, _: Context) -> Result { } ``` -The code above is the same as the [basic example](https://github.com/awslabs/aws-lambda-rust-runtime/blob/master/lambda/examples/hello-without-macro.rs) in the `lambda` crate. +The code above is the same as the [basic example](https://github.com/awslabs/aws-lambda-rust-runtime/blob/master/lambda/examples/hello-without-macro.rs) in the `lambda_runtime` crate. ### Deployment @@ -56,13 +56,13 @@ $ echo $'[target.x86_64-unknown-linux-musl]\nlinker = "x86_64-linux-musl-gcc"' > Compile one of the examples as a _release_ with a specific _target_ for deployment to AWS: ```bash -$ cargo build -p lambda --example basic --release --target x86_64-unknown-linux-musl +$ cargo build -p lambda_runtime --example basic --release --target x86_64-unknown-linux-musl ``` For [a custom runtime](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html), AWS Lambda looks for an executable called `bootstrap` in the deployment package zip. Rename the generated `basic` executable to `bootstrap` and add it to a zip archive. ```bash -$ cp ./target/release/examples/hello ./bootstrap && zip lambda.zip bootstrap && rm bootstrap +$ cp ./target/x86_64-unknown-linux-musl/release/examples/hello ./bootstrap && zip lambda.zip bootstrap && rm bootstrap ``` Now that we have a deployment package (`lambda.zip`), we can use the [AWS CLI](https://aws.amazon.com/cli/) to create a new Lambda function. Make sure to replace the execution role with an existing role in your account! @@ -127,7 +127,7 @@ $ npx serverless invoke -f hello -d '{"foo":"bar"}' Alternatively, you can build a Rust-based Lambda function in a [docker mirror of the AWS Lambda provided runtime with the Rust toolchain preinstalled](https://github.com/softprops/lambda-rust). -Running the following command will start a ephemeral docker container which will build your Rust application and produce a zip file containing its binary auto-renamed to `bootstrap` to meet the AWS Lambda's expectations for binaries under `target/lambda/release/{your-binary-name}.zip`, typically this is just the name of your crate if you are using the cargo default binary (i.e. `main.rs`) +Running the following command will start a ephemeral docker container which will build your Rust application and produce a zip file containing its binary auto-renamed to `bootstrap` to meet the AWS Lambda's expectations for binaries under `target/lambda_runtime/release/{your-binary-name}.zip`, typically this is just the name of your crate if you are using the cargo default binary (i.e. `main.rs`) ```bash # build and package deploy-ready artifact @@ -159,12 +159,12 @@ $ unzip -o \ ## `lambda` -`lambda` is a library for authoring reliable and performant Rust-based AWS Lambda functions. At a high level, it provides a few major components: +`lambda_runtime` is a library for authoring reliable and performant Rust-based AWS Lambda functions. At a high level, it provides a few major components: - `Handler`, a trait that defines interactions between customer-authored code and this library. -- `lambda::run`, function that runs an `Handler`. +- `lambda_runtime::run`, function that runs an `Handler`. -The function `handler_fn` converts a rust function or closure to `Handler`, which can then be run by `lambda::run`. +The function `handler_fn` converts a rust function or closure to `Handler`, which can then be run by `lambda_runtime::run`. ## AWS event objects diff --git a/lambda-http/Cargo.toml b/lambda-http/Cargo.toml index 526f45f3..2c882a1a 100644 --- a/lambda-http/Cargo.toml +++ b/lambda-http/Cargo.toml @@ -17,12 +17,12 @@ travis-ci = { repository = "awslabs/aws-lambda-rust-runtime" } maintenance = { status = "actively-developed" } [dependencies] -base64 = "0.12" +base64 = "0.13.0" http = "0.2" -lambda = { path = "../lambda", version = "0.1" } +lambda_runtime = { path = "../lambda-runtime", version = "0.1" } serde = { version = "^1", features = ["derive"] } serde_json = "^1" -serde_urlencoded = "0.6" +serde_urlencoded = "0.7.0" [dev-dependencies] log = "^0.4" diff --git a/lambda-http/examples/hello-http.rs b/lambda-http/examples/hello-http.rs index 0a609651..3fadb9de 100644 --- a/lambda-http/examples/hello-http.rs +++ b/lambda-http/examples/hello-http.rs @@ -1,6 +1,6 @@ use lambda_http::{ handler, - lambda::{self, Context}, + lambda_runtime::{self, Context}, IntoResponse, Request, RequestExt, Response, }; @@ -8,7 +8,7 @@ type Error = Box; #[tokio::main] async fn main() -> Result<(), Error> { - lambda::run(handler(func)).await?; + lambda_runtime::run(handler(func)).await?; Ok(()) } diff --git a/lambda-http/src/ext.rs b/lambda-http/src/ext.rs index 13740f49..13a3be52 100644 --- a/lambda-http/src/ext.rs +++ b/lambda-http/src/ext.rs @@ -66,7 +66,7 @@ impl Error for PayloadError { /// as well as `{"x":1, "y":2}` respectively. /// /// ```rust,no_run -/// use lambda_http::{handler, lambda::{self, Context}, Body, IntoResponse, Request, Response, RequestExt}; +/// use lambda_http::{handler, lambda_runtime::{self, Context}, Body, IntoResponse, Request, Response, RequestExt}; /// use serde::Deserialize; /// /// type Error = Box; @@ -81,7 +81,7 @@ impl Error for PayloadError { /// /// #[tokio::main] /// async fn main() -> Result<(), Error> { -/// lambda::run(handler(add)).await?; +/// lambda_runtime::run(handler(add)).await?; /// Ok(()) /// } /// diff --git a/lambda-http/src/lib.rs b/lambda-http/src/lib.rs index 7cff2b9a..2351383e 100644 --- a/lambda-http/src/lib.rs +++ b/lambda-http/src/lib.rs @@ -12,12 +12,12 @@ //! ## Hello World //! //! The following example is how you would structure your Lambda such that you have a `main` function where you explicitly invoke -//! `lambda::run` in combination with the [`handler`](fn.handler.html) function. This pattern allows you to utilize global initialization +//! `lambda_runtime::run` in combination with the [`handler`](fn.handler.html) function. This pattern allows you to utilize global initialization //! of tools such as loggers, to use on warm invokes to the same Lambda function after the first request, helping to reduce the latency of //! your function's execution path. //! //! ```rust,no_run -//! use lambda_http::{handler, lambda}; +//! use lambda_http::{handler, lambda_runtime}; //! //! type Error = Box; //! @@ -25,7 +25,7 @@ //! async fn main() -> Result<(), Error> { //! // initialize dependencies once here for the lifetime of your //! // lambda task -//! lambda::run(handler(|request, context| async { Ok("👋 world!") })).await?; +//! lambda_runtime::run(handler(|request, context| async { Ok("👋 world!") })).await?; //! Ok(()) //! } //! ``` @@ -36,13 +36,13 @@ //! with the [`RequestExt`](trait.RequestExt.html) trait. //! //! ```rust,no_run -//! use lambda_http::{handler, lambda::{self, Context}, IntoResponse, Request, RequestExt}; +//! use lambda_http::{handler, lambda_runtime::{self, Context}, IntoResponse, Request, RequestExt}; //! //! type Error = Box; //! //! #[tokio::main] //! async fn main() -> Result<(), Error> { -//! lambda::run(handler(hello)).await?; +//! lambda_runtime::run(handler(hello)).await?; //! Ok(()) //! } //! @@ -66,8 +66,8 @@ extern crate maplit; pub use http::{self, Response}; -use lambda::Handler as LambdaHandler; -pub use lambda::{self, Context}; +use lambda_runtime::Handler as LambdaHandler; +pub use lambda_runtime::{self, Context}; mod body; pub mod ext; @@ -93,7 +93,7 @@ pub type Request = http::Request; /// Functions serving as ALB and API Gateway REST and HTTP API handlers must conform to this type. /// -/// This can be viewed as a `lambda::Handler` constrained to `http` crate `Request` and `Response` types +/// This can be viewed as a `lambda_runtime::Handler` constrained to `http` crate `Request` and `Response` types pub trait Handler: Sized { /// The type of Error that this Handler will return type Error; @@ -105,7 +105,7 @@ pub trait Handler: Sized { fn call(&self, event: Request, context: Context) -> Self::Fut; } -/// Adapts a [`Handler`](trait.Handler.html) to the `lambda::run` interface +/// Adapts a [`Handler`](trait.Handler.html) to the `lambda_runtime::run` interface pub fn handler(handler: H) -> Adapter { Adapter { handler } } @@ -146,7 +146,7 @@ where } } -/// Exists only to satisfy the trait cover rule for `lambda::Handler` impl +/// Exists only to satisfy the trait cover rule for `lambda_runtime::Handler` impl /// /// User code should never need to interact with this type directly. Since `Adapter` implements `Handler` /// It serves as a opaque trait covering type. diff --git a/lambda/Cargo.toml b/lambda-runtime/Cargo.toml similarity index 97% rename from lambda/Cargo.toml rename to lambda-runtime/Cargo.toml index 6ef7e1b0..c1e4944f 100644 --- a/lambda/Cargo.toml +++ b/lambda-runtime/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "lambda" +name = "lambda_runtime" version = "0.1.0" authors = ["David Barsky "] description = "AWS Lambda Runtime" diff --git a/lambda/examples/README.md b/lambda-runtime/examples/README.md similarity index 98% rename from lambda/examples/README.md rename to lambda-runtime/examples/README.md index 51a5a10b..5e080eee 100644 --- a/lambda/examples/README.md +++ b/lambda-runtime/examples/README.md @@ -63,10 +63,6 @@ Runtime.ExitError See _error-handling.rs_ example for more error handling options. -## macro.rs - -The most basic example using `#[lambda]` macro to reduce the amount of boilerplate code. - **Deployment**: ```bash cp ./target/x86_64-unknown-linux-musl/release/examples/macro ./bootstrap && zip lambda.zip bootstrap && rm bootstrap diff --git a/lambda/examples/basic.rs b/lambda-runtime/examples/basic.rs similarity index 95% rename from lambda/examples/basic.rs rename to lambda-runtime/examples/basic.rs index 7b492a44..999efdcc 100644 --- a/lambda/examples/basic.rs +++ b/lambda-runtime/examples/basic.rs @@ -1,7 +1,7 @@ // This example requires the following input to succeed: // { "command": "do something" } -use lambda::{handler_fn, Context}; +use lambda_runtime::{handler_fn, Context}; use log::LevelFilter; use serde::{Deserialize, Serialize}; use simple_logger::SimpleLogger; @@ -35,7 +35,7 @@ async fn main() -> Result<(), Error> { SimpleLogger::new().with_level(LevelFilter::Info).init().unwrap(); let func = handler_fn(my_handler); - lambda::run(func).await?; + lambda_runtime::run(func).await?; Ok(()) } diff --git a/lambda/examples/error-handling.rs b/lambda-runtime/examples/error-handling.rs similarity index 95% rename from lambda/examples/error-handling.rs rename to lambda-runtime/examples/error-handling.rs index 1b061254..f5ba2474 100644 --- a/lambda/examples/error-handling.rs +++ b/lambda-runtime/examples/error-handling.rs @@ -1,5 +1,5 @@ /// See https://github.com/awslabs/aws-lambda-rust-runtime for more info on Rust runtime for AWS Lambda -use lambda::handler_fn; +use lambda_runtime::handler_fn; use log::LevelFilter; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; @@ -73,12 +73,12 @@ async fn main() -> Result<(), Error> { // call the actual handler of the request let func = handler_fn(func); - lambda::run(func).await?; + lambda_runtime::run(func).await?; Ok(()) } /// The actual handler of the Lambda request. -pub(crate) async fn func(event: Value, ctx: lambda::Context) -> Result { +pub(crate) async fn func(event: Value, ctx: lambda_runtime::Context) -> Result { // check what action was requested match serde_json::from_value::(event)?.event_type { EventType::SimpleError => { diff --git a/lambda/src/client.rs b/lambda-runtime/src/client.rs similarity index 100% rename from lambda/src/client.rs rename to lambda-runtime/src/client.rs diff --git a/lambda/src/lib.rs b/lambda-runtime/src/lib.rs similarity index 98% rename from lambda/src/lib.rs rename to lambda-runtime/src/lib.rs index ac454c33..e05edbf4 100644 --- a/lambda/src/lib.rs +++ b/lambda-runtime/src/lib.rs @@ -4,7 +4,7 @@ //! The mechanism available for defining a Lambda function is as follows: //! //! Create a type that conforms to the [`Handler`] trait. This type can then be passed -//! to the the `lambda::run` function, which launches and runs the Lambda runtime. +//! to the the `lambda_runtime::run` function, which launches and runs the Lambda runtime. pub use crate::types::Context; use client::Client; use hyper::client::{connect::Connection, HttpConnector}; @@ -273,7 +273,7 @@ where /// /// # Example /// ```no_run -/// use lambda::{handler_fn, Context}; +/// use lambda_runtime::{handler_fn, Context}; /// use serde_json::Value; /// /// type Error = Box; @@ -281,7 +281,7 @@ where /// #[tokio::main] /// async fn main() -> Result<(), Error> { /// let func = handler_fn(func); -/// lambda::run(func).await?; +/// lambda_runtime::run(func).await?; /// Ok(()) /// } /// diff --git a/lambda/src/requests.rs b/lambda-runtime/src/requests.rs similarity index 100% rename from lambda/src/requests.rs rename to lambda-runtime/src/requests.rs diff --git a/lambda/src/simulated.rs b/lambda-runtime/src/simulated.rs similarity index 100% rename from lambda/src/simulated.rs rename to lambda-runtime/src/simulated.rs diff --git a/lambda/src/types.rs b/lambda-runtime/src/types.rs similarity index 100% rename from lambda/src/types.rs rename to lambda-runtime/src/types.rs