Skip to content

Conversation

@sebsto
Copy link
Collaborator

@sebsto sebsto commented Jan 4, 2026

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 a Sendable requirement 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, and LambdaJSONOutputEncoderSendable - all thread-safe versions of existing adapters that enforce Sendable conformance for concurrent execution environments

  • Enhanced handler protocols for concurrency: Extended handler protocols to support Sendable constraints and concurrent response writing through LambdaResponseStreamWriter & Sendable, enabling safe multi-threaded invocation processing

  • Implemented 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_CONCURRENCY is 1 or unset, the runtime maintains the existing single-threaded behaviour for optimal performance on traditional Lambda deployments.

@sebsto sebsto self-assigned this Jan 4, 2026
@sebsto sebsto added the 🆕 semver/minor Adds new public API. label Jan 4, 2026
Copy link
Contributor

Copilot AI left a 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 LambdaManagedRuntime class with thread-safe handler execution supporting concurrent invocations based on the AWS_LAMBDA_MAX_CONCURRENCY environment variable
  • Created Sendable-conforming adapter types (LambdaHandlerAdapterSendable, LambdaCodableAdapterSendable, LambdaJSONEventDecoderSendable, LambdaJSONOutputEncoderSendable) to ensure safe concurrent execution
  • Refactored LambdaRuntime to extract reusable methods (startRuntimeInterfaceClient, startLocalServer) shared with LambdaManagedRuntime

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant