-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example and README for background tasks (#418)
* add an example for background tasks * swift-format * add CI * add background task section in the main readme * minor formatting changes
- Loading branch information
Showing
7 changed files
with
291 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// swift-tools-version:6.0 | ||
|
||
import PackageDescription | ||
|
||
// needed for CI to test the local version of the library | ||
import struct Foundation.URL | ||
|
||
#if os(macOS) | ||
let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)] | ||
#else | ||
let platforms: [PackageDescription.SupportedPlatform]? = nil | ||
#endif | ||
|
||
let package = Package( | ||
name: "swift-aws-lambda-runtime-example", | ||
platforms: platforms, | ||
products: [ | ||
.executable(name: "BackgroundTasks", targets: ["BackgroundTasks"]) | ||
], | ||
dependencies: [ | ||
// during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below | ||
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "BackgroundTasks", | ||
dependencies: [ | ||
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") | ||
], | ||
path: "." | ||
) | ||
] | ||
) | ||
|
||
if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"], | ||
localDepsPath != "", | ||
let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]), | ||
v.isDirectory == true | ||
{ | ||
// when we use the local runtime as deps, let's remove the dependency added above | ||
let indexToRemove = package.dependencies.firstIndex { dependency in | ||
if case .sourceControl( | ||
name: _, | ||
location: "https://github.com/swift-server/swift-aws-lambda-runtime.git", | ||
requirement: _ | ||
) = dependency.kind { | ||
return true | ||
} | ||
return false | ||
} | ||
if let indexToRemove { | ||
package.dependencies.remove(at: indexToRemove) | ||
} | ||
|
||
// then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..) | ||
print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") | ||
package.dependencies += [ | ||
.package(name: "swift-aws-lambda-runtime", path: localDepsPath) | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Background Tasks | ||
|
||
This is an example for running background tasks in an AWS Lambda function. | ||
|
||
Background tasks allow code to execute asynchronously after the main response has been returned, enabling additional processing without affecting response latency. This approach is ideal for scenarios like logging, data updates, or notifications that can be deferred. The code leverages Lambda's "Response Streaming" feature, which is effective for balancing real-time user responsiveness with the ability to perform extended tasks post-response. | ||
|
||
For more information about Lambda background tasks, see [this AWS blog post](https://aws.amazon.com/blogs/compute/running-code-after-returning-a-response-from-an-aws-lambda-function/). | ||
|
||
## Code | ||
|
||
The sample code creates a `BackgroundProcessingHandler` struct that conforms to the `LambdaWithBackgroundProcessingHandler` protocol provided by the Swift AWS Lambda Runtime. | ||
|
||
The `BackgroundProcessingHandler` struct defines the input and output JSON received and returned by the Handler. | ||
|
||
The `handle(...)` method of this protocol receives incoming events as `Input` and returns the output as a `Greeting`. The `handle(...)` methods receives an `outputWriter` parameter to write the output before the function returns, giving some opportunities to run long-lasting tasks after the response has been returned to the client but before the function returns. | ||
|
||
The `handle(...)` method uses the `outputWriter` to return the response as soon as possible. It then waits for 10 seconds to simulate a long background work. When the 10 seconds elapsed, the function returns. The billing cycle ends when the function returns. | ||
|
||
The `handle(...)` method is marked as `mutating` to allow handlers to be implemented with a `struct`. | ||
|
||
Once the struct is created and the `handle(...)` method is defined, the sample code creates a `LambdaCodableAdapter` adapter to adapt the `LambdaWithBackgroundProcessingHandler` to a type accepted by the `LambdaRuntime` struct. Then, the sample code initializes the `LambdaRuntime` with the adapter just created. Finally, the code calls `run()` to start the interaction with the AWS Lambda control plane. | ||
|
||
## Build & Package | ||
|
||
To build & archive the package, type the following commands. | ||
|
||
```bash | ||
swift package archive --allow-network-connections docker | ||
``` | ||
|
||
If there is no error, there is a ZIP file ready to deploy. | ||
The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/BackgroundTasks/BackgroundTasks.zip` | ||
|
||
## Deploy with the AWS CLI | ||
|
||
Here is how to deploy using the `aws` command line. | ||
|
||
### Create the function | ||
|
||
```bash | ||
AWS_ACCOUNT_ID=012345678901 | ||
aws lambda create-function \ | ||
--function-name BackgroundTasks \ | ||
--zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/BackgroundTasks/BackgroundTasks.zip \ | ||
--runtime provided.al2 \ | ||
--handler provided \ | ||
--architectures arm64 \ | ||
--role arn:aws:iam::${AWS_ACCOUNT_ID}:role/lambda_basic_execution \ | ||
--environment "Variables={LOG_LEVEL=debug}" \ | ||
--timeout 15 | ||
``` | ||
|
||
> [!IMPORTANT] | ||
> The timeout value must be bigger than the time it takes for your function to complete its background tasks. Otherwise, the Lambda control plane will terminate the execution environment before your code has a chance to finish the tasks. Here, the sample function waits for 10 seconds and we set the timeout for 15 seconds. | ||
The `--environment` arguments sets the `LOG_LEVEL` environment variable to `debug`. This will ensure the debugging statements in the handler `context.logger.debug("...")` are printed in the Lambda function logs. | ||
|
||
The `--architectures` flag is only required when you build the binary on an Apple Silicon machine (Apple M1 or more recent). It defaults to `x64`. | ||
|
||
Be sure to set `AWS_ACCOUNT_ID` with your actual AWS account ID (for example: 012345678901). | ||
|
||
### Invoke your Lambda function | ||
|
||
To invoke the Lambda function, use `aws` command line. | ||
```bash | ||
aws lambda invoke \ | ||
--function-name BackgroundTasks \ | ||
--cli-binary-format raw-in-base64-out \ | ||
--payload '{ "message" : "Hello Background Tasks" }' \ | ||
response.json | ||
``` | ||
|
||
This should immediately output the following result. | ||
|
||
``` | ||
{ | ||
"StatusCode": 200, | ||
"ExecutedVersion": "$LATEST" | ||
} | ||
``` | ||
|
||
The response is visible in the `response.json` file. | ||
|
||
```bash | ||
cat response.json | ||
{"echoedMessage":"Hello Background Tasks"} | ||
``` | ||
|
||
### View the function's logs | ||
|
||
You can observe additional messages being logged after the response is received. | ||
|
||
To tail the log, use the AWS CLI: | ||
```bash | ||
aws logs tail /aws/lambda/BackgroundTasks --follow | ||
``` | ||
|
||
This produces an output like: | ||
```text | ||
INIT_START Runtime Version: provided:al2.v59 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:974c4a90f22278a2ef1c3f53c5c152167318aaf123fbb07c055a4885a4e97e52 | ||
START RequestId: 4c8edd74-d776-4df9-9714-19086ab59bfd Version: $LATEST | ||
debug LambdaRuntime : [BackgroundTasks] BackgroundProcessingHandler - message received | ||
debug LambdaRuntime : [BackgroundTasks] BackgroundProcessingHandler - response sent. Performing background tasks. | ||
debug LambdaRuntime : [BackgroundTasks] BackgroundProcessingHandler - Background tasks completed. Returning | ||
END RequestId: 4c8edd74-d776-4df9-9714-19086ab59bfd | ||
REPORT RequestId: 4c8edd74-d776-4df9-9714-19086ab59bfd Duration: 10160.89 ms Billed Duration: 10250 ms Memory Size: 128 MB Max Memory Used: 27 MB Init Duration: 88.20 ms | ||
``` | ||
> [!NOTE] | ||
> The `debug` message are sent by the code inside the `handler()` function. Note that the `Duration` and `Billed Duration` on the last line are for 10.1 and 10.2 seconds respectively. | ||
Type CTRL-C to stop tailing the logs. | ||
|
||
## Cleanup | ||
|
||
When done testing, you can delete the Lambda function with this command. | ||
|
||
```bash | ||
aws lambda delete-function --function-name BackgroundTasks | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftAWSLambdaRuntime open source project | ||
// | ||
// Copyright (c) 2024 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import AWSLambdaRuntime | ||
import Foundation | ||
|
||
struct BackgroundProcessingHandler: LambdaWithBackgroundProcessingHandler { | ||
struct Input: Decodable { | ||
let message: String | ||
} | ||
|
||
struct Greeting: Encodable { | ||
let echoedMessage: String | ||
} | ||
|
||
typealias Event = Input | ||
typealias Output = Greeting | ||
|
||
func handle( | ||
_ event: Event, | ||
outputWriter: some LambdaResponseWriter<Output>, | ||
context: LambdaContext | ||
) async throws { | ||
// Return result to the Lambda control plane | ||
context.logger.debug("BackgroundProcessingHandler - message received") | ||
try await outputWriter.write(Greeting(echoedMessage: event.message)) | ||
|
||
// Perform some background work, e.g: | ||
context.logger.debug("BackgroundProcessingHandler - response sent. Performing background tasks.") | ||
try await Task.sleep(for: .seconds(10)) | ||
|
||
// Exit the function. All asynchronous work has been executed before exiting the scope of this function. | ||
// Follows structured concurrency principles. | ||
context.logger.debug("BackgroundProcessingHandler - Background tasks completed. Returning") | ||
return | ||
} | ||
} | ||
|
||
let adapter = LambdaCodableAdapter(handler: BackgroundProcessingHandler()) | ||
let runtime = LambdaRuntime.init(handler: adapter) | ||
try await runtime.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters