Skip to content

[core] Swift Service Lifecycle (WIP - do not merge) #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Examples/ServiceLifeCycle/.gitignore
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
56 changes: 56 additions & 0 deletions Examples/ServiceLifeCycle/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

// needed for CI to test the local version of the library
import struct Foundation.URL

let package = Package(
name: "LambdaWithServiceLifecycle",
platforms: [
.macOS(.v15)
],
dependencies: [
.package(url: "https://github.com/vapor/postgres-nio.git", from: "1.23.0"),
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.6.3"),
],
targets: [
.executableTarget(
name: "LambdaWithServiceLifecycle",
dependencies: [
.product(name: "PostgresNIO", package: "postgres-nio"),
.product(name: "AWSLambdaRuntimeService", package: "swift-aws-lambda-runtime"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
]
)
]
)

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)
]
}
92 changes: 92 additions & 0 deletions Examples/ServiceLifeCycle/Sources/Lambda.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2025 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 AWSLambdaRuntimeService
import Logging
import PostgresNIO
import ServiceLifecycle

@main
struct LambdaFunction {

static func main() async throws {

var logger = Logger(label: "ServiceLifecycleExample")
logger.logLevel = .trace

let pgClient = try preparePostgresClient(
host: Lambda.env("DB_HOST") ?? "localhost",
user: Lambda.env("DB_USER") ?? "postgres",
password: Lambda.env("DB_PASSWORD") ?? "secret",
dbName: Lambda.env("DB_NAME") ?? "test"
)

/// Instantiate LambdaRuntime with a closure handler implementing the business logic of the Lambda function
let runtime = LambdaRuntimeService(logger: logger) { (event: String, context: LambdaContext) in

do {
// Use initialized service within the handler
// IMPORTANT - CURRENTLY WHEN THERE IS AN ERROR, THIS CALL HANGS WHEN DB IS NOT REACHABLE
// https://github.com/vapor/postgres-nio/issues/489
let rows = try await pgClient.query("SELECT id, username FROM users")
for try await (id, username) in rows.decode((Int, String).self) {
logger.debug("\(id) : \(username)")
}
} catch {
logger.error("PG Error: \(error)")
}
}

/// Use ServiceLifecycle to manage the initialization and termination
/// of the PGClient together with the LambdaRuntime
let serviceGroup = ServiceGroup(
services: [pgClient, runtime],
gracefulShutdownSignals: [.sigterm, .sigint], // add SIGINT for CTRL+C in local testing
// cancellationSignals: [.sigint],
logger: logger
)
try await serviceGroup.run()

// perform any cleanup here
}

private static func preparePostgresClient(
host: String,
user: String,
password: String,
dbName: String
) throws -> PostgresClient {

var tlsConfig = TLSConfiguration.makeClientConfiguration()
// Load the root certificate
let rootCert = try NIOSSLCertificate.fromPEMBytes(Array(eu_central_1_bundle_pem.utf8))

// Add the root certificate to the TLS configuration
tlsConfig.trustRoots = .certificates(rootCert)

// Enable full verification
tlsConfig.certificateVerification = .fullVerification

let config = PostgresClient.Configuration(
host: host,
port: 5432,
username: user,
password: password,
database: dbName,
tls: .prefer(tlsConfig)
)

return PostgresClient(configuration: config)
}
}
95 changes: 95 additions & 0 deletions Examples/ServiceLifeCycle/Sources/RootRDSCert.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2025 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
//
//===----------------------------------------------------------------------===//

// you can download the root certificate for your RDS instance region from the following link:
// https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html

