Swift AWS Lambda Runtime v2.0.0-beta.1 is available

After 11 months of work, we're pleased to announce the beta availability of the Swift Lambda Runtime v2 for Swift 6.x.

The v2 API prioritizes the following principles:

  • Readability and maintainability: Extensive use of async/await improves code clarity and simplifies maintenance.
  • Developer control: Developers own the main() function and have the flexibility to inject dependencies into the LambdaRuntime. This allows you to manage service lifecycles efficiently using Swift Service Lifecycle for coordinated startup and shutdown sequences.
  • Simplified Codable support: The LambdaCodableAdapter struct eliminates the need for verbose boilerplate code when encoding and decoding events and responses.
  • Strictly apply Swift 6 structured concurrency

A big thank you to

  • @Aryan Shah who has written the majority of the heavy lifting to move from v1 to v2
  • @fabianfett who led the redesign and early implementation of the new API
  • @adam-fowler for your countless and patient code reviews and feedback.

Starting from this version, the Swift runtime for AWS Lambda supports Swift version 6.0 and more recent.

New Capabilities

The v2 API introduces three new features:

  • Response Streaming: This functionality is ideal for handling large responses that need to be sent incrementally, or to reduce the time to first byte sent to the client. You have the possibility to send custom HTTP headers and status code when the function is exposed through a function URL (as of today, only the NodeJS and the Swift runtime support custom HTTP headers and status from streaming Function URLs)
  • Background Work: Schedule tasks to run after returning a response to the AWS Lambda control plane.
  • Support Swift Service Lifecycle. It allows to nicely coordinate this library's startup and shutdown sequence with other Swift libraries, such as, for example, the PostgresNIO database driver.

These new capabilities provide greater flexibility and control when building serverless functions in Swift with the swift-aws-lambda-runtime library.

New Contributors

Full Changelog: Comparing 1.0.0-alpha.3...2.0.0-beta.1 · swift-server/swift-aws-lambda-runtime · GitHub

13 Likes

@sebsto this is awesome! I understand it's a bit early, but is there a migration guide for v1 or even (since that was never formally released) pre-v1 projects?

I’ve tried out the v2 API in a side project & really enjoyed it. I’m also excited about the new capabilities, though I haven’t used them yet.

Side note: the “full changelog” link is empty — I think you maybe meant Comparing 1.0.0-alpha.3...2.0.0-beta.1 · swift-server/swift-aws-lambda-runtime · GitHub ?

1 Like

Hello @willft

Thank you for your feedback.
I was not planning to write a migration guide, but if you think it might be useful, I can add a task for that.
There is a very short bullet point list I wrote in the release notes.

Basically,

  • There is no need to extend the LambdaHandler or SimpleLambdaHandler protocol anymore
  • You provide the code entry point, either by naming your file main.swift (see HelloJSON example) or using @main (see ServiceLifeCycle+Postgres example)
  • You create a LambdaRuntime object
  • You move the old handle(event:context:) function to a closure passed to LambdaRuntime.init()
  • You call try await runtime.run()

Version 1:

@main
struct HelloWorldHandler: SimpleLambdaHandler {
    func handle(_ event: String, context: LambdaContext) async throws -> String {
        "hello \(event)"
    }
}

Version 2:

let runtime = LambdaRuntime {
    (event: String, context: LambdaContext) in
    "Hello \(event)"
}

try await runtime.run()

The API provides you with different handler types. The easiest to use is the one you pass as a closure to the LambdaRuntime, but you can create your handler separately:

func handler(event: String, context: LambdaContext) async throws -> String {
    "Hello \(event)"
}

let runtime = LambdaRuntime(body: handler)
try await runtime.run()

There is an extensive list of examples in the Examples directory of the library. Let us know if you want us to write additional code examples.

As usual, the Lambda Event library provides you with a set of the most common event types when an AWS service invokes your Lambda function.

Let me know if the above helps you or if you need more details tailored towards your specific usage.

1 Like

Ah no, that’s perfect. Thanks @sebsto !

1 Like