Creating a AWS Lambda function with Haxe
Tags: aws, serverless, haxe, gamedev
Published: 2021-02-16 23:46:47

---
title: Creating a AWS Lambda function with Haxe
published: true
date: 2021-02-16 23:38:13 UTC
tags: aws,serverless,haxe,gamedevelopment
canonical_url: https://medium.com/@josephbmanley/creating-a-aws-lambda-function-with-haxe-ac0152b42fe6
---

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](https://dead-cells.com/). While exporting to JavaScript, I thought, “Hey, would this work with Lambda?” Sure, enough, it can.

![](https://cdn-images-1.medium.com/max/362/0*gXb79Qxrgx4FYW7M.png)<figcaption>Image Source: Haxe Foundation</figcaption>

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
```

Here is my compile.hxml :

```
-cp src
--js export/handler.js
--main Handler
```

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

```
class Handler {
    static function main() {}
}
```

> 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() {}
}
```

Then we can add our lambda handler function:

```
class Handler {
    static function main() {}
    static function handler() : String {
        return "Hello from Haxe!"
    }
}
```

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);
}
```

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

![](https://cdn-images-1.medium.com/max/1024/1*k62gCl9q37M7BSUW-WMXTg.png)

### 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!';
    }
}
```

![](https://cdn-images-1.medium.com/max/1024/1*U4ibbqYMRNyCy0odHJ-2_g.png)

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!';
    }
}
```

![](https://cdn-images-1.medium.com/max/1024/1*ZJKvvGA5CzTull-eYIiuRg.png)

#### Related Links

Source on GitHub: [https://github.com/josephbmanley/haxe-lambda-example](https://github.com/josephbmanley/haxe-lambda-example)

Official Haxe Website: [https://haxe.org/](https://haxe.org/)