Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This package makes it easy to run AWS Lambda Functions written in Rust. This workspace includes multiple crates:

- [![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.
- [![Docs](https://docs.rs/lambda_http/badge.svg)](https://docs.rs/lambda_http) **`lambda-http`** is a library for writing HTTP Lambda functions in Rust, with support for upstream API Gateway, ALB, Lambda Function URLs, and VPC Lattice.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clarify the top-level description of what the library does (but happy if this is tidied/shortened). Possibly does not need to enumerate all upstream services.

- [![Docs](https://docs.rs/lambda-extension/badge.svg)](https://docs.rs/lambda-extension) **`lambda-extension`** is a library that makes it easy to write Lambda Runtime Extensions in Rust.
- [![Docs](https://docs.rs/aws_lambda_events/badge.svg)](https://docs.rs/aws_lambda_events) **`lambda-events`** is a library with strongly-typed Lambda event structs in Rust.
- [![Docs](https://docs.rs/lambda_runtime_api_client/badge.svg)](https://docs.rs/lambda_runtime_api_client) **`lambda-runtime-api-client`** is a shared library between the lambda runtime and lambda extension libraries that includes a common API client to talk with the AWS Lambda Runtime API.
Expand Down
7 changes: 2 additions & 5 deletions lambda-events/src/event/vpc_lattice/v2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
custom_serde::{deserialize_headers, deserialize_nullish, http_method, serialize_multi_value_headers},
encodings::Body,
};
use crate::custom_serde::{deserialize_headers, deserialize_nullish, http_method, serialize_multi_value_headers};
#[cfg(feature = "builders")]
use bon::Builder;
use http::{HeaderMap, Method};
Expand Down Expand Up @@ -47,7 +44,7 @@ pub struct VpcLatticeRequestV2 {

/// The request body
#[serde(default)]
pub body: Option<Body>,
pub body: Option<String>,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a mistake by me in the original event PR. This makes the type consistent with all other request event types. The Body abstraction is meant for responses (I think)


/// Whether the body is base64 encoded
#[serde(default, deserialize_with = "deserialize_nullish")]
Expand Down
5 changes: 3 additions & 2 deletions lambda-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ categories = ["web-programming::http-server"]
readme = "README.md"

[features]
default = ["apigw_rest", "apigw_http", "apigw_websockets", "alb", "tracing"]
default = ["apigw_rest", "apigw_http", "apigw_websockets", "alb", "vpc_lattice", "tracing"]
apigw_rest = []
apigw_http = []
apigw_websockets = []
alb = []
vpc_lattice = []
pass_through = []
catch-all-fields = ["aws_lambda_events/catch-all-fields"]
tracing = ["lambda_runtime/tracing"] # enables access to the Tracing utilities
Expand Down Expand Up @@ -53,7 +54,7 @@ url = "2.2"
path = "../lambda-events"
version = "1.1"
default-features = false
features = ["alb", "apigw"]
features = ["alb", "apigw", "vpc_lattice"]

[dev-dependencies]
axum-core = "0.5.4"
Expand Down
18 changes: 11 additions & 7 deletions lambda-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[![Docs](https://docs.rs/lambda_http/badge.svg)](https://docs.rs/lambda_http)

**`lambda-http`** is an abstraction that takes payloads from different services and turns them into http objects, making it easy to write API Gateway proxy event focused Lambda functions in Rust.
**`lambda-http`** is an abstraction that takes HTTP-shaped payloads from different AWS services and turns them
into standard `http` objects, making it easy to write Lambda functions in Rust that serve HTTP traffic
regardless of the upstream trigger.
Comment on lines +5 to +7
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clarifying the overall purpose of the library - I feel it is more centered on HTTP shape payloads and not API gateway proxy events..


lambda-http handler is made of:

Expand All @@ -14,17 +16,18 @@ We are able to handle requests from:
* [API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) REST, HTTP and WebSockets API lambda integrations
* AWS [ALB](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html)
* AWS [Lambda function URLs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html)
* AWS [VPC Lattice](https://docs.aws.amazon.com/vpc-lattice/latest/ug/lambda-functions.html)

Thanks to the `Request` type we can seamlessly handle proxy integrations without the worry to specify the specific service type.
Thanks to the `Request` type we can seamlessly handle all of these triggers without having to write service-specific code.

There is also an extension for `lambda_http::Request` structs that provides access to [API gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format) and [ALB](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html) features.
There is also an extension for `lambda_http::Request` structs that provides access to [API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format), [ALB](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html), and [VPC Lattice](https://docs.aws.amazon.com/vpc-lattice/latest/ug/lambda-functions.html) features.

For example some handy extensions:

* `query_string_parameters` - Return pre-parsed http query string parameters, parameters provided after the `?` portion of a url associated with the request
* `path_parameters` - Return pre-extracted path parameters, parameter provided in url placeholders `/foo/{bar}/baz/{qux}` associated with the request
* `lambda_context` - Return the Lambda context for the invocation; see the [runtime docs](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-next)
* `request_context` - Return the ALB/API Gateway request context
* `request_context` - Return the ALB, API Gateway, or VPC Lattice request context
* payload - Return the Result of a payload parsed into a type that implements `serde::Deserialize`

See the `lambda_http::RequestPayloadExt` and `lambda_http::RequestExt` traits for more extensions.
Expand Down Expand Up @@ -238,16 +241,17 @@ If you don't want to receive the stage as part of the path, you can set the envi

## Feature flags

`lambda_http` is a wrapper for HTTP events coming from three different services, Amazon Load Balancer (ALB), Amazon Api Gateway (APIGW), and AWS Lambda Function URLs. Amazon Api Gateway can also send events from three different endpoints, REST APIs, HTTP APIs, and WebSockets. `lambda_http` transforms events from all these sources into native `http::Request` objects, so you can incorporate Rust HTTP semantics into your Lambda functions.
`lambda_http` is a wrapper for HTTP events coming from four different AWS services: Application Load Balancer (ALB), API Gateway, Lambda Function URLs, and VPC Lattice. API Gateway can send events from three different endpoint types: REST APIs, HTTP APIs, and WebSockets. `lambda_http` transforms events from all these sources into native `http::Request` objects, so you can incorporate Rust HTTP semantics into your Lambda functions.

By default, `lambda_http` compiles your function to support any of those services. This increases the compile time of your function because we have to generate code for all the sources. In reality, you'll usually put a Lambda function only behind one of those sources. You can choose which source to generate code for with feature flags.

The available features flags for `lambda_http` are the following:
The available feature flags for `lambda_http` are the following:

- `alb`: for events coming from [Amazon Elastic Load Balancer](https://aws.amazon.com/elasticloadbalancing/).
- `apigw_rest`: for events coming from [Amazon API Gateway Rest APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.html).
- `apigw_rest`: for events coming from [Amazon API Gateway REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-rest-api.html).
- `apigw_http`: for events coming from [Amazon API Gateway HTTP APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api.html) and [AWS Lambda Function URLs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html).
- `apigw_websockets`: for events coming from [Amazon API Gateway WebSockets](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html).
- `vpc_lattice`: for events coming from [AWS VPC Lattice](https://docs.aws.amazon.com/vpc-lattice/latest/ug/lambda-functions.html).

If you only want to support one of these sources, you can disable the default features, and enable only the source that you care about in your package's `Cargo.toml` file. Substitute the dependency line for `lambda_http` for the snippet below, changing the feature that you want to enable:

Expand Down
23 changes: 23 additions & 0 deletions lambda-http/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use aws_lambda_events::apigw::ApiGatewayProxyRequest;
use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
#[cfg(feature = "apigw_websockets")]
use aws_lambda_events::apigw::ApiGatewayWebsocketProxyRequest;
#[cfg(feature = "vpc_lattice")]
use aws_lambda_events::vpc_lattice::VpcLatticeRequestV2;

use serde::{de::Error, Deserialize};
use serde_json::value::RawValue;

Expand Down Expand Up @@ -39,6 +42,10 @@ impl<'de> Deserialize<'de> for LambdaRequest {
if let Ok(res) = serde_json::from_str::<ApiGatewayWebsocketProxyRequest>(data) {
return Ok(LambdaRequest::WebSocket(res));
}
#[cfg(feature = "vpc_lattice")]
if let Ok(res) = serde_json::from_str::<VpcLatticeRequestV2>(data) {
return Ok(LambdaRequest::VpcLatticeV2(res));
}
#[cfg(feature = "pass_through")]
if PASS_THROUGH_ENABLED {
return Ok(LambdaRequest::PassThrough(data.to_string()));
Expand Down Expand Up @@ -136,6 +143,22 @@ mod tests {
}
}

#[test]
fn test_deserialize_vpc_lattice() {
let data = include_bytes!("../../lambda-events/src/fixtures/example-vpc-lattice-v2-request.json");

let req: LambdaRequest = serde_json::from_slice(data).expect("failed to deserialize vpc lattice data");
match req {
LambdaRequest::VpcLatticeV2(req) => {
assert_eq!(
"arn:aws:vpc-lattice:ap-southeast-2:123456789012:targetgroup/tg-6d0ecf831eec9f09",
req.request_context.target_group_arn
);
}
other => panic!("unexpected request variant: {other:?}"),
}
}

#[test]
#[cfg(feature = "pass_through")]
fn test_deserialize_bedrock_agent() {
Expand Down
5 changes: 4 additions & 1 deletion lambda-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
//#![deny(warnings)]
//! Enriches the `lambda` crate with [`http`](https://github.com/hyperium/http)
//! types targeting AWS [ALB](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html), [API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) REST and HTTP API lambda integrations.
//! types targeting AWS
//! * [ALB](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html)
//! * [API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) REST and HTTP API lambda integrations
//! * [VPC Lattice](https://docs.aws.amazon.com/vpc-lattice/latest/ug/lambda-functions.html)
//!
//! This crate abstracts over all of these trigger events using standard [`http`](https://github.com/hyperium/http) types minimizing the mental overhead
//! of understanding the nuances and variation between trigger details allowing you to focus more on your application while also giving you to the maximum flexibility to
Expand Down
Loading
Loading