Skip to content

Commit

Permalink
fix: Avoid failure if a handler argument is provided to the lambda (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Jul 1, 2022
1 parent f312f4c commit e8b1641
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fixed incorrect environment variable parsing when running Image-based lambdas in LocalStack.
- Updated `aws-sdk-*` dependencies to `0.15.0`.

## 0.5.0
Expand Down
2 changes: 1 addition & 1 deletion scripts/read_logs.sh
Expand Up @@ -20,4 +20,4 @@ if [ -z "$STREAM_NAME" ] || [ "$STREAM_NAME" = "null" ]; then
exit 1
fi

$AWSLOCAL logs get-log-events --log-group-name /aws/lambda/$EXAMPLE --log-stream-name "$STREAM_NAME" --start-from-head | jq
$AWSLOCAL logs get-log-events --log-group-name /aws/lambda/$EXAMPLE --log-stream-name "$STREAM_NAME" --start-from-head | jq -r '.events[].message'
12 changes: 10 additions & 2 deletions src/lambda.rs
Expand Up @@ -10,7 +10,9 @@ use clap::Parser;
use futures::stream::{self, StreamExt, TryStreamExt};
use futures::FutureExt;
use lambda_runtime::{service_fn, LambdaEvent};
use std::ffi::OsString;
use std::future::Future;
use std::iter::empty;
use std::sync::Arc;
use tracing_subscriber::filter::EnvFilter;

Expand Down Expand Up @@ -188,7 +190,13 @@ where
)
.json()
.init();
let env = Env::try_parse().context(
// We only care about values provided as environment variables, so we pass in an empty
// iterator, rather than having clap parse the command line arguments. This avoids an
// unfortunate issue in LocalStack where a command line arg of "handler.handler" is provided
// as a command line argument to the process when using an Image based lambda. This triggers
// a problem, as clap (wrongly, IMHO) tries to assign this command line option to the first
//element of the Env struct, and then ignores any actual environment variable provided.
let env = Env::try_parse_from(empty::<OsString>()).context(
"An error occurred while parsing environment variables for message context.",
)?;
let ctx = Arc::new(Context::from_env(&env).await?);
Expand All @@ -198,7 +206,7 @@ where
})()
.await;

let handler_env: HandlerEnv = HandlerEnv::try_parse()
let handler_env: HandlerEnv = HandlerEnv::try_parse_from(empty::<OsString>())
.context("An error occured while parsing environment variable for handler")?;

lambda_runtime::run(service_fn(|event: LambdaEvent<SqsEvent>| async {
Expand Down

0 comments on commit e8b1641

Please sign in to comment.