Skip to content

Commit 721f616

Browse files
Bring README up to date with master branch (#247)
* First pass of changes to bring README up to date with `master` branch, preparing for next release * First round of PR feedback - address typos and `extern crate` * Apply suggestions from softprops and davidbarsky Co-authored-by: David Barsky <[email protected]> * Remove mentions of attribute macro per discussion in PR. * Return hello functionality to README and referenced example, while leaving custom object handling to later in the document * Update Deployment to reflect new example behavior * Suggestion from softprops Co-authored-by: David Barsky <[email protected]>
1 parent feab8b7 commit 721f616

File tree

2 files changed

+25
-67
lines changed

2 files changed

+25
-67
lines changed

README.md

Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,38 @@
44

55
This package makes it easy to run AWS Lambda Functions written in Rust. This workspace includes multiple crates:
66

7-
- [![Docs](https://docs.rs/lambda_runtime_client/badge.svg)](https://docs.rs/lambda_runtime_client) **`lambda-runtime-client`** is a client SDK for the Lambda Runtime APIs. You probably don't need to use this crate directly!
8-
- [![Docs](https://docs.rs/lambda_runtime/badge.svg)](https://docs.rs/lambda_runtime) **`lambda-runtime`** is a library that makes it easy to write Lambda functions in Rust.
7+
- [![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.
98
- [![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.
109

1110
## Example function
1211

13-
The code below creates a simple function that receives an event with a `greeting` and `name` field and returns a `GreetingResponse` message for the given name and greeting. Notice: to run these examples, we require a minimum Rust version of 1.31.
12+
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.
1413

1514
```rust,no_run
16-
use std::error::Error;
17-
18-
use lambda_runtime::{error::HandlerError, lambda, Context};
19-
use log::{self, error};
20-
use serde_derive::{Deserialize, Serialize};
21-
use simple_error::bail;
22-
use simple_logger;
23-
24-
#[derive(Deserialize)]
25-
struct CustomEvent {
26-
#[serde(rename = "firstName")]
27-
first_name: String,
28-
}
15+
use lambda::{handler_fn, Context};
16+
use serde_json::{json, Value};
2917
30-
#[derive(Serialize)]
31-
struct CustomOutput {
32-
message: String,
33-
}
34-
35-
fn main() -> Result<(), Box<dyn Error>> {
36-
simple_logger::init_with_level(log::Level::Debug)?;
37-
lambda!(my_handler);
18+
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
3819
20+
#[tokio::main]
21+
async fn main() -> Result<(), Error> {
22+
let func = handler_fn(func);
23+
lambda::run(func).await?;
3924
Ok(())
4025
}
4126
42-
fn my_handler(e: CustomEvent, c: Context) -> Result<CustomOutput, HandlerError> {
43-
if e.first_name == "" {
44-
error!("Empty first name in request {}", c.aws_request_id);
45-
bail!("Empty first name");
46-
}
27+
async fn func(event: Value, _: Context) -> Result<Value, Error> {
28+
let first_name = event["firstName"].as_str().unwrap_or("world");
4729
48-
Ok(CustomOutput {
49-
message: format!("Hello, {}!", e.first_name),
50-
})
30+
Ok(json!({ "message": format!("Hello, {}!", first_name) }))
5131
}
5232
```
5333

54-
The code above is the same as the [basic example](https://github.com/awslabs/aws-lambda-rust-runtime/tree/master/lambda-runtime/examples/basic.rs) in the `lambda-runtime` crate.
34+
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.
5535

5636
### Deployment
5737

58-
There are currently multiple ways of building this package: manually, and with the [Serverless framework](https://serverless.com/framework/).
38+
There are currently multiple ways of building this package: manually with the AWS CLI, and with the [Serverless framework](https://serverless.com/framework/).
5939

6040
#### AWS CLI
6141

@@ -103,7 +83,7 @@ You can now test the function using the AWS CLI or the AWS Lambda console
10383
$ aws lambda invoke --function-name rustTest \
10484
--payload '{"firstName": "world"}' \
10585
output.json
106-
$ cat output.json # Prints: {"message":"Hello, world!"}
86+
$ cat output.json # Prints: {"message": "Hello, world!"}
10787
```
10888

10989
**Note:** `--cli-binary-format raw-in-base64-out` is a required
@@ -177,34 +157,14 @@ $ unzip -o \
177157
# Ctrl-D to yield control back to your function
178158
```
179159

180-
## lambda-runtime-client
181-
182-
Defines the `RuntimeClient` trait and provides its `HttpRuntimeClient` implementation. The client fetches events and returns output as `Vec<u8>`.
183-
184-
For error reporting to the runtime APIs the library defines the `RuntimeApiError` trait and the `ErrorResponse` object. Custom errors for the APIs should implement the `to_response() -> ErrorResponse` method of the `RuntimeApiError` trait.
160+
## `lambda`
185161

186-
## lambda-runtime
162+
`lambda` is a library for authoring reliable and performant Rust-based AWS Lambda functions. At a high level, it provides a few major components:
187163

188-
This library makes it easy to create Rust executables for AWS lambda. The library defines a `lambda!()` macro. Call the `lambda!()` macro from your main method with an implementation the `Handler` type:
164+
- `Handler`, a trait that defines interactions between customer-authored code and this library.
165+
- `lambda::run`, function that runs an `Handler`.
189166

190-
```rust
191-
pub trait Handler<E, O> {
192-
/// Run the handler.
193-
fn run(
194-
&mut self,
195-
event: E,
196-
ctx: Context
197-
) -> Result<O, HandlerError>;
198-
}
199-
```
200-
201-
`Handler` provides a default implementation that enables you to provide a Rust closure or function pointer to the `lambda!()` macro.
202-
203-
Optionally, you can pass your own instance of Tokio runtime to the `lambda!()` macro:
204-
```
205-
let rt = tokio::runtime::Runtime::new()?;
206-
lambda!(my_handler, rt);
207-
```
167+
The function `handler_fn` converts a rust function or closure to `Handler`, which can then be run by `lambda::run`.
208168

209169
## AWS event objects
210170

@@ -215,11 +175,7 @@ This project does not currently include Lambda event struct definitions though w
215175
To serialize and deserialize events and responses, we suggest using the use the [`serde`](https://github.com/serde-rs/serde) library. To receive custom events, annotate your structure with Serde's macros:
216176

217177
```rust
218-
extern crate serde;
219-
extern crate serde_derive;
220-
extern crate serde_json;
221-
222-
use serde_derive::{Serialize, Deserialize};
178+
use serde::{Serialize, Deserialize};
223179
use serde_json::json;
224180
use std::error::Error;
225181

lambda/examples/hello-without-macro.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use lambda::{handler_fn, Context};
2-
use serde_json::Value;
2+
use serde_json::{json, Value};
33

44
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
55

@@ -11,5 +11,7 @@ async fn main() -> Result<(), Error> {
1111
}
1212

1313
async fn func(event: Value, _: Context) -> Result<Value, Error> {
14-
Ok(event)
14+
let first_name = event["firstName"].as_str().unwrap_or("world");
15+
16+
Ok(json!({ "message": format!("Hello, {}!", first_name) }))
1517
}

0 commit comments

Comments
 (0)