Giter Club home page Giter Club logo

aws-lambda-swift's Introduction

aws-lambda-swift

Swift 5.1

The goal of this project is to implement a custom AWS Lambda Runtime for the Swift programming language.

Step 1: Implement a lambda handler function

Examples/SquareNumber is an SPM package with a single, executable target that implements the lambda handler function. This package depends on the AWSLambdaSwift package which produces a library that contains the actual runtime. In the main.swift file of the SquareNumber executable we import the AWSLambdaSwift library, instantiate the Runtime class and then register our handler function. Finally, we start the runtime:

import AWSLambdaSwift

struct Input: Codable {
    let number: Double
}

struct Output: Codable {
    let result: Double
}

func squareNumber(input: Input, context: Context) -> Output {
    let squaredNumber = input.number * input.number
    return Output(result: squaredNumber)
}

let runtime = try Runtime()
runtime.registerLambda("squareNumber", handlerFunction: squareNumber)
try runtime.start()

The handler function takes two arguments: the input object which can be of any type that conforms to the Decodable protocol and the context object. It then processes the input and returns an output. The output can be of any type that conforms to the Encodable protocol.

Alternatively, you can also define a handler function which takes and returns a JSONDictionary. This type is just a typealias for the type Dictionary<String, Any>:

import AWSLambdaSwift

extension String: Error {}

func squareNumber(input: JSONDictionary, context: Context) throws -> JSONDictionary {
    guard let number = input["number"] as? Double else {
        throw "invalid input data"
    }

    let squaredNumber = number * number
    return ["result": squaredNumber]
}

let runtime = try Runtime()
runtime.registerLambda("squareNumber", handlerFunction: squareNumber)
try runtime.start()

Step 2: Build the lambda

AWS Lambdas run on Amazon Linux (see https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html). This means that we can't just run swift build on macOS because that will produce a macOS executable which doesn't run on Linux. Instead, I have used Docker to build the SquareNumber package. Execute the following command to build the SquareNumber package and bundle the executable in the lambda.zip file:

make package_lambda

Step 3: Build the layer

We now have a Linux executable. However, this executable dynamically links to the Swift standard library and a bunch of other shared libraries (Foundation, Grand Central Dispatch, Glibc, etc). Those libraries are not available on Amazon Linux. Thus, I created an AWS Lambda Layer which contains all of those shared libraries. The AWS Lambda can then reference this layer. This makes sure that we only have to upload the libraries once instead of every time we want to update the lambda. Run the following command to create a swift-lambda-runtime.zip file that contains the bootstrap file and the libraries for the layer:

make package_layer

The bootstrap file is a simple shell script that launches the executable.

Step 4: Setup the layer on AWS

Create a new lambda layer in the AWS Management console using the swift-lambda-runtime.zip file:

Create a new layer

This might take a few minutes. Once the upload is done, the layer's ARN (Amazon Resource Name) is shown on the page. In the next step we will use this ARN to reference the layer from the lambda.

Step 5: Setup the lambda on AWS

First create a new lambda function in the AWS Management console and select "Use custom runtime in function code or layer":

Create a new lambda function

Next, select the lambda.zip file as the function package to upload and set the handler to "SquareNumber.squareNumber". The first part of the handler should be the same as the name of the executable. The second part of the handler should be the same as the name that has been used to register the lambda handler in the runtime (see Step 1):

Upload the function package and set the handler

Click on the "Layers" button and then on the "Add a layer" button. Reference the layer we have created in Step 4 using its ARN and click on the "Add" button:

Reference the layer from the lambda

Finally, click the "Save" button in the upper right corner in order to save these changes.

Step 6: Test the lambda

Now the lambda function is ready for testing. Click on "Select a test event" and then on "Configure test events". Configure a new test event like this:

Create a new test event

Then call the lambda function with the new test event by clicking on the "Test" button. You should see the following result:

Successful lambda execution result

