Skip to content

Commit 1b3c81c

Browse files
sebstoSebastien Stormacq
andauthored
Add Swift ServiceLifecycle section to the readme (#601)
Add Swift ServiceLifecycle section to the readme --------- Co-authored-by: Sebastien Stormacq <stormacq@amazon.lu>
1 parent 76c272e commit 1b3c81c

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

readme.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,48 @@ try await runtime.run()
384384

385385
### Integration with Swift Service LifeCycle
386386

387-
Support for [Swift Service Lifecycle](https://github.com/swift-server/swift-service-lifecycle) is currently being implemented. You can follow https://github.com/awslabs/swift-aws-lambda-runtime/issues/374 for more details and the current status. Your contributions are welcome.
387+
The Swift AWS Lambda Runtime provides built-in support for [Swift Service Lifecycle](https://github.com/swift-server/swift-service-lifecycle), allowing you to manage the lifecycle of your Lambda runtime alongside other services like database clients, HTTP clients, or any other resources that need proper initialization and cleanup.
388+
389+
Here's how to integrate your Lambda function with ServiceLifecycle to manage multiple services:
390+
391+
```swift
392+
import AWSLambdaRuntime
393+
import ServiceLifecycle
394+
import PostgresNIO
395+
396+
@main
397+
struct LambdaFunction {
398+
private func start() async throws {
399+
// Create a database client
400+
let pgClient = PostgresClient(configuration: /* your config */)
401+
402+
// Create the Lambda runtime
403+
let lambdaRuntime = LambdaRuntime(body: self.handler)
404+
405+
// Use ServiceLifecycle to manage both the database client and Lambda runtime
406+
let serviceGroup = ServiceGroup(
407+
services: [pgClient, lambdaRuntime],
408+
gracefulShutdownSignals: [.sigterm],
409+
cancellationSignals: [.sigint],
410+
logger: self.logger
411+
)
412+
413+
// Start all services - this will handle initialization and cleanup
414+
try await serviceGroup.run()
415+
}
416+
417+
private func handler(event: String, context: LambdaContext) async throws -> String {
418+
// Your Lambda function logic here
419+
return "Hello, World!"
420+
}
421+
422+
static func main() async throws {
423+
try await LambdaFunction().start()
424+
}
425+
}
426+
```
427+
428+
You can see a complete working example in the [ServiceLifecycle+Postgres example](Examples/ServiceLifecycle+Postgres/README.md), which demonstrates how to manage a PostgreSQL client alongside the Lambda runtime using ServiceLifecycle.
388429

389430
### Use Lambda Background Tasks
390431

0 commit comments

Comments
 (0)