Skip to content

Commit e74d7cf

Browse files
authored
Fix defaults to work with the lambda emulator better (awslabs#4)
* Fix api gateway serializer. Add missing fields. Signed-off-by: David Calavera <[email protected]> * Set default log names. So the lambda emulator works as expected. Signed-off-by: David Calavera <[email protected]>
1 parent b14a4e6 commit e74d7cf

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

lambda-http/src/request.rs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub enum LambdaRequest<'a> {
6868
},
6969
#[serde(rename_all = "camelCase")]
7070
ApiGateway {
71+
resource: Cow<'a, str>,
7172
path: Cow<'a, str>,
7273
#[serde(default, deserialize_with = "deserialize_method")]
7374
http_method: http::Method,
@@ -125,7 +126,7 @@ pub struct ApiGatewayV2RequestContext {
125126
/// The identifier API Gateway assigns to your API.
126127
pub api_id: String,
127128
/// The stringified value of the specified key-value pair of the context map returned from an API Gateway Lambda authorizer function.
128-
#[serde(default)]
129+
#[serde(default, deserialize_with = "nullable_default")]
129130
pub authorizer: HashMap<String, Value>,
130131
/// The full domain name used to invoke the API. This should be the same as the incoming Host header.
131132
pub domain_name: String,
@@ -155,19 +156,30 @@ pub struct ApiGatewayRequestContext {
155156
pub resource_id: String,
156157
/// The deployment stage of the API request (for example, Beta or Prod).
157158
pub stage: String,
159+
/// The domain name of the API Gateway.
160+
pub domain_name: String,
161+
/// The domain prefix of the API Gateway.
162+
pub domain_prefix: String,
158163
/// The ID that API Gateway assigns to the API request.
159164
pub request_id: String,
160165
/// 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.
161166
pub resource_path: String,
167+
/// The HTTP protocol used for this request.
168+
pub protocol: String,
162169
/// The HTTP method used. Valid values include: DELETE, GET, HEAD, OPTIONS, PATCH, POST, and PUT.
163170
pub http_method: String,
164171
/// The stringified value of the specified key-value pair of the context map returned from an API Gateway Lambda authorizer function.
165-
#[serde(default)]
172+
#[serde(default, deserialize_with = "nullable_default")]
166173
pub authorizer: HashMap<String, Value>,
167174
/// The identifier API Gateway assigns to your API.
168175
pub api_id: String,
169176
/// Cofnito identity information
170177
pub identity: Identity,
178+
/// The time of the request in String format.
179+
pub request_time: String,
180+
/// The epoch of the request.
181+
pub request_time_epoch: i64,
182+
pub operation_name: Option<String>,
171183
}
172184

173185
/// Elastic load balancer context information
@@ -243,6 +255,7 @@ pub struct Identity {
243255
/// For API methods that require an API key, this variable is the API key associated with the method request.
244256
/// For methods that don't require an API key, this variable is null.
245257
pub api_key: Option<String>,
258+
pub api_key_id: Option<String>,
246259
/// Undocumented. Can be the API key ID associated with an API request that requires an API key.
247260
/// The description of `api_key` and `access_key` may actually be reversed.
248261
pub access_key: Option<String>,
@@ -437,6 +450,7 @@ impl<'a> From<LambdaRequest<'a>> for http::Request<Body> {
437450
body,
438451
is_base64_encoded,
439452
request_context,
453+
..
440454
} => {
441455
let builder = http::Request::builder()
442456
.method(http_method)
@@ -738,4 +752,53 @@ mod tests {
738752
Test { foo: HashMap::new() }
739753
)
740754
}
755+
756+
#[test]
757+
fn deserialize_from_str() {
758+
let data = r#"
759+
{
760+
"resource": "",
761+
"path": "/.netlify/functions/crabs",
762+
"httpMethod": "GET",
763+
"headers": {},
764+
"multiValueHeaders": {},
765+
"queryStringParameters": null,
766+
"multiValueQueryStringParameters": null,
767+
"pathParameters": null,
768+
"stageVariables": null,
769+
"requestContext": {
770+
"accountId": "",
771+
"resourceId": "",
772+
"stage": "",
773+
"domainName": "",
774+
"domainPrefix": "",
775+
"requestId": "",
776+
"protocol": "",
777+
"identity": {
778+
"cognitoIdentityPoolId": "",
779+
"accountId": "",
780+
"cognitoIdentityId": "",
781+
"caller": "",
782+
"apiKey": "",
783+
"apiKeyId": "",
784+
"accessKey": "",
785+
"sourceIp": "",
786+
"cognitoAuthenticationType": "",
787+
"cognitoAuthenticationProvider": "",
788+
"userArn": "",
789+
"userAgent": "",
790+
"user": ""
791+
},
792+
"resourcePath": "",
793+
"authorizer": null,
794+
"httpMethod": "",
795+
"requestTime": "",
796+
"requestTimeEpoch": 0,
797+
"apiId": ""
798+
},
799+
"body": ""
800+
}
801+
"#;
802+
super::from_str(data).expect("failed to deserialize");
803+
}
741804
}

lambda/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ mod types;
6060
use requests::{EventCompletionRequest, EventErrorRequest, IntoRequest, NextEventRequest};
6161
use types::Diagnostic;
6262

63+
static DEFAULT_LOG_GROUP: &str = "/aws/lambda/Functions";
64+
static DEFAULT_LOG_STREAM: &str = "$LATEST";
65+
6366
/// Error type that lambdas may result in
6467
pub(crate) type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
6568

@@ -88,8 +91,8 @@ impl Config {
8891
function_name: env::var("AWS_LAMBDA_FUNCTION_NAME")?,
8992
memory: env::var("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")?.parse::<i32>()?,
9093
version: env::var("AWS_LAMBDA_FUNCTION_VERSION")?,
91-
log_stream: env::var("AWS_LAMBDA_LOG_STREAM_NAME")?,
92-
log_group: env::var("AWS_LAMBDA_LOG_GROUP_NAME")?,
94+
log_stream: env::var("AWS_LAMBDA_LOG_STREAM_NAME").unwrap_or_else(|_e| DEFAULT_LOG_STREAM.to_owned()),
95+
log_group: env::var("AWS_LAMBDA_LOG_GROUP_NAME").unwrap_or_else(|_e| DEFAULT_LOG_GROUP.to_owned()),
9396
};
9497
Ok(conf)
9598
}

0 commit comments

Comments
 (0)