-
Notifications
You must be signed in to change notification settings - Fork 361
Support internal Lambda extensions #744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "extension-internal-flush" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
aws_lambda_events = { path = "../../lambda-events" } | ||
lambda-extension = { path = "../../lambda-extension" } | ||
lambda_runtime = { path = "../../lambda-runtime" } | ||
serde = "1.0.136" | ||
tokio = { version = "1", features = ["macros", "sync"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# AWS Lambda runtime + internal extension example | ||
|
||
This example demonstrates how to build an AWS Lambda function that includes a | ||
[Lambda internal extension](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html). | ||
Unlike external extensions that run as separate processes, an internal extension runs within the | ||
main runtime process. | ||
|
||
One use case for internal extensions is to flush logs or telemetry data after the Lambda runtime | ||
handler has finished processing an event but before the execution environment is frozen awaiting the | ||
arrival of the next event. Without an explicit flush, telemetry data may never be sent since the | ||
execution environment will remain frozen and eventually be terminated if no additional events arrive. | ||
|
||
Note that for | ||
[synchronous](https://docs.aws.amazon.com/lambda/latest/dg/invocation-sync.html) Lambda invocations | ||
(e.g., via | ||
[Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integrations.html)), | ||
the Lambda service returns the response to the caller immediately. Extensions may continue to run | ||
without introducing an observable delay. | ||
|
||
## Build & Deploy | ||
|
||
1. Install [cargo-lambda](https://github.com/cargo-lambda/cargo-lambda#installation) | ||
2. Build the extension with `cargo lambda build --release` | ||
3. Deploy the function to AWS Lambda with `cargo lambda deploy --iam-role YOUR_ROLE` | ||
|
||
The last command will give you an ARN for the extension layer that you can use in your functions. | ||
|
||
## Build for ARM 64 | ||
|
||
Build the extension with `cargo lambda build --release --arm64` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use anyhow::anyhow; | ||
use aws_lambda_events::sqs::{SqsBatchResponse, SqsEventObj}; | ||
use lambda_extension::{service_fn, Error, Extension, NextEvent}; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; | ||
use tokio::sync::Mutex; | ||
|
||
use std::sync::Arc; | ||
|
||
/// Implements an internal Lambda extension to flush logs/telemetry after each request. | ||
struct FlushExtension { | ||
request_done_receiver: Mutex<UnboundedReceiver<()>>, | ||
} | ||
|
||
impl FlushExtension { | ||
pub fn new(request_done_receiver: UnboundedReceiver<()>) -> Self { | ||
Self { | ||
request_done_receiver: Mutex::new(request_done_receiver), | ||
} | ||
} | ||
|
||
pub async fn invoke(&self, event: lambda_extension::LambdaEvent) -> Result<(), Error> { | ||
match event.next { | ||
// NB: Internal extensions only support the INVOKE event. | ||
NextEvent::Shutdown(shutdown) => { | ||
return Err(anyhow!("extension received unexpected SHUTDOWN event: {:?}", shutdown).into()); | ||
} | ||
NextEvent::Invoke(_e) => {} | ||
} | ||
|
||
eprintln!("[extension] waiting for event to be processed"); | ||
|
||
// Wait for runtime to finish processing event. | ||
self.request_done_receiver | ||
.lock() | ||
.await | ||
.recv() | ||
.await | ||
.ok_or_else(|| anyhow!("channel is closed"))?; | ||
|
||
eprintln!("[extension] flushing logs and telemetry"); | ||
|
||
// <flush logs and telemetry here> | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// Object that you send to SQS and plan to process with the function. | ||
#[derive(Debug, Deserialize, Serialize)] | ||
struct Data { | ||
a: String, | ||
b: i64, | ||
} | ||
|
||
/// Implements the main event handler for processing events from an SQS queue. | ||
struct EventHandler { | ||
request_done_sender: UnboundedSender<()>, | ||
} | ||
|
||
impl EventHandler { | ||
pub fn new(request_done_sender: UnboundedSender<()>) -> Self { | ||
Self { request_done_sender } | ||
} | ||
|
||
pub async fn invoke( | ||
&self, | ||
event: lambda_runtime::LambdaEvent<SqsEventObj<Data>>, | ||
) -> Result<SqsBatchResponse, Error> { | ||
let data = &event.payload.records[0].body; | ||
eprintln!("[runtime] received event {data:?}"); | ||
|
||
// <process event here> | ||
|
||
// Notify the extension to flush traces. | ||
self.request_done_sender.send(()).map_err(Box::new)?; | ||
|
||
Ok(SqsBatchResponse::default()) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
let (request_done_sender, request_done_receiver) = unbounded_channel::<()>(); | ||
|
||
let flush_extension = Arc::new(FlushExtension::new(request_done_receiver)); | ||
let extension = Extension::new() | ||
// Internal extensions only support INVOKE events. | ||
.with_events(&["INVOKE"]) | ||
.with_events_processor(service_fn(|event| { | ||
let flush_extension = flush_extension.clone(); | ||
async move { flush_extension.invoke(event).await } | ||
})) | ||
// Internal extension names MUST be unique within a given Lambda function. | ||
.with_extension_name("internal-flush") | ||
// Extensions MUST be registered before calling lambda_runtime::run(), which ends the Init | ||
// phase and begins the Invoke phase. | ||
.register() | ||
.await?; | ||
|
||
let handler = Arc::new(EventHandler::new(request_done_sender)); | ||
|
||
tokio::try_join!( | ||
lambda_runtime::run(service_fn(|event| { | ||
let handler = handler.clone(); | ||
async move { handler.invoke(event).await } | ||
})), | ||
extension.run(), | ||
)?; | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new type seemed unavoidable since
Extension::register
has to pass ownership of all the logging and telemetry fields to the spawned background tasks. These are the remaining fields to be used in the invoke phase.