DEV Community

Joseph B. Manley for AWS Community Builders

Posted on • Originally published at Medium on

Creating a AWS Lambda function with Haxe

Over the weekend, I had some fun and experimented with Haxe. For those unaware, Haxe is a typed multi-platform high-level programming language mostly used for game development, such as the indie hit Deadcells. While exporting to JavaScript, I thought, “Hey, would this work with Lambda?” Sure, enough, it can.

Image Source: Haxe Foundation

Is it very practical? I’m not so sure. Haxe isn’t as popular as Python or JavaScript, therefore not having as much third-party support, and Haxe isn’t run directly but rather as an exported language of your choice. But if you’re a Haxe shop or want a strongly typed language that can run well and easily on AWS, it’s worth a quick look.

Bellow, I put together a quick guide on how to get started with Haxe on Lambda.

Building a simple Haxe Lambda function

First you need to setup your Haxe project. For this guide, I am going to use Handler as my main class and will export to JavaScript to export/handler.js

Here is my structure:

├── compile.hxml
├── export // Note: Directory won't exist until compile
│ └── handler.js
└── src
    └── Handler.hx
Enter fullscreen mode Exit fullscreen mode

Here is my compile.hxml :

-cp src
--js export/handler.js
--main Handler
Enter fullscreen mode Exit fullscreen mode

Now let’s jump into the Haxe code. Here is the start of my src/Handler.hx :

class Handler {
    static function main() {}
}
Enter fullscreen mode Exit fullscreen mode

As a note here, while Handler.main is not executed by the Lambda function, it is still required to property export your Haxe project.

It’s actually really simple, first we just have to expose the Handler class, you that it can be accessed by JavaScript files by adding @:expose

@:expose
class Handler {
    static function main() {}
}
Enter fullscreen mode Exit fullscreen mode

Then we can add our lambda handler function:

class Handler {
    static function main() {}
    static function handler() : String {
        return "Hello from Haxe!"
    }
}
Enter fullscreen mode Exit fullscreen mode

Now go and compile your code, to do so call haxe compile.hxml from your project directory.

Now the big secret to getting your function called is by passing your function through to the lambda function. This can be achieved importing your exported Javascript and calling the function from an async method. This file will become the entrypoint for your Lambda function. In my case it’s export/index.js

const handler = require("handler");

exports.handler = async function(event, context, callback) {
    return handler.Handler.handler(event,context,callback);
}
Enter fullscreen mode Exit fullscreen mode

Now, if we zip up our export directory and upload it to our lambda function…

Using Reading inputs

If you noticed in the previous part, we pass the event, context, and callback objects all to our function in index.js . This is because we can actually reference those objects as an AnonType and referencing the properties we would like to access. Here’s an example using the event object:

@:expose
class Handler {
    static function main() {}

    static function handler(event : {source : String}) : String {

        // Run your Haxe code
        var eventSource: String = event.source;

        // Return response
        return 'Hello to $eventSource from Haxe!';
    }
}
Enter fullscreen mode Exit fullscreen mode

Now with the context object:

@:expose
class Handler {
    static function main() {}

    static function handler(event : {source : String}, context : {functionName : String}) : String {

        // Run your Haxe code
        var eventSource : String = event.source;
        var lambdaName : String = context.functionName;

        // Return response
        return 'Hello to $eventSource from $lambdaName!';
    }
}
Enter fullscreen mode Exit fullscreen mode

Related Links

Source on GitHub: https://github.com/josephbmanley/haxe-lambda-example

Official Haxe Website: https://haxe.org/

Top comments (0)