Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ pub trait Handler<E, O> {

`Handler` provides a default implementation that enables you to provide a Rust closure or function pointer to the `lambda!()` macro.

Optionally, you can pass your own instance of Tokio runtime to the `lambda!()` macro. See our [`with_custom_runtime.rs` example](https://github.com/awslabs/aws-lambda-rust-runtime/tree/master/lambda-runtime/examples/with_custom_runtime.rs)
Optionally, you can pass your own instance of Tokio runtime to the `lambda!()` macro:
```
let rt = tokio::runtime::Runtime::new()?;
lambda!(my_handler, rt);
```

## AWS event objects

Expand Down
6 changes: 3 additions & 3 deletions lambda-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub trait Handler: Sized {
/// The type of Response this Handler will return
type Response: IntoResponse;
/// The type of Future this Handler will return
type Fut: Future<Output = Result<Self::Response, Self::Error>> + 'static;
type Fut: Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + 'static;
/// Function used to execute handler behavior
fn call(&self, event: Request, context: Context) -> Self::Fut;
}
Expand All @@ -137,7 +137,7 @@ impl<F, R, Fut> Handler for F
where
F: Fn(Request, Context) -> Fut,
R: IntoResponse,
Fut: Future<Output = Result<R, Error>> + Send + 'static,
Fut: Future<Output = Result<R, Error>> + Send + Sync + 'static,
{
type Response = R;
type Error = Error;
Expand All @@ -150,7 +150,7 @@ where
#[doc(hidden)]
pub struct TransformResponse<R, E> {
is_alb: bool,
fut: Pin<Box<dyn Future<Output = Result<R, E>>>>,
fut: Pin<Box<dyn Future<Output = Result<R, E>> + Send + Sync>>,
}

impl<R, E> Future for TransformResponse<R, E>
Expand Down
56 changes: 52 additions & 4 deletions lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,42 +102,65 @@ impl LambdaRequest<'_> {
}
}

/// See [context-variable-reference](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html) for more detail.
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ApiGatewayV2RequestContext {
/// The API owner's AWS account ID.
pub account_id: String,
/// The identifier API Gateway assigns to your API.
pub api_id: String,
/// The stringified value of the specified key-value pair of the context map returned from an API Gateway Lambda authorizer function.
#[serde(default)]
pub authorizer: HashMap<String, Value>,
/// The full domain name used to invoke the API. This should be the same as the incoming Host header.
pub domain_name: String,
/// The first label of the $context.domainName. This is often used as a caller/customer identifier.
pub domain_prefix: String,
/// The HTTP method used.
pub http: Http,
/// The ID that API Gateway assigns to the API request.
pub request_id: String,
/// Undocumented, could be resourcePath
pub route_key: String,
/// The deployment stage of the API request (for example, Beta or Prod).
pub stage: String,
/// Undocumented, could be requestTime
pub time: String,
/// Undocumented, could be requestTimeEpoch
pub time_epoch: usize,
}

/// See [context-variable-reference](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html) for more detail.
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ApiGatewayRequestContext {
//pub path: String,
/// The API owner's AWS account ID.
pub account_id: String,
/// The identifier that API Gateway assigns to your resource.
pub resource_id: String,
/// The deployment stage of the API request (for example, Beta or Prod).
pub stage: String,
/// The ID that API Gateway assigns to the API request.
pub request_id: String,
/// The path to your resource. For example, for the non-proxy request URI of `https://{rest-api-id.execute-api.{region}.amazonaws.com/{stage}/root/child`, The $context.resourcePath value is /root/child.
pub resource_path: String,
/// The HTTP method used. Valid values include: DELETE, GET, HEAD, OPTIONS, PATCH, POST, and PUT.
pub http_method: String,
/// The stringified value of the specified key-value pair of the context map returned from an API Gateway Lambda authorizer function.
#[serde(default)]
pub authorizer: HashMap<String, Value>,
/// The identifier API Gateway assigns to your API.
pub api_id: String,
/// Cofnito identity information
pub identity: Identity,
}

/// Elastic load balancer context information
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AlbRequestContext {
/// Elastic load balancer context information
pub elb: Elb,
}

Expand Down Expand Up @@ -167,28 +190,53 @@ pub struct Elb {
#[serde(rename_all = "camelCase")]
pub struct Http {
#[serde(deserialize_with = "deserialize_method")]
/// The HTTP method used. Valid values include: DELETE, GET, HEAD, OPTIONS, PATCH, POST, and PUT.
pub method: http::Method,
/// The request path. For example, for a non-proxy request URL of
/// `https://{rest-api-id.execute-api.{region}.amazonaws.com/{stage}/root/child`,
/// the $context.path value is `/{stage}/root/child`.
pub path: String,
/// The request protocol, for example, HTTP/1.1.
pub protocol: String,
/// The source IP address of the TCP connection making the request to API Gateway.
pub source_ip: String,
/// The User-Agent header of the API caller.
pub user_agent: String,
}

/// Identity assoicated with API Gateway request
#[derive(Deserialize, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Identity {
/// The source IP address of the TCP connection making the request to API Gateway.
pub source_ip: String,
/// The Amazon Cognito identity ID of the caller making the request.
/// Available only if the request was signed with Amazon Cognito credentials.
pub cognito_identity_id: Option<String>,
/// The Amazon Cognito identity pool ID of the caller making the request.
/// Available only if the request was signed with Amazon Cognito credentials.
pub cognito_identity_pool_id: Option<String>,
/// A comma-separated list of the Amazon Cognito authentication providers used by the caller making the request.
/// Available only if the request was signed with Amazon Cognito credentials.
pub cognito_authentication_provider: Option<String>,
/// The Amazon Cognito authentication type of the caller making the request.
/// Available only if the request was signed with Amazon Cognito credentials.
pub cognito_authentication_type: Option<String>,
/// The AWS account ID associated with the request.
pub account_id: Option<String>,
/// The principal identifier of the caller making the request.
pub caller: Option<String>,
/// For API methods that require an API key, this variable is the API key associated with the method request.
/// For methods that don't require an API key, this variable is null.
pub api_key: Option<String>,
/// Undocumented. Can be the API key ID associated with an API request that requires an API key.
/// The description of `api_key` and `access_key` may actually be reversed.
pub access_key: Option<String>,
/// The principal identifier of the user making the request. Used in Lambda authorizers.
pub user: Option<String>,
/// The User-Agent header of the API caller.
pub user_agent: Option<String>,
/// The Amazon Resource Name (ARN) of the effective user identified after authentication.
pub user_arn: Option<String>,
}

Expand Down Expand Up @@ -352,7 +400,7 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
.expect("failed to build request");

// no builder method that sets headers in batch
mem::replace(req.headers_mut(), headers);
let _ = mem::replace(req.headers_mut(), headers);

req
}
Expand Down Expand Up @@ -416,7 +464,7 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
}

// no builder method that sets headers in batch
mem::replace(req.headers_mut(), multi_value_headers);
let _ = mem::replace(req.headers_mut(), multi_value_headers);

req
}
Expand Down Expand Up @@ -477,7 +525,7 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
}

// no builder method that sets headers in batch
mem::replace(req.headers_mut(), multi_value_headers);
let _ = mem::replace(req.headers_mut(), multi_value_headers);

req
}
Expand Down
2 changes: 2 additions & 0 deletions lambda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ async-stream = "0.2"
tracing-subscriber = "0.2"
once_cell = "1.4.0"
simple_logger = "1.6.0"
log = "0.4"
simple-error = "0.2"
Copy link
Contributor

@Veetaha Veetaha Jul 24, 2020

Choose a reason for hiding this comment

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

I suggest using anyhow instead of simple-error. simple-error has only 17 stars and was last updated a year ago...

Loading