lambda-http for AWS Lambda 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.
lambda-http handler is made of:
Request- Represents an HTTP requestIntoResponse- Future that will convert an [IntoResponse] into an actual [LambdaResponse]
We are able to handle requests from:
- API Gateway REST, HTTP and WebSockets API lambda integrations
- AWS ALB
- AWS Lambda function URLs
- AWS VPC Lattice
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, ALB, and VPC Lattice 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 requestpath_parameters- Return pre-extracted path parameters, parameter provided in url placeholders/foo/{bar}/baz/{qux}associated with the requestlambda_context- Return the Lambda context for the invocation; see the runtime docsrequest_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.
Examples
Here you will find a few examples to handle basic scenarios:
- Reading a JSON from a body and deserialize into a structure
- Reading query string parameters
- Lambda Request Authorizer
- Passing the Lambda execution context initialization to the handler
Reading a JSON from a body and deserialize into a structure
The code below creates a simple API Gateway proxy (HTTP, REST) that accepts in input a JSON payload.
use ;
use ;
use json;
async
pub async
Reading query string parameters
use ;
use json;
async
pub async
Lambda Request Authorizer
Because lambda-http is an abstraction, we cannot use it for the Lambda Request Authorizer case.
If you remove the abstraction, you need to handle the request/response for your service.
use ;
use ;
use json;
async
pub async
Passing the Lambda execution context initialization to the handler
One of the best practices is to take advantage of execution environment reuse to improve the performance of your function. Initialize SDK clients and database connections outside the function handler. Subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time.
use AttributeValue;
use Utc;
use ;
use json;
async
pub async
Integration with API Gateway stages
When you integrate HTTP Lambda functions with API Gateway stages, the path received in the request will include the stage as the first segment, for example /production/api/v1, where production is the API Gateway stage.
If you don't want to receive the stage as part of the path, you can set the environment variable AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH to true, either in your Lambda function configuration, or inside the main Rust function. Following the previous example, when this environment variable is present, the path that the function receives is /api/v1, eliminating the stage from the first segment.
Feature flags
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 feature flags for lambda_http are the following:
alb: for events coming from Amazon Elastic Load Balancer.apigw_rest: for events coming from Amazon API Gateway REST APIs.apigw_http: for events coming from Amazon API Gateway HTTP APIs and AWS Lambda Function URLs.apigw_websockets: for events coming from Amazon API Gateway WebSockets.vpc_lattice: for events coming from AWS VPC Lattice.catch-all-fields: enables catch-all fields in event types to preserve unknown JSON fields during deserialization.builders: enables builder pattern support for request/response types inaws_lambda_events, useful for constructing test fixtures.
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:
[]
= "0.5.3"
= false
= ["apigw_rest"]