From 81be4d030d49e2d2f3c40d1e8c352a759009d461 Mon Sep 17 00:00:00 2001 From: rimutaka Date: Sun, 21 Mar 2021 17:49:35 +1300 Subject: [PATCH 1/7] Added changes from 2020 on top of v.0.3 master --- lambda-runtime/src/client.rs | 4 ++-- lambda-runtime/src/lib.rs | 10 ++++++---- lambda-runtime/src/types.rs | 12 +++++++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lambda-runtime/src/client.rs b/lambda-runtime/src/client.rs index 45693a17..d26841e4 100644 --- a/lambda-runtime/src/client.rs +++ b/lambda-runtime/src/client.rs @@ -273,10 +273,10 @@ mod endpoint_tests { Ok(event) } let f = crate::handler_fn(func); - + let config = crate::Config::from_env()?; let client = &runtime.client; let incoming = incoming(client).take(1); - runtime.run(incoming, f).await?; + runtime.run(incoming, f, &config).await?; // shutdown server tx.send(()).expect("Receiver has been dropped"); diff --git a/lambda-runtime/src/lib.rs b/lambda-runtime/src/lib.rs index a78da826..2f176c4a 100644 --- a/lambda-runtime/src/lib.rs +++ b/lambda-runtime/src/lib.rs @@ -34,7 +34,7 @@ use types::Diagnostic; pub type Error = Box; /// Configuration derived from environment variables. -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)] pub struct Config { /// The host and port of the [runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html). pub endpoint: String, @@ -133,6 +133,7 @@ where &self, incoming: impl Stream, Error>> + Send, handler: F, + config: &Config, ) -> Result<(), Error> where F: Handler + Send + Sync + 'static, @@ -150,6 +151,7 @@ where let (parts, body) = event.into_parts(); let ctx: Context = Context::try_from(parts.headers)?; + let ctx: Context = ctx.with_config(config); let body = hyper::body::to_bytes(body).await?; trace!("{}", std::str::from_utf8(&body)?); // this may be very verbose let body = serde_json::from_slice(&body)?; @@ -299,16 +301,16 @@ where { trace!("Loading config from env"); let config = Config::from_env()?; - let uri = config.endpoint.try_into().expect("Unable to convert to URL"); + let uri = config.endpoint.clone().try_into().expect("Unable to convert to URL"); let runtime = Runtime::builder() .with_connector(HttpConnector::new()) .with_endpoint(uri) .build() - .expect("Unable create runtime"); + .expect("Unable to create a runtime"); let client = &runtime.client; let incoming = incoming(client); - runtime.run(incoming, handler).await + runtime.run(incoming, handler, &config).await } fn type_name_of_val(_: T) -> &'static str { diff --git a/lambda-runtime/src/types.rs b/lambda-runtime/src/types.rs index c3e11498..c8579250 100644 --- a/lambda-runtime/src/types.rs +++ b/lambda-runtime/src/types.rs @@ -93,7 +93,7 @@ pub struct CognitoIdentity { /// are populated using the [Lambda environment variables](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html) /// and the headers returned by the poll request to the Runtime APIs. #[non_exhaustive] -#[derive(Clone, Debug, PartialEq, Default)] +#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)] pub struct Context { /// The AWS request ID generated by the Lambda service. pub request_id: String, @@ -141,3 +141,13 @@ impl TryFrom for Context { Ok(ctx) } } + +impl Context { + /// Add environment details to the context by setting `env_config`. + pub fn with_config(self, config: &Config) -> Self { + Self { + env_config: config.clone(), + ..self + } + } +} From d70cc07e7ab62daf6ff4c037fea396a3e3f0f3a1 Mon Sep 17 00:00:00 2001 From: rimutaka Date: Sun, 21 Mar 2021 23:34:45 +0000 Subject: [PATCH 2/7] Init Lambda env vars for an end to end test --- lambda-runtime/src/client.rs | 24 +++++++++++++++++++++++- lambda-runtime/src/lib.rs | 15 +++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lambda-runtime/src/client.rs b/lambda-runtime/src/client.rs index d26841e4..b13fa1dd 100644 --- a/lambda-runtime/src/client.rs +++ b/lambda-runtime/src/client.rs @@ -68,6 +68,7 @@ mod endpoint_tests { use serde_json::json; use simulated::DuplexStreamWrapper; use std::convert::TryFrom; + use std::env; use tokio::{ io::{self, AsyncRead, AsyncWrite}, select, @@ -273,7 +274,28 @@ mod endpoint_tests { Ok(event) } let f = crate::handler_fn(func); - let config = crate::Config::from_env()?; + + // set env vars needed to init Config if they are not already set in the environment + if env::var("AWS_LAMBDA_RUNTIME_API").is_err() { + env::set_var("AWS_LAMBDA_RUNTIME_API", "http://localhost:9001"); + } + if env::var("AWS_LAMBDA_FUNCTION_NAME").is_err() { + env::set_var("AWS_LAMBDA_FUNCTION_NAME", "test_fn"); + } + if env::var("AWS_LAMBDA_FUNCTION_MEMORY_SIZE").is_err() { + env::set_var("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128"); + } + if env::var("AWS_LAMBDA_FUNCTION_VERSION").is_err() { + env::set_var("AWS_LAMBDA_FUNCTION_VERSION", "1"); + } + if env::var("AWS_LAMBDA_LOG_STREAM_NAME").is_err() { + env::set_var("AWS_LAMBDA_LOG_STREAM_NAME", "test_stream"); + } + if env::var("AWS_LAMBDA_LOG_GROUP_NAME").is_err() { + env::set_var("AWS_LAMBDA_LOG_GROUP_NAME", "test_log"); + } + let config = crate::Config::from_env().expect("Failed to read env vars"); + let client = &runtime.client; let incoming = incoming(client).take(1); runtime.run(incoming, f, &config).await?; diff --git a/lambda-runtime/src/lib.rs b/lambda-runtime/src/lib.rs index 2f176c4a..c629fd0c 100644 --- a/lambda-runtime/src/lib.rs +++ b/lambda-runtime/src/lib.rs @@ -54,12 +54,15 @@ impl Config { /// Attempts to read configuration from environment variables. pub fn from_env() -> Result { let conf = Config { - endpoint: env::var("AWS_LAMBDA_RUNTIME_API")?, - function_name: env::var("AWS_LAMBDA_FUNCTION_NAME")?, - memory: env::var("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")?.parse::()?, - version: env::var("AWS_LAMBDA_FUNCTION_VERSION")?, - log_stream: env::var("AWS_LAMBDA_LOG_STREAM_NAME")?, - log_group: env::var("AWS_LAMBDA_LOG_GROUP_NAME")?, + endpoint: env::var("AWS_LAMBDA_RUNTIME_API").expect("Missing AWS_LAMBDA_RUNTIME_API env var"), + function_name: env::var("AWS_LAMBDA_FUNCTION_NAME").expect("Missing AWS_LAMBDA_FUNCTION_NAME env var"), + memory: env::var("AWS_LAMBDA_FUNCTION_MEMORY_SIZE") + .expect("Missing AWS_LAMBDA_FUNCTION_MEMORY_SIZE env var") + .parse::() + .expect("AWS_LAMBDA_FUNCTION_MEMORY_SIZE env var is not "), + version: env::var("AWS_LAMBDA_FUNCTION_VERSION").expect("Missing AWS_LAMBDA_FUNCTION_VERSION env var"), + log_stream: env::var("AWS_LAMBDA_LOG_STREAM_NAME").expect("Missing AWS_LAMBDA_LOG_STREAM_NAME env var"), + log_group: env::var("AWS_LAMBDA_LOG_GROUP_NAME").expect("Missing AWS_LAMBDA_LOG_GROUP_NAME env var"), }; Ok(conf) } From 6fdfead5e3f91557a5ec7146326375ea34f5fd12 Mon Sep 17 00:00:00 2001 From: rimutaka Date: Sun, 21 Mar 2021 23:40:59 +0000 Subject: [PATCH 3/7] Formatting change for cargo fmt --- lambda-runtime/src/client.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lambda-runtime/src/client.rs b/lambda-runtime/src/client.rs index b13fa1dd..2b860e87 100644 --- a/lambda-runtime/src/client.rs +++ b/lambda-runtime/src/client.rs @@ -67,8 +67,7 @@ mod endpoint_tests { use hyper::{server::conn::Http, service::service_fn, Body}; use serde_json::json; use simulated::DuplexStreamWrapper; - use std::convert::TryFrom; - use std::env; + use std::{convert::TryFrom, env}; use tokio::{ io::{self, AsyncRead, AsyncWrite}, select, From e579150dc83f1d31352619a202c727333f587af3 Mon Sep 17 00:00:00 2001 From: rimutaka Date: Sun, 21 Mar 2021 23:48:54 +0000 Subject: [PATCH 4/7] Bumped the version to 0.3.1 --- lambda-runtime/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambda-runtime/Cargo.toml b/lambda-runtime/Cargo.toml index b2d68246..f218f196 100644 --- a/lambda-runtime/Cargo.toml +++ b/lambda-runtime/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lambda_runtime" -version = "0.3.0" +version = "0.3.1" authors = ["David Barsky "] description = "AWS Lambda Runtime" edition = "2018" From c96e15174f5c5a20257c5138bcf1461396284233 Mon Sep 17 00:00:00 2001 From: rimutaka Date: Mon, 22 Mar 2021 00:29:07 +0000 Subject: [PATCH 5/7] Added debugging section to ReadMe --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index e0abe94b..d42b6199 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,10 @@ $ unzip -o \ # Ctrl-D to yield control back to your function ``` +### Debugging + +Lambdas can be run and debugged locally using a special [Lambda debug proxy](https://github.com/rimutaka/lambda-debug-proxy), which is a Lambda function that forwards incoming requests to one AWS SQS queue and reads responses from another queue. A local proxy running on your development computer reads the queue, calls your lambda locally and sends back the response. This approach allows debugging of Lambda functions locally while being part of your AWS workflow. The lambda handler code does not need to be modified between the local and AWS versions. + ## `lambda` `lambda_runtime` is a library for authoring reliable and performant Rust-based AWS Lambda functions. At a high level, it provides a few major components: From b7a2be806c0acbbe9e239624ac6e6dae8dc6177b Mon Sep 17 00:00:00 2001 From: rimutaka Date: Sun, 28 Mar 2021 16:21:04 +1300 Subject: [PATCH 6/7] ReadMe Debugging section mentioned non-AWS project --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d42b6199..a9891b2b 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ $ unzip -o \ ### Debugging -Lambdas can be run and debugged locally using a special [Lambda debug proxy](https://github.com/rimutaka/lambda-debug-proxy), which is a Lambda function that forwards incoming requests to one AWS SQS queue and reads responses from another queue. A local proxy running on your development computer reads the queue, calls your lambda locally and sends back the response. This approach allows debugging of Lambda functions locally while being part of your AWS workflow. The lambda handler code does not need to be modified between the local and AWS versions. +Lambdas can be run and debugged locally using a special [Lambda debug proxy](https://github.com/rimutaka/lambda-debug-proxy) (a non-AWS repo maintained by @rimutaka), which is a Lambda function that forwards incoming requests to one AWS SQS queue and reads responses from another queue. A local proxy running on your development computer reads the queue, calls your lambda locally and sends back the response. This approach allows debugging of Lambda functions locally while being part of your AWS workflow. The lambda handler code does not need to be modified between the local and AWS versions. ## `lambda` From 317a362f243f60e4670bc3704135c3bfd1f20131 Mon Sep 17 00:00:00 2001 From: rimutaka Date: Sun, 28 Mar 2021 16:21:40 +1300 Subject: [PATCH 7/7] Roll back version bump back to 0.3.0 --- lambda-runtime/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambda-runtime/Cargo.toml b/lambda-runtime/Cargo.toml index f218f196..b2d68246 100644 --- a/lambda-runtime/Cargo.toml +++ b/lambda-runtime/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lambda_runtime" -version = "0.3.1" +version = "0.3.0" authors = ["David Barsky "] description = "AWS Lambda Runtime" edition = "2018"