From 91cdbbebc78cf6d9b3bcaaaf3d7ba8ff5f85a528 Mon Sep 17 00:00:00 2001 From: tom doron Date: Thu, 29 Jul 2021 13:16:49 -0700 Subject: [PATCH] split events into spearate package motivation: the runtime library has a stabel API while the events are still moving target, so in order to provide a 1.0 stable version we should separate them out changes: * import events module from the swift-aws-lambda-runtime, including git history * update docker setup * remove redundant examples and scripts --- CONTRIBUTORS.txt | 12 +- Package.swift | 47 +--- docker/docker-compose.1804.52.yaml | 9 +- docker/docker-compose.al2.52.yaml | 9 +- docker/docker-compose.al2.53.yaml | 9 +- docker/docker-compose.al2.54.yaml | 9 +- docker/docker-compose.al2.55.yaml | 9 +- docker/docker-compose.al2.main.yaml | 14 +- docker/docker-compose.yaml | 7 - readme.md | 351 ++-------------------------- scripts/linux_performance_setup.sh | 46 ---- scripts/performance_test.sh | 127 ---------- 12 files changed, 46 insertions(+), 603 deletions(-) delete mode 100755 scripts/linux_performance_setup.sh delete mode 100755 scripts/performance_test.sh diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index c2e31f9..f538259 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -18,16 +18,26 @@ needs to be listed here. - Bryan Moffatt - Christoph Walcher - Colton Schlosser +- DwayneCoussement +- DwayneCoussement - Eneko Alonso -- Fabian Fett +- Fabian Fett +- Filipp Fediakov - George Barnett +- Johannes Bosecker - Johannes Weiss +- Josh <29730338+mr-j-tree@users.noreply.github.com> +- ML <44809298+mufumade@users.noreply.github.com> +- Matt Massicotte <85322+mattmassicotte@users.noreply.github.com> - Max Desiatov - Norman Maurer +- Ralph Küpper - Ro-M - Tomer Doron - Zhibin Cai - pmarrufo +- pokryfka <5090827+pokryfka@users.noreply.github.com> +- pokryfka - tachyonics **Updating this list** diff --git a/Package.swift b/Package.swift index 962a3ce..4181b13 100644 --- a/Package.swift +++ b/Package.swift @@ -3,56 +3,13 @@ import PackageDescription let package = Package( - name: "swift-aws-lambda-runtime", + name: "swift-aws-lambda-events", products: [ - // this library exports `AWSLambdaRuntimeCore` and adds Foundation convenience methods - .library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]), - // this has all the main functionality for lambda and it does not link Foundation - .library(name: "AWSLambdaRuntimeCore", targets: ["AWSLambdaRuntimeCore"]), - // common AWS events .library(name: "AWSLambdaEvents", targets: ["AWSLambdaEvents"]), - // for testing only - .library(name: "AWSLambdaTesting", targets: ["AWSLambdaTesting"]), - ], - dependencies: [ - .package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.30.0")), - .package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.4.2")), - .package(url: "https://github.com/swift-server/swift-backtrace.git", .upToNextMajor(from: "1.2.3")), ], + dependencies: [], targets: [ - .target(name: "AWSLambdaRuntime", dependencies: [ - .byName(name: "AWSLambdaRuntimeCore"), - .product(name: "NIO", package: "swift-nio"), - .product(name: "NIOFoundationCompat", package: "swift-nio"), - ]), - .target(name: "AWSLambdaRuntimeCore", dependencies: [ - .product(name: "Logging", package: "swift-log"), - .product(name: "Backtrace", package: "swift-backtrace"), - .product(name: "NIOHTTP1", package: "swift-nio"), - .product(name: "_NIOConcurrency", package: "swift-nio"), - ]), - .testTarget(name: "AWSLambdaRuntimeCoreTests", dependencies: [ - .byName(name: "AWSLambdaRuntimeCore"), - .product(name: "NIOTestUtils", package: "swift-nio"), - .product(name: "NIOFoundationCompat", package: "swift-nio"), - ]), - .testTarget(name: "AWSLambdaRuntimeTests", dependencies: [ - .byName(name: "AWSLambdaRuntimeCore"), - .byName(name: "AWSLambdaRuntime"), - ]), .target(name: "AWSLambdaEvents", dependencies: []), .testTarget(name: "AWSLambdaEventsTests", dependencies: ["AWSLambdaEvents"]), - // testing helper - .target(name: "AWSLambdaTesting", dependencies: [ - .byName(name: "AWSLambdaRuntime"), - .product(name: "NIO", package: "swift-nio"), - ]), - .testTarget(name: "AWSLambdaTestingTests", dependencies: ["AWSLambdaTesting"]), - // for perf testing - .target(name: "MockServer", dependencies: [ - .product(name: "NIOHTTP1", package: "swift-nio"), - ]), - .target(name: "StringSample", dependencies: ["AWSLambdaRuntime"]), - .target(name: "CodableSample", dependencies: ["AWSLambdaRuntime"]), ] ) diff --git a/docker/docker-compose.1804.52.yaml b/docker/docker-compose.1804.52.yaml index 29c7727..636c69b 100644 --- a/docker/docker-compose.1804.52.yaml +++ b/docker/docker-compose.1804.52.yaml @@ -3,7 +3,7 @@ version: "3" services: runtime-setup: - image: swift-aws-lambda:18.04-5.2 + image: swift-aws-lambda-events:18.04-5.2 build: dockerfile: Dockerfile.ubuntu args: @@ -11,10 +11,7 @@ services: swift_version: "5.2" test: - image: swift-aws-lambda:18.04-5.2 - - test-samples: - image: swift-aws-lambda:18.04-5.2 + image: swift-aws-lambda-events:18.04-5.2 shell: - image: swift-aws-lambda:18.04-5.2 + image: swift-aws-lambda-events:18.04-5.2 diff --git a/docker/docker-compose.al2.52.yaml b/docker/docker-compose.al2.52.yaml index 30f1208..35e6089 100644 --- a/docker/docker-compose.al2.52.yaml +++ b/docker/docker-compose.al2.52.yaml @@ -3,16 +3,13 @@ version: "3" services: runtime-setup: - image: swift-aws-lambda:al2-5.2 + image: swift-aws-lambda-events:al2-5.2 build: args: swift_version: "5.2" test: - image: swift-aws-lambda:al2-5.2 - - test-samples: - image: swift-aws-lambda:al2-5.2 + image: swift-aws-lambda-events:al2-5.2 shell: - image: swift-aws-lambda:al2-5.2 + image: swift-aws-lambda-events:al2-5.2 diff --git a/docker/docker-compose.al2.53.yaml b/docker/docker-compose.al2.53.yaml index 628a189..bad3f17 100644 --- a/docker/docker-compose.al2.53.yaml +++ b/docker/docker-compose.al2.53.yaml @@ -3,16 +3,13 @@ version: "3" services: runtime-setup: - image: swift-aws-lambda:al2-5.3 + image: swift-aws-lambda-events:al2-5.3 build: args: swift_version: "5.3" test: - image: swift-aws-lambda:al2-5.3 - - test-samples: - image: swift-aws-lambda:al2-5.3 + image: swift-aws-lambda-events:al2-5.3 shell: - image: swift-aws-lambda:al2-5.3 + image: swift-aws-lambda-events:al2-5.3 diff --git a/docker/docker-compose.al2.54.yaml b/docker/docker-compose.al2.54.yaml index cb300eb..6c0b1d7 100644 --- a/docker/docker-compose.al2.54.yaml +++ b/docker/docker-compose.al2.54.yaml @@ -3,16 +3,13 @@ version: "3" services: runtime-setup: - image: swift-aws-lambda:al2-5.4 + image: swift-aws-lambda-events:al2-5.4 build: args: swift_version: "5.4" test: - image: swift-aws-lambda:al2-5.4 - - test-samples: - image: swift-aws-lambda:al2-5.4 + image: swift-aws-lambda-events:al2-5.4 shell: - image: swift-aws-lambda:al2-5.4 + image: swift-aws-lambda-events:al2-5.4 diff --git a/docker/docker-compose.al2.55.yaml b/docker/docker-compose.al2.55.yaml index 7877865..a81f929 100644 --- a/docker/docker-compose.al2.55.yaml +++ b/docker/docker-compose.al2.55.yaml @@ -3,16 +3,13 @@ version: "3" services: runtime-setup: - image: swift-aws-lambda:al2-5.5 + image: swift-aws-lambda-events:al2-5.5 build: args: base_image: "swiftlang/swift:nightly-5.5-amazonlinux2" test: - image: swift-aws-lambda:al2-5.5 - - test-samples: - image: swift-aws-lambda:al2-5.5 + image: swift-aws-lambda-events:al2-5.5 shell: - image: swift-aws-lambda:al2-5.5 + image: swift-aws-lambda-events:al2-5.5 diff --git a/docker/docker-compose.al2.main.yaml b/docker/docker-compose.al2.main.yaml index 741c8a4..8f96c15 100644 --- a/docker/docker-compose.al2.main.yaml +++ b/docker/docker-compose.al2.main.yaml @@ -3,21 +3,13 @@ version: "3" services: runtime-setup: - image: swift-aws-lambda:al2-main + image: swift-aws-lambda-events:al2-main build: args: base_image: "swiftlang/swift:nightly-main-amazonlinux2" test: - image: swift-aws-lambda:al2-main - command: /bin/bash -cl "swift test --enable-test-discovery -Xswiftc -warnings-as-errors $${SANITIZER_ARG-} -Xswiftc -Xfrontend -Xswiftc -enable-experimental-concurrency" - - test-samples: - image: swift-aws-lambda:al2-main - command: >- - /bin/bash -clx " - swift build -Xswiftc -Xfrontend -Xswiftc -enable-experimental-concurrency --package-path Examples/LambdaFunctions && - swift build -Xswiftc -Xfrontend -Xswiftc -enable-experimental-concurrency --package-path Examples/LocalDebugging/MyLambda" + image: swift-aws-lambda-events:al2-main shell: - image: swift-aws-lambda:al2-main + image: swift-aws-lambda-events:al2-main diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 122ccd9..d161dd0 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -30,13 +30,6 @@ services: <<: *common command: /bin/bash -cl "swift test --enable-test-discovery -Xswiftc -warnings-as-errors $${SANITIZER_ARG-}" - test-samples: - <<: *common - command: >- - /bin/bash -clx " - swift build --package-path Examples/LambdaFunctions && - swift build --package-path Examples/LocalDebugging/MyLambda" - # util shell: diff --git a/readme.md b/readme.md index cf1981a..4036342 100644 --- a/readme.md +++ b/readme.md @@ -1,339 +1,14 @@ -# Swift AWS Lambda Runtime +# Swift AWS Lambda Events -Many modern systems have client components like iOS, macOS or watchOS applications as well as server components that those clients interact with. Serverless functions are often the easiest and most efficient way for client application developers to extend their applications into the cloud. - -Serverless functions are increasingly becoming a popular choice for running event-driven or otherwise ad-hoc compute tasks in the cloud. They power mission critical microservices and data intensive workloads. In many cases, serverless functions allow developers to more easily scale and control compute costs given their on-demand nature. - -When using serverless functions, attention must be given to resource utilization as it directly impacts the costs of the system. This is where Swift shines! With its low memory footprint, deterministic performance, and quick start time, Swift is a fantastic match for the serverless functions architecture. - -Combine this with Swift's developer friendliness, expressiveness, and emphasis on safety, and we have a solution that is great for developers at all skill levels, scalable, and cost effective. +## Overview Swift AWS Lambda Runtime was designed to make building Lambda functions in Swift simple and safe. The library is an implementation of the [AWS Lambda Runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html) and uses an embedded asynchronous HTTP Client based on [SwiftNIO](http://github.com/apple/swift-nio) that is fine-tuned for performance in the AWS Runtime context. The library provides a multi-tier API that allows building a range of Lambda functions: From quick and simple closures to complex, performance-sensitive event handlers. -## Project status - -This is the beginning of a community-driven open-source project actively seeking contributions. -While the core API is considered stable, the API may still evolve as we get closer to a `1.0` version. -There are several areas which need additional attention, including but not limited to: - -* Further performance tuning -* Additional trigger events -* Additional documentation and best practices -* Additional examples - -## Getting started - -If you have never used AWS Lambda or Docker before, check out this [getting started guide](https://fabianfett.de/getting-started-with-swift-aws-lambda-runtime) which helps you with every step from zero to a running Lambda. - -First, create a SwiftPM project and pull Swift AWS Lambda Runtime as dependency into your project - - ```swift - // swift-tools-version:5.2 - - import PackageDescription - - let package = Package( - name: "my-lambda", - products: [ - .executable(name: "MyLambda", targets: ["MyLambda"]), - ], - dependencies: [ - .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "0.1.0"), - ], - targets: [ - .target(name: "MyLambda", dependencies: [ - .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), - ]), - ] - ) - ``` - -Next, create a `main.swift` and implement your Lambda. - - ### Using Closures - - The simplest way to use `AWSLambdaRuntime` is to pass in a closure, for example: - - ```swift - // Import the module - import AWSLambdaRuntime - - // in this example we are receiving and responding with strings - Lambda.run { (context, name: String, callback: @escaping (Result) -> Void) in - callback(.success("Hello, \(name)")) - } - ``` - - More commonly, the event would be a JSON, which is modeled using `Codable`, for example: - - ```swift - // Import the module - import AWSLambdaRuntime - - // Request, uses Codable for transparent JSON encoding - private struct Request: Codable { - let name: String - } - - // Response, uses Codable for transparent JSON encoding - private struct Response: Codable { - let message: String - } - - // In this example we are receiving and responding with `Codable`. - Lambda.run { (context, request: Request, callback: @escaping (Result) -> Void) in - callback(.success(Response(message: "Hello, \(request.name)"))) - } - ``` - - Since most Lambda functions are triggered by events originating in the AWS platform like `SNS`, `SQS` or `APIGateway`, the package also includes a `AWSLambdaEvents` module that provides implementations for most common AWS event types further simplifying writing Lambda functions. For example, handling an `SQS` message: - - ```swift - // Import the modules - import AWSLambdaRuntime - import AWSLambdaEvents - - // In this example we are receiving an SQS Event, with no response (Void). - Lambda.run { (context, message: SQS.Event, callback: @escaping (Result) -> Void) in - ... - callback(.success(Void())) - } - ``` - - Modeling Lambda functions as Closures is both simple and safe. Swift AWS Lambda Runtime will ensure that the user-provided code is offloaded from the network processing thread such that even if the code becomes slow to respond or gets hang, the underlying process can continue to function. This safety comes at a small performance penalty from context switching between threads. In many cases, the simplicity and safety of using the Closure based API is often preferred over the complexity of the performance-oriented API. - -### Using EventLoopLambdaHandler - - Performance sensitive Lambda functions may choose to use a more complex API which allows user code to run on the same thread as the networking handlers. Swift AWS Lambda Runtime uses [SwiftNIO](https://github.com/apple/swift-nio) as its underlying networking engine which means the APIs are based on [SwiftNIO](https://github.com/apple/swift-nio) concurrency primitives like the `EventLoop` and `EventLoopFuture`. For example: - - ```swift - // Import the modules - import AWSLambdaRuntime - import AWSLambdaEvents - import NIO - - // Our Lambda handler, conforms to EventLoopLambdaHandler - struct Handler: EventLoopLambdaHandler { - typealias In = SNS.Message // Request type - typealias Out = Void // Response type - - // In this example we are receiving an SNS Message, with no response (Void). - func handle(context: Lambda.Context, event: In) -> EventLoopFuture { - ... - context.eventLoop.makeSucceededFuture(Void()) - } - } - - Lambda.run(Handler()) - ``` - - Beyond the small cognitive complexity of using the `EventLoopFuture` based APIs, note these APIs should be used with extra care. An `EventLoopLambdaHandler` will execute the user code on the same `EventLoop` (thread) as the library, making processing faster but requiring the user code to never call blocking APIs as it might prevent the underlying process from functioning. - -## Deploying to AWS Lambda - -To deploy Lambda functions to AWS Lambda, you need to compile the code for Amazon Linux which is the OS used on AWS Lambda microVMs, package it as a Zip file, and upload to AWS. - -AWS offers several tools to interact and deploy Lambda functions to AWS Lambda including [SAM](https://aws.amazon.com/serverless/sam/) and the [AWS CLI](https://aws.amazon.com/cli/). The [Examples Directory](/Examples) includes complete sample build and deployment scripts that utilize these tools. - -Note the examples mentioned above use dynamic linking, therefore bundle the required Swift libraries in the Zip package along side the executable. You may choose to link the Lambda function statically (using `-static-stdlib`) which could improve performance but requires additional linker flags. - -To build the Lambda function for Amazon Linux, use the Docker image published by Swift.org on [Swift toolchains and Docker images for Amazon Linux 2](https://swift.org/download/), as demonstrated in the examples. - -## Architecture - -The library defines three protocols for the implementation of a Lambda Handler. From low-level to more convenient: - -### ByteBufferLambdaHandler - -An `EventLoopFuture` based processing protocol for a Lambda that takes a `ByteBuffer` and returns a `ByteBuffer?` asynchronously. - -`ByteBufferLambdaHandler` is the lowest level protocol designed to power the higher level `EventLoopLambdaHandler` and `LambdaHandler` based APIs. Users are not expected to use this protocol, though some performance sensitive applications that operate at the `ByteBuffer` level or have special serialization needs may choose to do so. - -```swift -public protocol ByteBufferLambdaHandler { - /// The Lambda handling method - /// Concrete Lambda handlers implement this method to provide the Lambda functionality. - /// - /// - parameters: - /// - context: Runtime `Context`. - /// - event: The event or request payload encoded as `ByteBuffer`. - /// - /// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine. - /// The `EventLoopFuture` should be completed with either a response encoded as `ByteBuffer` or an `Error` - func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture -} -``` - -### EventLoopLambdaHandler - -`EventLoopLambdaHandler` is a strongly typed, `EventLoopFuture` based asynchronous processing protocol for a Lambda that takes a user defined In and returns a user defined Out. +Swift AWS Lambda Events is a supporting library for the [Swift AWS Lambda Runtime](http://github.com/swift-server/swift-aws-lambda-runtime) library, providing abstractions for popular AWS events. -`EventLoopLambdaHandler` extends `ByteBufferLambdaHandler`, providing `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer?` encoding for `Codable` and String. +## Integration with AWS Platform Events -`EventLoopLambdaHandler` executes the user provided Lambda on the same `EventLoop` as the core runtime engine, making the processing fast but requires more care from the implementation to never block the `EventLoop`. It it designed for performance sensitive applications that use `Codable` or String based Lambda functions. - -```swift -public protocol EventLoopLambdaHandler: ByteBufferLambdaHandler { - associatedtype In - associatedtype Out - - /// The Lambda handling method - /// Concrete Lambda handlers implement this method to provide the Lambda functionality. - /// - /// - parameters: - /// - context: Runtime `Context`. - /// - event: Event of type `In` representing the event or request. - /// - /// - Returns: An `EventLoopFuture` to report the result of the Lambda back to the runtime engine. - /// The `EventLoopFuture` should be completed with either a response of type `Out` or an `Error` - func handle(context: Lambda.Context, event: In) -> EventLoopFuture - - /// Encode a response of type `Out` to `ByteBuffer` - /// Concrete Lambda handlers implement this method to provide coding functionality. - /// - parameters: - /// - allocator: A `ByteBufferAllocator` to help allocate the `ByteBuffer`. - /// - value: Response of type `Out`. - /// - /// - Returns: A `ByteBuffer` with the encoded version of the `value`. - func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? - - /// Decode a`ByteBuffer` to a request or event of type `In` - /// Concrete Lambda handlers implement this method to provide coding functionality. - /// - /// - parameters: - /// - buffer: The `ByteBuffer` to decode. - /// - /// - Returns: A request or event of type `In`. - func decode(buffer: ByteBuffer) throws -> In -} -``` - -### LambdaHandler - -`LambdaHandler` is a strongly typed, completion handler based asynchronous processing protocol for a Lambda that takes a user defined In and returns a user defined Out. - -`LambdaHandler` extends `ByteBufferLambdaHandler`, performing `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding for `Codable` and String. - -`LambdaHandler` offloads the user provided Lambda execution to a `DispatchQueue` making processing safer but slower. - -```swift -public protocol LambdaHandler: EventLoopLambdaHandler { - /// Defines to which `DispatchQueue` the Lambda execution is offloaded to. - var offloadQueue: DispatchQueue { get } - - /// The Lambda handling method - /// Concrete Lambda handlers implement this method to provide the Lambda functionality. - /// - /// - parameters: - /// - context: Runtime `Context`. - /// - event: Event of type `In` representing the event or request. - /// - callback: Completion handler to report the result of the Lambda back to the runtime engine. - /// The completion handler expects a `Result` with either a response of type `Out` or an `Error` - func handle(context: Lambda.Context, event: In, callback: @escaping (Result) -> Void) -} -``` - -### Closures - -In addition to protocol-based Lambda, the library provides support for Closure-based ones, as demonstrated in the overview section above. Closure-based Lambdas are based on the `LambdaHandler` protocol which mean they are safer. For most use cases, Closure-based Lambda is a great fit and users are encouraged to use them. - -The library includes implementations for `Codable` and String based Lambda. Since AWS Lambda is primarily JSON based, this covers the most common use cases. - -```swift -public typealias CodableClosure = (Lambda.Context, In, @escaping (Result) -> Void) -> Void -``` - -```swift -public typealias StringClosure = (Lambda.Context, String, @escaping (Result) -> Void) -> Void -``` - -This design allows for additional event types as well, and such Lambda implementation can extend one of the above protocols and provided their own `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding. - -### Context - -When calling the user provided Lambda function, the library provides a `Context` class that provides metadata about the execution context, as well as utilities for logging and allocating buffers. - -```swift -public final class Context { - /// The request ID, which identifies the request that triggered the function invocation. - public let requestID: String - - /// The AWS X-Ray tracing header. - public let traceID: String - - /// The ARN of the Lambda function, version, or alias that's specified in the invocation. - public let invokedFunctionARN: String - - /// The timestamp that the function times out - public let deadline: DispatchWallTime - - /// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider. - public let cognitoIdentity: String? - - /// For invocations from the AWS Mobile SDK, data about the client application and device. - public let clientContext: String? - - /// `Logger` to log with - /// - /// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable. - public let logger: Logger - - /// The `EventLoop` the Lambda is executed on. Use this to schedule work with. - /// This is useful when implementing the `EventLoopLambdaHandler` protocol. - /// - /// - note: The `EventLoop` is shared with the Lambda runtime engine and should be handled with extra care. - /// Most importantly the `EventLoop` must never be blocked. - public let eventLoop: EventLoop - - /// `ByteBufferAllocator` to allocate `ByteBuffer` - /// This is useful when implementing `EventLoopLambdaHandler` - public let allocator: ByteBufferAllocator -} -``` - -### Configuration - -The library’s behavior can be fine tuned using environment variables based configuration. The library supported the following environment variables: - -* `LOG_LEVEL`: Define the logging level as defined by [SwiftLog](https://github.com/apple/swift-log). Set to INFO by default. -* `MAX_REQUESTS`: Max cycles the library should handle before exiting. Set to none by default. -* `STOP_SIGNAL`: Signal to capture for termination. Set to TERM by default. -* `REQUEST_TIMEOUT`: Max time to wait for responses to come back from the AWS Runtime engine. Set to none by default. - - -### AWS Lambda Runtime Engine Integration - -The library is designed to integrate with AWS Lambda Runtime Engine via the [AWS Lambda Runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html) which was introduced as part of [AWS Lambda Custom Runtimes](https://aws.amazon.com/about-aws/whats-new/2018/11/aws-lambda-now-supports-custom-runtimes-and-layers/) in 2018. The latter is an HTTP server that exposes three main RESTful endpoint: - -* `/runtime/invocation/next` -* `/runtime/invocation/response` -* `/runtime/invocation/error` - -A single Lambda execution workflow is made of the following steps: - -1. The library calls AWS Lambda Runtime Engine `/next` endpoint to retrieve the next invocation request. -2. The library parses the response HTTP headers and populate the Context object. -3. The library reads the `/next` response body and attempt to decode it. Typically it decodes to user provided `In` type which extends `Decodable`, but users may choose to write Lambda functions that receive the input as String or `ByteBuffer` which require less, or no decoding. -4. The library hands off the `Context` and `In` event to the user provided handler. In the case of `LambdaHandler` based handler this is done on a dedicated `DispatchQueue`, providing isolation between user's and the library's code. -5. User provided handler processes the request asynchronously, invoking a callback or returning a future upon completion, which returns a Result type with the Out or Error populated. -6. In case of error, the library posts to AWS Lambda Runtime Engine `/error` endpoint to provide the error details, which will show up on AWS Lambda logs. -7. In case of success, the library will attempt to encode the response. Typically it encodes from user provided `Out` type which extends `Encodable`, but users may choose to write Lambda functions that return a String or `ByteBuffer`, which require less, or no encoding. The library then posts the response to AWS Lambda Runtime Engine `/response` endpoint to provide the response to the callee. - -The library encapsulates the workflow via the internal `LambdaRuntimeClient` and `LambdaRunner` structs respectively. - -### Lifecycle Management - -AWS Lambda Runtime Engine controls the Application lifecycle and in the happy case never terminates the application, only suspends it's execution when no work is available. - -As such, the library main entry point is designed to run forever in a blocking fashion, performing the workflow described above in an endless loop. - -That loop is broken if/when an internal error occurs, such as a failure to communicate with AWS Lambda Runtime Engine API, or under other unexpected conditions. - -By default, the library also registers a Signal handler that traps `INT` and `TERM` , which are typical Signals used in modern deployment platforms to communicate shutdown request. - -### Integration with AWS Platform Events - -AWS Lambda functions can be invoked directly from the AWS Lambda console UI, AWS Lambda API, AWS SDKs, AWS CLI, and AWS toolkits. More commonly, they are invoked as a reaction to an events coming from the AWS platform. To make it easier to integrate with AWS platform events, the library includes an `AWSLambdaEvents` target which provides abstractions for many commonly used events. Additional events can be easily modeled when needed following the same patterns set by `AWSLambdaEvents`. Integration points with the AWS Platform include: +AWS Lambda functions can be invoked directly from the AWS Lambda console UI, AWS Lambda API, AWS SDKs, AWS CLI, and AWS toolkits. More commonly, they are invoked as a reaction to an events coming from the AWS platform. To make it easier to integrate with AWS platform events, this library includes an `AWSLambdaEvents` target which provides abstractions for many commonly used events. Additional events can be easily modeled when needed following the same patterns set by `AWSLambdaEvents`. Integration points with the AWS Platform include: * [APIGateway Proxy](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html) * [S3 Events](https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html) @@ -344,17 +19,21 @@ AWS Lambda functions can be invoked directly from the AWS Lambda console UI, AWS **Note**: Each one of the integration points mentioned above includes a set of `Codable` structs that mirror AWS' data model for these APIs. -## Performance +## Getting started -Lambda functions performance is usually measured across two axes: +If you have never used AWS Lambda or Docker before, check out this [getting started guide](https://fabianfett.de/getting-started-with-swift-aws-lambda-runtime) which helps you with every step from zero to a running Lambda. -- **Cold start times**: The time it takes for a Lambda function to startup, ask for an invocation and process the first invocation. +Swift AWS Lambda Events is a supporting library for the [Swift AWS Lambda Runtime](http://github.com/swift-server/swift-aws-lambda-runtime) library, where you can find further documentation and examples. -- **Warm invocation times**: The time it takes for a Lambda function to process an invocation after the Lambda has been invoked at least once. +## Project status -Larger packages size (Zip file uploaded to AWS Lambda) negatively impact the cold start time, since AWS needs to download and unpack the package before starting the process. +This is the beginning of a community-driven open-source project actively seeking contributions. +While the core API is considered stable, the API may still evolve as we get closer to a `1.0` version. +There are several areas which need additional attention, including but not limited to: -Swift provides great Unicode support via [ICU](http://site.icu-project.org/home). Therefore, Swift-based Lambda functions include the ICU libraries which tend to be large. This impacts the download time mentioned above and an area for further optimization. Some of the alternatives worth exploring are using the system ICU that comes with Amazon Linux (albeit older than the one Swift ships with) or working to remove the ICU dependency altogether. We welcome ideas and contributions to this end. +* Additional events +* Additional documentation and best practices +* Additional examples ## Security diff --git a/scripts/linux_performance_setup.sh b/scripts/linux_performance_setup.sh deleted file mode 100755 index 7c11cbb..0000000 --- a/scripts/linux_performance_setup.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## This source file is part of the SwiftAWSLambdaRuntime open source project -## -## Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors -## Licensed under Apache License v2.0 -## -## See LICENSE.txt for license information -## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors -## -## SPDX-License-Identifier: Apache-2.0 -## -##===----------------------------------------------------------------------===## - -# docker run --privileged -it -v `pwd`:/code -w /code swiftlang/swift:nightly-5.3-bionic bash - -apt-get update -y -apt-get install -y vim htop strace linux-tools-common linux-tools-generic libc6-dbg - -echo 0 > /proc/sys/kernel/kptr_restrict - -cd /usr/bin -rm -rf perf -ln -s /usr/lib/linux-tools/*/perf perf -cd - - -cd /opt -git clone https://github.com/brendangregg/FlameGraph.git -cd - - -# build the code in relase mode with debug symbols -# swift build -c release -Xswiftc -g -# -# run the server -# (.build/release/MockServer) & -# -# strace -# export MAX_REQUESTS=10000 (or MAX_REQUESTS=1 for cold start analysis) -# strace -o .build/strace-c-string-$MAX_REQUESTS -c .build/release/StringSample -# strace -o .build/strace-ffftt-string-$MAX_REQUESTS -fftt .build/release/StringSample -# -# perf -# export MAX_REQUESTS=10000 (or MAX_REQUESTS=1 for cold start analysis) -# perf record -o .build/perf-$MAX_REQUESTS.data -g -F 100000 .build/release/StringSample dwarf -# perf script -i .build/perf-$MAX_REQUESTS.data | /opt/FlameGraph/stackcollapse-perf.pl | swift-demangle | /opt/FlameGraph/flamegraph.pl > .build/flamegraph-$MAX_REQUESTS.svg diff --git a/scripts/performance_test.sh b/scripts/performance_test.sh deleted file mode 100755 index 77904ec..0000000 --- a/scripts/performance_test.sh +++ /dev/null @@ -1,127 +0,0 @@ -#!/bin/bash -##===----------------------------------------------------------------------===## -## -## This source file is part of the SwiftAWSLambdaRuntime open source project -## -## Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors -## Licensed under Apache License v2.0 -## -## See LICENSE.txt for license information -## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors -## -## SPDX-License-Identifier: Apache-2.0 -## -##===----------------------------------------------------------------------===## - -set -eu - -export HOST=127.0.0.1 -export PORT=3000 -export AWS_LAMBDA_RUNTIME_API="$HOST:$PORT" -export LOG_LEVEL=warning # important, otherwise log becomes a bottleneck - -# using gdate on mdarwin for nanoseconds -if [[ $(uname -s) == "Linux" ]]; then - shopt -s expand_aliases - alias gdate="date" -fi - -swift build -c release -Xswiftc -g - -cleanup() { - kill -9 $server_pid -} - -trap "cleanup" ERR - -cold_iterations=1000 -warm_iterations=10000 -results=() - -#------------------ -# string -#------------------ - -export MODE=string - -# start (fork) mock server -pkill -9 MockServer && echo "killed previous servers" && sleep 1 -echo "starting server in $MODE mode" -(./.build/release/MockServer) & -server_pid=$! -sleep 1 -kill -0 $server_pid # check server is alive - -# cold start -echo "running $MODE mode cold test" -cold=() -export MAX_REQUESTS=1 -for (( i=0; i<$cold_iterations; i++ )); do - start=$(gdate +%s%N) - ./.build/release/StringSample - end=$(gdate +%s%N) - cold+=( $(($end-$start)) ) -done -sum_cold=$(IFS=+; echo "$((${cold[*]}))") -avg_cold=$(($sum_cold/$cold_iterations)) -results+=( "$MODE, cold: $avg_cold (ns)" ) - -# normal calls -echo "running $MODE mode warm test" -export MAX_REQUESTS=$warm_iterations -start=$(gdate +%s%N) -./.build/release/StringSample -end=$(gdate +%s%N) -sum_warm=$(($end-$start-$avg_cold)) # substract by avg cold since the first call is cold -avg_warm=$(($sum_warm/($warm_iterations-1))) # substract since the first call is cold -results+=( "$MODE, warm: $avg_warm (ns)" ) - -#------------------ -# JSON -#------------------ - -export MODE=json - -# start (fork) mock server -pkill -9 MockServer && echo "killed previous servers" && sleep 1 -echo "starting server in $MODE mode" -(./.build/release/MockServer) & -server_pid=$! -sleep 1 -kill -0 $server_pid # check server is alive - -# cold start -echo "running $MODE mode cold test" -cold=() -export MAX_REQUESTS=1 -for (( i=0; i<$cold_iterations; i++ )); do - start=$(gdate +%s%N) - ./.build/release/CodableSample - end=$(gdate +%s%N) - cold+=( $(($end-$start)) ) -done -sum_cold=$(IFS=+; echo "$((${cold[*]}))") -avg_cold=$(($sum_cold/$cold_iterations)) -results+=( "$MODE, cold: $avg_cold (ns)" ) - -# normal calls -echo "running $MODE mode warm test" -export MAX_REQUESTS=$warm_iterations -start=$(gdate +%s%N) -./.build/release/CodableSample -end=$(gdate +%s%N) -sum_warm=$(($end-$start-$avg_cold)) # substract by avg cold since the first call is cold -avg_warm=$(($sum_warm/($warm_iterations-1))) # substract since the first call is cold -results+=( "$MODE, warm: $avg_warm (ns)" ) - -# print results -echo "-----------------------------" -echo "results" -echo "-----------------------------" -for i in "${results[@]}"; do - echo $i -done -echo "-----------------------------" - -# cleanup -cleanup