An open API service indexing awesome lists of open source software.

https://github.com/pmlopes/vertx-lambda-runtime


https://github.com/pmlopes/vertx-lambda-runtime

Last synced: 9 months ago
JSON representation

Awesome Lists containing this project

README

          

# vertx lambda

## Run native Lambda's with Vert.x

*Disclaimer - This project should be considered a POC and has not been tested or verified for production use.
If you decided to run this on production systems you do so at your own risk.*

## Building this Runtime

This runtime should give you all the boilerplate code for:

* AWS Lambda
* Knative
* OpenFaaS

## Compile the Runtime Classes

```
$ mvn package
```

## Writing your lambda

Create a project and add the dependency:

```xml

xyz.jetdrone
vertx.lambda
LATEST

```

Create a lambda as:

```java
package com.example;

import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import xyz.jetdrone.vertx.lambda.Lambda;

public class MyLambda implements Lambda {
@Override
public void handle(Message event) {
System.out.println("HEADERS: " + event.headers());
System.out.println("BODY: " + event.body());

// Here your business logic...

event.reply("OK");
}
}
```

And register your class in the service loader `src/main/resources/META-INF/services/xyz.jetdrone.vertx.lambda.Lambda`:

```
# List all available Lambdas here
com.example.knativelambda.MyLambda
```

## Build

### AWS Lambda

In your `pom.xml` add the plugins:

```xml



com.oracle.substratevm
native-image-maven-plugin
${graal.version}



native-image

package



xyz.jetdrone.vertx.lambda.knative.Bootstrap
bootstrap
--report-unsupported-elements-at-runtime --allow-incomplete-classpath --no-server



org.apache.maven.plugins
maven-assembly-plugin
3.1.1


xyz.jetdrone
vertx.lambda
LATEST




aws-function




package

single




```

The first plugin will compile your code to a `native-image` and the second
will `zip` your function as required by AWS.

### Knative

In your `pom.xml` add the plugins:

```xml



com.oracle.substratevm
native-image-maven-plugin
${graal.version}



native-image

package



xyz.jetdrone.vertx.lambda.knative.Bootstrap
bootstrap
--report-unsupported-elements-at-runtime --allow-incomplete-classpath --no-server



org.apache.maven.plugins
maven-dependency-plugin
3.1.1


unpack
package

unpack




xyz.jetdrone
vertx.lambda
LATEST
jar
false
${project.build.directory}
**/knative/*






```

The first plugin will compile your code to a `native-image` and the second
will `unpack` a default `service.yaml` to be used to deploy your lambda.

### OpenFaaS

In your `pom.xml` add the plugin:

```xml



com.oracle.substratevm
native-image-maven-plugin
${graal.version}



native-image

package



xyz.jetdrone.vertx.lambda.knative.Bootstrap
bootstrap
--report-unsupported-elements-at-runtime --allow-incomplete-classpath --no-server


```

The plugin will compile your code to a `native-image`.

## Next steps

Lambdas are exposed to `vert.x` event bus, however for `knative` and `openFaaS` you might
need to map the functions to URLs too. In this case you can override the method `alias`:

```java
public class MyLambda implements Lambda {
@Override
public String alias() {
return "/";
}
}
```

In this case the mapping is not base on the fully qualified class name, but on the given
alias.

# Finally

Deploy and profit!