Announcing Swift AWS Lambda Runtime

This may not be an issue specific to this project, but I am seeing some odd behavior. I was just playing around with the square number example and I have this code that does not compile

Lambda.run { (context: Lambda.Context, input: Input, callback) in
    context.logger.info("input is \(input.number)")
    callback(.success(Output(result: input.number * input.number)))
}

with an error on the callback line of

Cannot convert value of type 'Output' to expected argument type 'Void'

however if I add a type hint, it works just fine

Lambda.run { (context: Lambda.Context, input: Input, callback: (Result<Output, Error>) -> Void) in
    context.logger.info("input is \(input.number)")
    callback(.success(Output(result: input.number * input.number)))
}

Is this just bad type inference on the swift compiler, or is there an issue with the library? I dug into the code and it seems like it's just swift choosing the Void callback vs the Out callback, but I can't see why it would make that choice. Also of note, the non type-hinted version works just fine without the logging line. (as below with the line commented out)

Lambda.run { (context: Lambda.Context, input: Input, callback) in
    //context.logger.info("input is \(input.number)")
    callback(.success(Output(result: input.number * input.number)))
}

Thanks for reporting @brendankirchner! There's a ticket about this you may want to follow here: Lambda.run cannot be used without explicit types Β· Issue #107 Β· swift-server/swift-aws-lambda-runtime Β· GitHub

1 Like

Hi Chris,

I just posted a couple of blog posts related to the swift aws lambda runtime. The second one details using it with AWS SDK Swift.

Messing with Swift AWS Lambda Runtime (Part 1) - Optical Aberration and Messing with Swift AWS Lambda Runtime (Part 2) - Optical Aberration

3 Likes

Awesome. Thank you Adam!

I just made this a little more convenient using swift lambda, you can read about it over here: Deploying Swift on AWS Lambda – Helge Heß – Software engineer..

It uses SPMDestinations for cross compiling Amazon Linux binaries directly on macOS, and then has two wrapper scripts (swift lambda build and swift lambda deploy) which do the heavy lifting and make it even more convenient.

As usual: feedback welcome!

9 Likes

Great source of information. I need to try to port my examples written for the managed version of Apache OpenWhisk project with Swift that I described here in Serverless Swift book by Apress https://www.apress.com/us/book/9781484258354 . I will let you know about porting my examples to Swift Lambda :-9