aws-lambda-swift's People

Contributors

fabianfett avatar natanrolnik avatar tonisuter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-lambda-swift's Issues

Random crash

This is a lambda function with proxy integration connected to an API Gateway.
It randomly crashes with this log:

18:33:58
START RequestId: d78628da-a0c9-4e39-be97-5c0a604dadf9 Version: $LATEST
18:33:58
Fatal error: Error raised at top level: AWSLambdaSwift.RuntimeError.endpointError("The operation could not be completed"): file /home/buildnode/jenkins/workspace/oss-swift-5.0-package-linux-ubuntu-18_04/swift/stdlib/public/core/ErrorType.swift, line 200
18:33:58
Current stack trace:
18:33:58
0 libswiftCore.so 0x00007fefdf1387a0 _swift_stdlib_reportFatalErrorInFile + 115
18:33:58
1 libswiftCore.so 0x00007fefdf0739cc <unavailable> + 3463628
18:33:58
2 libswiftCore.so 0x00007fefdf073abe <unavailable> + 3463870
18:33:58
3 libswiftCore.so 0x00007fefdee6f31a <unavailable> + 1348378
18:33:58
4 libswiftCore.so 0x00007fefdf047ab2 <unavailable> + 3283634
18:33:58
5 libswiftCore.so 0x00007fefdeec4d6e <unavailable> + 1699182
18:33:58
6 myFunc 0x00007fefdfd75f80 <unavailable> + 335744
18:33:58
7 libc.so.6 0x00007fefdd7eeab0 __libc_start_main + 231
18:33:58
8 myFunc 0x00007fefdfd34e5a <unavailable> + 69210

It seems to have something to do with URLSession on Linux. I have tried to print more info, but all I could get was: Foundation.URLError(_nsError: The operation could not be completed.

Also, both HTTPResponse and data are empty.

@tonisuter thoughts?

Where does the list of required shared libraries come from?

Hi Toni, great project! I found it through your post on the Swift forums.

I modified your project to write my own little function. Since my function uses Swift 5 features, I switched the Docker image to codevapor/swift:5.0.

Everything seemed to work, but when I tested my function on AWS, it failed with:

error while loading shared libraries: libswiftDispatch.so: cannot open shared object file: No such file or directory

screen shot 2018-12-25 at 21 04 36

It looks like the source of this error is that the names of one or more shared libraries changed in Swift 5, e.g. libdispatch.so is now called libswiftDispatch.so. So I need to update the list of shared libraries in the Makefile. Can I ask how you generated this list?

Running ldd [path_to_my_binary] on Linux seems to be an option to get a list of the shared libraries loaded by an executable, but I wanted to ask if you used a different option.

Thanks and happy holidays!

PS: Don't let me stop you from enjoying your holidays. This isn't urgent.

Failure of 'make package_layer' command.

A great project. I love Swift and AWS and I am looking to combine the two with your amazing work and hoping to contribute in the future if I can.

I do have an issue when creating the swift-runtime zip file. I do feel that I am getting a user side error when running the make package_layer command. I receive the following errors:
cp: cannot stat '/lib/x86_64-linux-gnu/libcrypto.so.1.0.0': No such file or directory cp: cannot stat '/lib/x86_64-linux-gnu/libssl.so.1.0.0': No such file or directory cp: cannot stat '/usr/lib/x86_64-linux-gnu/libicudata.so.55': No such file or directory cp: cannot stat '/usr/lib/x86_64-linux-gnu/libicui18n.so.55': No such file or directory cp: cannot stat '/usr/lib/x86_64-linux-gnu/libicuuc.so.55': No such file or directory cp: cannot stat '/usr/lib/x86_64-linux-gnu/libidn.so.11': No such file or directory make: *** [package_layer] Error 1

I do feel like I am missing those libraries locally but can't seem to figure out the installion of them. Any help will be appreciate. (I do feel like this is a beginner issue so apologies in advance.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.