Commit Graph
35 Commits
Author SHA1 Message Date
Sébastien Stormacq e6ba07fd06 [example] Add example for Swift Service Lifecycle (#522)
Now that task cancellation works, re publishing this PR with a new
example for Swift Service Lifecycle
2025-07-30 06:40:55 +04:00
Sébastien StormacqandTim Condon 412a345bdd [core] Add user-facing API for Streaming Lambda functions that receive JSON events (#532)
Add user-facing API for Streaming Lambda functions that receives JSON
events

### Motivation:

Streaming Lambda functions developed by developers had no choice but to
implement a handler that receives incoming data as a `ByteBuffer`. While
this is useful for low-level development, I assume most developers will
want to receive a JSON event to trigger their streaming Lambda function.

Going efficiently from a `ByteBuffer` to a Swift struct requires some
code implemented in the `JSON+ByteBuffer.swift` file of the librray. We
propose to further help developers by providing them with a new
`handler()` function that directly receives their `Decodable` type.

### Modifications:

This PR adds a public facing API (+ unit test + updated README) allowing
developers to write a handler method accepting any `Decodable` struct as
input.

```swift
import AWSLambdaRuntime
import NIOCore

// Define your input event structure
struct StreamingRequest: Decodable {
    let count: Int
    let message: String
    let delayMs: Int?
}

// Use the new streaming handler with JSON decoding
let runtime = LambdaRuntime { (event: StreamingRequest, responseWriter, context: LambdaContext) in
    context.logger.info("Received request to send \(event.count) messages")
    
    // Stream the messages
    for i in 1...event.count {
        let response = "Message \(i)/\(event.count): \(event.message)\n"
        try await responseWriter.write(ByteBuffer(string: response))
        
        // Optional delay between messages
        if let delay = event.delayMs, delay > 0 {
            try await Task.sleep(for: .milliseconds(delay))
        }
    }
    
    // Finish the stream
    try await responseWriter.finish()
    
    // Optional: Execute background work after response is sent
    context.logger.info("Background work: processing completed")
}

try await runtime.run()
```

This interface provides:
- **Type-safe JSON input**: Automatic decoding of JSON events into Swift
structs
- **Streaming responses**: Full control over when and how to stream data
back to clients
- **Background work support**: Ability to execute code after the
response stream is finished
- **Familiar API**: Uses the same closure-based pattern as regular
Lambda handlers

Because streaming Lambda functions can be invoked either directly
through the API or through Lambda Function URL, this PR adds the
decoding logic to support both types, shielding developers from working
with Function URL requests and base64 encoding.

We understand these choice will have an impact on the raw performance
for event handling. Those advanced users that want to get the maximum
might use the existing `handler(_ event: ByteBuffer, writer:
LambaStreamingWriter)` function to implement their own custom decoding
logic.

This PR provides a balance between ease of use for 80% of the users vs
ultimate performance, without closing the door for the 20% who need it.

### Result:

Lambda function developers can now use arbitrary `Decodable` Swift
struct or Lambda events to trigger their streaming functions. 🎉

---------

Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
2025-07-23 19:03:25 +04:00
Sébastien Stormacq a616996722 [test] add a unit test for the LambdaHTTPServer Pool (#500) 2025-06-28 14:11:28 +02:00
Konrad `ktoso` Malawski c7df9bcd8a Revert "[ci] : Remove deprecated options on unit-test action" (#513)
Reverts swift-server/swift-aws-lambda-runtime#510

I think the issue
https://github.com/swift-server/swift-aws-lambda-runtime/issues/509 that
caused this tests disabling has been resolved in Swift and would like to
confirm here as well.
2025-05-16 08:08:27 +02:00
Sébastien StormacqandFabian Fett 893909543b [ci] : Remove deprecated options on unit-test action (#510)
~~Attempt to fix CI~~

Remove usage of deprecated parameters on unit-test. 

- explicitly enable tests on nightly 6.0
- explicitly enable tests on nightly next
- explicitly enable tests on nightly main

At the time of this writing, this resolves to 

```
linux_6_0_container_image="swift:6.0-jammy"
linux_nightly_next_container_image="swiftlang/swift:nightly-6.1-jammy"
linux_nightly_main_container_image="swiftlang/swift:nightly-main-jammy"
```

---------

Co-authored-by: Fabian Fett <fabianfett@apple.com>
2025-03-20 19:57:21 +01:00
Sébastien Stormacq 7322a3694a [plugin] Fix #505 : resources are not included in the archive on Linux (#506)
Resources are not included in the archive on Linux
 https://github.com/swift-server/swift-aws-lambda-runtime/issues/505

In addition, the script that checks if the resources are correctly
included in an archive when building on Linux is not working.
It failed to detected missing resources.

This patch fix both the plugin and the  CI script
2025-03-17 11:24:07 +01:00
Sébastien Stormacq 8f10a6bbdd Fix CI error on Amazon S3 examples (#496)
When we include Traits, we changed the CI container from Swift 6.0
Amazon Linux to Swift 6.1 Nightly on Ubuntu.
This broke the CI because AWS SDK used in the `Examples/S3_AWSSDK`
example depends on `libssl-devel` which is not installed by default on
Jammy images.

This PR reverts back the container used to test all examples to an
Amazon Linux 2, but this time the 6.1-nightly version to allow to
compile the package traits.

This should address
https://github.com/swift-server/swift-aws-lambda-runtime/issues/494
2025-03-06 18:58:50 +01:00
Fabian Fett 0a75e0f9cc Use package traits when using Swift 6.1 (#490) 2025-03-06 09:52:23 +01:00
Paul ToffoloniandSébastien Stormacq 0777c80172 Add S3EventNotifier example (#477)
Add `S3EventNotifier` example

### Motivation:

There is currently no example regarding an AWS event triggered lambda,
such as a lambda that gets invoked on an S3 object upload. I've had to
look for a while to figure out that the `AWSLambdaEvents` exists and
that it can be used for that (had to find out via a Java example! 😜) so
I think this could be useful and a somewhat common use case

### Modifications:

Added an example of a lambda that gets triggered when an object gets
uploaded to an S3 bucket.

I have not described how to set up the actual S3 event in AWS because I
figured if you needed such a lambda you'd know, but I can describe that
too in the README if needed

---------

Co-authored-by: Sébastien Stormacq <sebastien.stormacq@gmail.com>
2025-02-11 17:22:12 +01:00
Sébastien Stormacq 71d49b049e [plugin] support for resources packaging on ubuntu (#467)
When including resources in the package and packaging on Ubuntu,
`FileManager` throws a FilePermission error. The docker daemon runs as
root and files to be copied are owned by `root` while the archiver runs
as the current user (`ubuntu` on EC2 Ubuntu). The `FileManager` manages
to copy the files but throws an error after the copy. We suspect the
`FileManager` to perform some kind of operation after the copy and it
fails because of the `root` permission of the files.

See
https://github.com/swift-server/swift-aws-lambda-runtime/issues/449#issuecomment-2595978246
for a description of the problem.

This PR contains code to reproduce the problem, a very simple
workaround, and an integration test.
The workaround consists of 
- trapping all errors
- verify if the error is the permission error (Code = 513)
- verify if the files have been copied or not 
- if the two above conditions are met, ignore the error, otherwise
re-throw it

I would rather prefer a solution that solves the root cause rather than
just ignoring the error.
We're still investigating the root cause (see [this
thread](https://forums.swift.org/t/filemanager-copyitem-on-linux-fails-after-copying-the-files/77282)
on the Swift Forum and this issue on Swift Foundation
https://github.com/swiftlang/swift-foundation/issues/1125
2025-01-21 08:06:51 +01:00
Sébastien Stormacq 051029fc0f [examples] Add an example of an APIGateway Lambda Authorizer (#453)
Add an example of an APIGateway Lambda Authorizer

### Motivation:

Lambda Authorizers allow developers to write custom logic to validate
HTTP REST requests.
This example shows how to write a Lambda Authorizer in Swift and how to
configure an API Gateway to use a Lambda Authorizer.

This example use a SAM template to deploy the resources.

### Modifications:

- Add `Examples/APIGateway+LambdaAuthorizer` directory with the source
code, a `template.yaml`, and a `README` file.
- Add a reference to the new example in `Examples/README`
- Modify CI to build the new example 

### Result:

There is a new example showing how to develop a Lambda authorizer in
Swift and how to attach it to an APIGateway with SAM
2025-01-06 16:44:13 +01:00
Sébastien Stormacq 2669009d3b Update the tutorial for v2 (#450)
Update the text, code sample, and screenshots for runtime v2

Address
https://github.com/swift-server/swift-aws-lambda-runtime/issues/371
2025-01-04 10:54:37 +01:00
Sébastien Stormacq 38d9e43edb [CI] Add testing example in the list of integration tests (#444) 2024-12-25 08:12:42 +01:00
Sébastien Stormacq 31a7edaf87 [CI] add test to check libFoundation.so & co are not linked to the bi… (#441)
This adds a test in the `integration_test.yml` workflow to verify that
`libFoundation.so`, `libFoundationInternationalization.so`, and
`lib_FoundationICU.so` and not linked the the function binary.

This is a follow up from the work make by @t089 to ensure that we don't
accidentally link Foundation again in the future.

Remove Foundation :
https://github.com/swift-server/swift-aws-lambda-runtime/pull/436
Add test in CI to ensure we don't link to foundation :
https://github.com/swift-server/swift-aws-lambda-runtime/issues/402
2024-12-25 07:42:54 +01:00
Sébastien Stormacq 8c1c1b6fc3 revert commit 04b01da9fe 2024-12-24 08:43:45 +01:00
Sébastien Stormacq 04b01da9fe move integration test to .github (#443)
_[One line description of your change]_

### Motivation:

_[Explain here the context, and why you're making that change. What is
the problem you're trying to solve.]_

### Modifications:

_[Describe the modifications you've done.]_

### Result:

_[After your change, what will change.]_
2024-12-22 15:17:44 +01:00
Sébastien Stormacq b4d17fdf8d [CI] add CI scripts separate from the workflows (#442)
_[One line description of your change]_

### Motivation:

_[Explain here the context, and why you're making that change. What is
the problem you're trying to solve.]_

### Modifications:

_[Describe the modifications you've done.]_

### Result:

_[After your change, what will change.]_
2024-12-22 14:39:52 +01:00
Sébastien Stormacq 64a4d829f0 Fix CI (#429)
* disable yaml linter

* add semver label check

* re-enable soundness checks (shell, python, and yaml)

* disable checks on docc shell command

* fix errors
2024-11-26 18:32:24 +01:00
Sébastien Stormacq 21e224ea8d Update local server for Swift 6 compliance (#428)
* Update local server to Swift 6

* fix compilation error for local server in release mode

* [CI] fix error for integration plugin test
2024-11-16 10:22:01 +01:00
Sébastien Stormacq c0df329d1f [CI] fix name for integration test 2024-11-14 08:05:57 +01:00
Sébastien StormacqandTim Condon c22f5271a0 add HelloJSON example + README (#424)
* add HelloJSON example + README

* multiple corrections of typos and grammatical errors

Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
2024-11-13 13:53:19 +01:00
Sébastien StormacqandTim Condon 54fd25bdad Add example and README for a streaming Lambda function (#415)
* add streaming example and doc

* add streaming example to CI

* fix soundness

* remove unused files

* correct usage of zsh env var

* typo

Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>

* typo

Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>

* add comments

Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>

* typo

Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>

* add an example README with common sections + link to each example

---------

Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
2024-11-07 14:55:37 +01:00
Sébastien Stormacq ca82709a9b 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
2024-11-07 11:33:34 +01:00
Sébastien Stormacq 7016dfc0fd Set the log level to LOG_LEVEL env variable (#417)
* set the log level to LOG_LEVEL env variable

* remove need for `var logger`

* swift-format

* [ci] update static sdk to 6.0.2
2024-11-07 09:49:11 +01:00
Sébastien Stormacq 1edaedfb19 [CI] add testing on musl (#412)
* [CI] add testing on musl
2024-10-17 17:57:23 +02:00
Sébastien Stormacq 75d0404a05 [CI] drop Swift 5.8 (#411)
Align to the new version of the CI script is Swift NIO project
2024-10-17 11:09:21 +02:00
Sébastien Stormacq 2fad042186 [CI] add integration test for the archive plugin (#406)
* add integration test for the archive plugin
2024-10-14 21:37:52 +02:00
Sébastien Stormacq 8eddc2872c Do not run python linter during CI (#410) 2024-10-14 11:13:12 +02:00
Franz Busch 59dd68adab [CI] Adopt soundess workflow from swiftlang (#405) 2024-10-11 15:49:34 +02:00
Sébastien Stormacq b4673d264b Add AWS SDK and Soto examples (#396)
* add aws sdk example

* add soto example

* use amazonlinux docker image instead of ubuntu

* dynamically add runtime dependency based on env var

* remove import of Foundation.ProcessInfo

* workaround https://github.com/actions/checkout/issues/1487
2024-10-10 13:08:31 +02:00
Sébastien StormacqandFabian Fett 65061a638c Remove docker compose files + add GH action to compile examples during CI (#397)
* add GH action to compile examples on PR

* remove old docker infrastructure


---------


Co-authored-by: Fabian Fett <fabianfett@apple.com>
2024-10-10 11:16:12 +02:00
Fabian Fett d2bd7f1aae Fix docs CI (#392) 2024-10-08 17:46:53 +02:00
aryan-25andFabian Fett d1a6fbc132 Set container image argument to "swiftlang/swift:nightly-6.0-jammy" for API breakage and documentation checks. (#346)
Co-authored-by: Fabian Fett <fabianfett@apple.com>
2024-08-30 14:47:51 +02:00
aryan-25 f32987422c Require Swift 6 (#341)
* Require Swift 6
* Apply formatter
* Disable 5.8, 5.9, and 5.10 checks
* Remove -warnings-as-errors for now
* Ensure plugin compiles in Swift 6 language mode
2024-08-28 11:10:15 +02:00
ab8166a39d [CI] Add GHA CI and release flow (#340)
Co-authored-by: Fabian Fett <fabianfett@apple.com>
Co-authored-by: Sébastien Stormacq <sebastien.stormacq@gmail.com>
Co-authored-by: Mahdi Bahrami <github@mahdibm.com>
2024-08-26 16:36:07 +02:00