The AWS Lambda Runtime for Swift v2 is now officially available.
Overview
Swift AWS Lambda Runtime v2 introduces a complete redesign of the API with async/await-first architecture, response streaming support, background task execution, and seamless integration with Swift Service Lifecycle. This release prioritizes developer experience, and structured concurrency.
🚀 v2 - Major New Features
Complete Swift 6 Concurrency Integration
- Full async/await support throughout the entire API surface
- Structured concurrency with proper task management and cancellation
- Swift 6 compatibility with complete concurrency checking
- Non-blocking I/O foundation built on SwiftNIO for optimal performance
- Own the main() function for complete control over initialization
// Clean async/await API
let runtime = LambdaRuntime { (event: Input, context: LambdaContext) async throws -> Output in
let result = try await someAsyncOperation()
return Output(data: result)
}
try await runtime.run()
Response Streaming Support
- Stream large responses incrementally to improve time-to-first-byte (TTFB) performance
- 200MB soft limit for streamed responses vs 6MB for buffered responses
- Reduced memory usage for large responses
- Support for custom HTTP status codes and headers in the streamed response
let runtime = LambdaRuntime { (event: StreamingRequest, responseWriter, context: LambdaContext) in
for i in 1...10 {
try await responseWriter.write(ByteBuffer(string: "Message \(i)\n"))
try await Task.sleep(for: .milliseconds(500))
}
try await responseWriter.finish()
}
Background Task Execution
- Execute code after returning response without affecting response latency
- Structured concurrency approach following Swift best practices
let runtime = LambdaRuntime { (event: Input, outputWriter: some LambdaResponseWriter<Output>, context: LambdaContext) in
// Return response immediately
try await output.write(Greeting(message: event.message))
// Execute background work
try await performAnalytics(event)
try await updateCache()
}
Swift Service Lifecycle Integration
- Structured dependency with ServiceGroup
- Graceful shutdown handling with proper resource cleanup
- Eliminate LambdaTerminator complexity
let postgresClient = PostgresClient()
let runtime = LambdaRuntime { (event: Input, context: LambdaContext) in
try await postgresClient.query("SELECT * FROM users")
}
let serviceGroup = ServiceGroup(
services: [postgresClient, runtime],
configuration: .init(gracefulShutdownSignals: [.sigterm])
)
try await serviceGroup.run()
Changes between 2.0.0-beta.1 and 2.0.0
What's Changed
SemVer Major
- Use a struct for ClientContext (fix #169) by @sebsto in #539
- Remove dependency on DispatchWallTime (fix #384) by @sebsto in #540
- Revert streaming codable handler and provide it as an example, not an API by @sebsto in #549
- [fix] The library must compile when no traits are enabled by @sebsto in #563
SemVer Minor
- Performance Test the invocation loop (fix #377) by @sebsto in #542
- Add hummingbird Lambda example by @sebsto in #544
- Propagate Connection Closed Information up to top-level (fix #465) by @sebsto in #545
- add support for LOCAL_LAMBDA_PORT by @sebsto in #557
- Bump Amazon RDS version 19 PG 17 by @sebsto in #561
- Update toolchain and doc for 6.2 by @sebsto in #564
- Apply recommendation for security and reliability by @sebsto in #573
SemVer Patch
- fix: Fix deadline header with correct instant value (#551) by @sebsto in #552
- Split LambdaRuntimeClient in two files for easier reading by @sebsto in #554
- Rename Tests'
timeout()
function by @sebsto in #555 - refactor the Swift Settings in
Package.swift
by @sebsto in #558 - Allow compiling on macOS by @fabianfett in #559
- [Example] Use smaller containers in all examples by @sebsto in #571
- [Example] Add APIGatewayV1 example by @everyplace in #569
- Rename APIGateway example to APIGatewayV2 by @sebsto in #575
Other Changes
- Fix link (Response Streaming) in readme.md by @natanrolnik in #543
- [doc] Updated streaming response limit to 200 MB by @dsplatonov in #548
New Contributors
- @natanrolnik made their first contribution in #543
- @dsplatonov made their first contribution in #548
- @everyplace made their first contribution in #569
Full Changelog: 2.0.0-beta.1...2.0.0