let eu_central_1_bundle_pem = """
-----BEGIN CERTIFICATE-----
MIICtDCCAjmgAwIBAgIQenQbcP/Zbj9JxvZ+jXbRnTAKBggqhkjOPQQDAzCBmTEL
MAkGA1UEBhMCVVMxIjAgBgNVBAoMGUFtYXpvbiBXZWIgU2VydmljZXMsIEluYy4x
EzARBgNVBAsMCkFtYXpvbiBSRFMxCzAJBgNVBAgMAldBMTIwMAYDVQQDDClBbWF6
b24gUkRTIGV1LWNlbnRyYWwtMSBSb290IENBIEVDQzM4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-----
"""
14 changes: 14 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,33 @@ let package = Package(
products: [
// this library exports `AWSLambdaRuntimeCore` and adds Foundation convenience methods
.library(name: "AWSLambdaRuntime", targets: ["AWSLambdaRuntime"]),

// this library exports `AWSLambdaRuntime` and adds conformances to `Service` from Swift Service Lifecycle
.library(name: "AWSLambdaRuntimeService", targets: ["AWSLambdaRuntimeService"]),

// this has all the main functionality for lambda and it does not link Foundation
.library(name: "AWSLambdaRuntimeCore", targets: ["AWSLambdaRuntimeCore"]),

// plugin to package the lambda, creating an archive that can be uploaded to AWS
// requires Linux or at least macOS v15
.plugin(name: "AWSLambdaPackager", targets: ["AWSLambdaPackager"]),

// for testing only
.library(name: "AWSLambdaTesting", targets: ["AWSLambdaTesting"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.77.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.5.4"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.6.3"),
],
targets: [
.target(
name: "AWSLambdaRuntimeService",
dependencies: [
.byName(name: "AWSLambdaRuntime"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
]
),
.target(
name: "AWSLambdaRuntime",
dependencies: [
Expand Down
57 changes: 40 additions & 17 deletions Sources/AWSLambdaRuntimeCore/Lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Dispatch
import Logging
import NIOCore
import NIOPosix
import Synchronization

#if os(macOS)
import Darwin.C
Expand All @@ -30,33 +31,55 @@ import ucrt
#endif

public enum Lambda {

// allow to gracefully shitdown the runtime client loop
// this supports gracefull shutdown of the Lambda runtime when integarted with Swift ServiceLifeCycle
private static let gracefulShutdown: Mutex<Bool> = Mutex(false)
public static func shutdown() {
Lambda.gracefulShutdown.withLock {
$0 = true
}
}
package static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>(
runtimeClient: RuntimeClient,
handler: Handler,
logger: Logger
) async throws where Handler: StreamingLambdaHandler {
var handler = handler
var gracefulShutdown: Bool = Lambda.gracefulShutdown.withLock { $0 }
do {
while !Task.isCancelled && !gracefulShutdown {
logger.trace("Waiting for next invocation")
let (invocation, writer) = try await runtimeClient.nextInvocation()

while !Task.isCancelled {
let (invocation, writer) = try await runtimeClient.nextInvocation()

do {
try await handler.handle(
invocation.event,
responseWriter: writer,
context: LambdaContext(
requestID: invocation.metadata.requestID,
traceID: invocation.metadata.traceID,
invokedFunctionARN: invocation.metadata.invokedFunctionARN,
deadline: DispatchWallTime(millisSinceEpoch: invocation.metadata.deadlineInMillisSinceEpoch),
logger: logger
logger.trace("Received invocation : \(invocation.metadata.requestID)")
do {
try await handler.handle(
invocation.event,
responseWriter: writer,
context: LambdaContext(
requestID: invocation.metadata.requestID,
traceID: invocation.metadata.traceID,
invokedFunctionARN: invocation.metadata.invokedFunctionARN,
deadline: DispatchWallTime(
millisSinceEpoch: invocation.metadata.deadlineInMillisSinceEpoch
),
logger: logger
)
)
)
} catch {
try await writer.reportError(error)
continue
} catch {
try await writer.reportError(error)
continue
}
logger.trace("Completed invocation : \(invocation.metadata.requestID)")
gracefulShutdown = Lambda.gracefulShutdown.withLock { $0 }
}

} catch is CancellationError {
// don't allow cancellation error to propagate further
logger.trace("Lambda runLoop() task has been cancelled")
}
logger.trace("Lambda runLoop() terminated \(gracefulShutdown ? "with gracefull shutdown" : "")")
}

/// The default EventLoop the Lambda is scheduled on.
Expand Down
Loading
Loading