-
Notifications
You must be signed in to change notification settings - Fork 118
Add support for Lambda Managed Instances without changing the public API #623
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull request overview
This pull request adds support for Lambda Managed Instances, enabling Swift Lambda functions to handle multiple concurrent invocations within a single execution environment. The implementation introduces thread-safe runtime components with Sendable conformance while maintaining backward compatibility with the existing single-threaded Lambda runtime.
Key Changes:
- Introduced
LambdaManagedRuntimeclass with thread-safe handler execution supporting concurrent invocations based on theAWS_LAMBDA_MAX_CONCURRENCYenvironment variable - Created Sendable-conforming adapter types (
LambdaHandlerAdapterSendable,LambdaCodableAdapterSendable,LambdaJSONEventDecoderSendable,LambdaJSONOutputEncoderSendable) to ensure safe concurrent execution - Refactored
LambdaRuntimeto extract reusable methods (startRuntimeInterfaceClient,startLocalServer) shared withLambdaManagedRuntime
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
scripts/extract_aws_credentials.sh |
Removed script (likely no longer needed) |
Tests/AWSLambdaRuntimeTests/Utils.swift |
Added test helper for creating LambdaContext instances |
Tests/AWSLambdaRuntimeTests/LambdaManagedRuntimeTests.swift |
Comprehensive tests for concurrent handler execution, Sendable constraints, and thread-safe adapters |
Sources/AWSLambdaRuntime/Runtime/LambdaRuntime.swift |
Refactored to extract reusable methods and renamed atomic guard variable for clarity |
Sources/AWSLambdaRuntime/Runtime/LambdaRuntime+Codable.swift |
Added Sendable conformance to VoidEncoder |
Sources/AWSLambdaRuntime/ManagedRuntime/LambdaManagedRuntimeHandlers.swift |
Introduced ClosureHandlerSendable for thread-safe closure-based handlers |
Sources/AWSLambdaRuntime/ManagedRuntime/LambdaManagedRuntime.swift |
Core implementation of managed runtime with concurrency detection and multiple RIC support |
Sources/AWSLambdaRuntime/ManagedRuntime/LambdaManagedRuntime+ServiceLifecycle.swift |
ServiceLifecycle integration for managed runtime |
Sources/AWSLambdaRuntime/ManagedRuntime/LambdaManagedRuntime+Codable.swift |
Thread-safe adapter implementations for managed runtime |
Sources/AWSLambdaRuntime/FoundationSupport/LambdaRuntime+JSON.swift |
Convenience initializers for LambdaRuntime with JSON encoding/decoding |
Sources/AWSLambdaRuntime/FoundationSupport/LambdaManagedRuntime+JSON.swift |
Sendable JSON encoder/decoder implementations and convenience initializers for managed runtime |
Sources/AWSLambdaRuntime/FoundationSupport/LambdaHandler+JSON.swift |
Extracted common JSON handler support to separate file |
[email protected] |
Added ManagedRuntimeSupport trait and updated dependency versions |
Package.swift |
Added ManagedRuntimeSupport trait to default enabled traits and updated dependency versions |
Examples/Streaming+Codable/Tests/LambdaStreamingCodableTests.swift |
Updated logger label for consistency |
Examples/ManagedInstances/template.yaml |
SAM template for deploying managed instances examples |
Examples/ManagedInstances/Sources/Streaming/main.swift |
Streaming response example for managed instances |
Examples/ManagedInstances/Sources/HelloJSON/main.swift |
Simple JSON request/response example for managed instances |
Examples/ManagedInstances/Sources/BackgroundTasks/main.swift |
Background processing example for managed instances |
Examples/ManagedInstances/README.md |
Documentation for deploying and testing managed instances examples |
Examples/ManagedInstances/Package.swift |
Package definition for managed instances examples |
Examples/ManagedInstances/.gitignore |
Ignore patterns for managed instances examples |
.github/workflows/pull_request.yml |
Added ManagedInstances to CI examples list |
Comments suppressed due to low confidence (3)
Sources/AWSLambdaRuntime/FoundationSupport/LambdaManagedRuntime+JSON.swift:149
- Inconsistent default logger label. This initializer uses "LambdaRuntime" as the default logger label, but the previous initializer on line 122 uses "LambdaManagedRuntime". For consistency, this should likely also use "LambdaManagedRuntime" since it's an extension on LambdaManagedRuntime.
Sources/AWSLambdaRuntime/FoundationSupport/LambdaManagedRuntime+JSON.swift:207 - Inconsistent default logger label. This initializer uses "LambdaRuntime" as the default logger label, but line 122 uses "LambdaManagedRuntime". For consistency across the LambdaManagedRuntime extension, this should likely also use "LambdaManagedRuntime".
Sources/AWSLambdaRuntime/FoundationSupport/LambdaManagedRuntime+JSON.swift:176 - Inconsistent default logger label. This initializer uses "LambdaRuntime" as the default logger label, but line 122 uses "LambdaManagedRuntime". For consistency across the LambdaManagedRuntime extension, this should likely also use "LambdaManagedRuntime".
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
AWS launched Lambda Managed Instance, i.e Lambda functions running on EC2 instances.
This comes with a major change in the programming model as function handlers are now allowed to run concurrently on the same machine (multiple in flight events being processed in parallel in the same execution environment). The maximum concurrency per runtime environment is controlled by the user.
This PR adds support for running multiple Runtime Interface Clients (RICs) concurrently when deployed on Lambda Managed Instances, enabling the runtime to handle multiple invocations simultaneously within a single execution environment.
This PR is a followup to #617 which used another approach to support Lambda Managed Instances by changing the public API and requiring that all handlers must conform to
Sendable. The original PR was closed as we agreed that only a fraction of the Lambda functions will be deployed on EC2 and it was not worth adding aSendablerequirement for all.Changes
Introduced thread-safe LambdaManagedRuntime: Created new Sendable-conforming runtime class that supports concurrent handler execution with atomic guards to prevent multiple runtime instances and thread-safe handler requirements (
Handler: StreamingLambdaHandler & Sendable)Added Sendable adapter types: Implemented
LambdaHandlerAdapterSendable,LambdaCodableAdapterSendable,LambdaJSONEventDecoderSendable, andLambdaJSONOutputEncoderSendable- all thread-safe versions of existing adapters that enforceSendableconformance for concurrent execution environmentsEnhanced handler protocols for concurrency: Extended handler protocols to support
Sendableconstraints and concurrent response writing throughLambdaResponseStreamWriter & Sendable, enabling safe multi-threaded invocation processingImplemented ServiceLifecycle integration: Added managed runtime support for structured concurrency lifecycle management, allowing proper startup/shutdown coordination in multi-concurrent environments
Created comprehensive Lambda Managed Instances examples: Built three demonstration functions showcasing concurrent execution capabilities, streaming responses, and background processing patterns specific to the new managed instances deployment model
Context
Lambda Managed Instances support multi-concurrent invocations where multiple invocations execute simultaneously within the same execution environment. The runtime now detects the configured concurrency level and launches the appropriate number of RICs to handle concurrent requests efficiently.
When
AWS_LAMBDA_MAX_CONCURRENCYis 1 or unset, the runtime maintains the existing single-threaded behaviour for optimal performance on traditional Lambda deployments.