Skip to content

Commit 86fbf59

Browse files
committed
Update readme
1 parent 3902588 commit 86fbf59

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,80 @@ By default, the log level to emit events is `INFO`. Log at `TRACE` level for mor
449449

450450
This project includes Lambda event struct definitions, [`aws_lambda_events`](https://crates.io/crates/aws_lambda_events). This crate can be leveraged to provide strongly-typed Lambda event structs. You can create your own custom event objects and their corresponding structs as well.
451451

452+
### Builder pattern for event responses
453+
454+
The `aws_lambda_events` crate provides an optional `builders` feature that adds builder pattern support for constructing event responses. This is particularly useful when working with custom context types that don't implement `Default`.
455+
456+
Enable the builders feature in your `Cargo.toml`:
457+
458+
```toml
459+
[dependencies]
460+
aws_lambda_events = { version = "*", features = ["builders"] }
461+
```
462+
463+
Example with API Gateway custom authorizers:
464+
465+
```rust
466+
use aws_lambda_events::event::apigw::{
467+
ApiGatewayV2CustomAuthorizerSimpleResponseBuilder,
468+
ApiGatewayV2CustomAuthorizerV2Request,
469+
};
470+
use lambda_runtime::{Error, LambdaEvent};
471+
472+
struct MyContext {
473+
user_id: String,
474+
permissions: Vec<String>,
475+
}
476+
477+
async fn handler(
478+
event: LambdaEvent<ApiGatewayV2CustomAuthorizerV2Request>,
479+
) -> Result<ApiGatewayV2CustomAuthorizerSimpleResponse<MyContext>, Error> {
480+
let context = MyContext {
481+
user_id: "user-123".to_string(),
482+
permissions: vec!["read".to_string()],
483+
};
484+
485+
let response = ApiGatewayV2CustomAuthorizerSimpleResponseBuilder::default()
486+
.is_authorized(true)
487+
.context(context)
488+
.build()?;
489+
490+
Ok(response)
491+
}
492+
```
493+
494+
Example with SQS batch responses:
495+
496+
```rust
497+
use aws_lambda_events::event::sqs::{
498+
BatchItemFailureBuilder,
499+
SqsBatchResponseBuilder,
500+
SqsEvent,
501+
};
502+
use lambda_runtime::{Error, LambdaEvent};
503+
504+
async fn handler(event: LambdaEvent<SqsEvent>) -> Result<SqsBatchResponse, Error> {
505+
let mut failures = Vec::new();
506+
507+
for record in event.payload.records {
508+
if let Err(_) = process_record(&record).await {
509+
let failure = BatchItemFailureBuilder::default()
510+
.item_identifier(record.message_id.unwrap())
511+
.build()?;
512+
failures.push(failure);
513+
}
514+
}
515+
516+
let response = SqsBatchResponseBuilder::default()
517+
.batch_item_failures(failures)
518+
.build()?;
519+
520+
Ok(response)
521+
}
522+
```
523+
524+
See the [examples directory](https://github.com/aws/aws-lambda-rust-runtime/tree/main/lambda-events/examples) for more builder pattern examples.
525+
452526
### Custom event objects
453527

454528
To serialize and deserialize events and responses, we suggest using the [`serde`](https://github.com/serde-rs/serde) library. To receive custom events, annotate your structure with Serde's macros:

lambda-events/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,50 @@ This crate divides all Lambda Events into features named after the service that
2727
cargo add aws_lambda_events --no-default-features --features apigw,alb
2828
```
2929

30+
### Builder pattern support
31+
32+
The crate provides an optional `builders` feature that adds builder pattern support for event types. This enables type-safe, immutable construction of event responses without requiring `Default` trait implementations on custom context types.
33+
34+
Enable the builders feature:
35+
36+
```
37+
cargo add aws_lambda_events --features builders
38+
```
39+
40+
Example using builders with API Gateway custom authorizers:
41+
42+
```rust
43+
use aws_lambda_events::event::apigw::{
44+
ApiGatewayV2CustomAuthorizerSimpleResponseBuilder,
45+
ApiGatewayV2CustomAuthorizerV2Request,
46+
};
47+
use lambda_runtime::{Error, LambdaEvent};
48+
49+
// Context type without Default implementation
50+
struct MyContext {
51+
user_id: String,
52+
permissions: Vec<String>,
53+
}
54+
55+
async fn handler(
56+
event: LambdaEvent<ApiGatewayV2CustomAuthorizerV2Request>,
57+
) -> Result<ApiGatewayV2CustomAuthorizerSimpleResponse<MyContext>, Error> {
58+
let context = MyContext {
59+
user_id: "user-123".to_string(),
60+
permissions: vec!["read".to_string()],
61+
};
62+
63+
let response = ApiGatewayV2CustomAuthorizerSimpleResponseBuilder::default()
64+
.is_authorized(true)
65+
.context(context)
66+
.build()?;
67+
68+
Ok(response)
69+
}
70+
```
71+
72+
See the [examples directory](https://github.com/aws/aws-lambda-rust-runtime/tree/main/lambda-events/examples) for more builder pattern examples.
73+
3074
## History
3175

3276
The AWS Lambda Events crate was created by [Christian Legnitto](https://github.com/LegNeato). Without all his work and dedication, this project could have not been possible.

0 commit comments

Comments
 (0)