Serverless Server Side Swift with Hexaville

Yuki Takei
May 24, 2017 · 6 min read

Hello World.

I’m writing client and server applications with Swift at my company.
Our company has only two people(me and co-founder/Engineer) and running by our own money.
So we don’t have abundant money to launch a lot of server instances usually and difficult to hire other people to operate/observe our clusters in 24 hours a day.

I hope Serverless computing is one of the solution for this situation in the beginning of such a venture well.

Serverless computing

Serverless computing, also known as function as a service (FaaS), is a cloud computing code execution model in which the cloud provider fully manages starting and stopping of a function’s container platform as a service (PaaS) as necessary to serve requests, and requests are billed by an abstract measure of the resources required to satisfy the request, rather than per virtual machine, per hour

In the Serveless comuputing, The charging is occured when the function is executed(it’s incredibly cheap) so we don’t need to pay fixed cost for Server Instances.
And most of the case, Cloud provider provide it as fully managed service, therefore We don’t have to worry about server scaling in/out and downing.

It seems perfectly nice, but there are disadvantages as well.

  • Locked into the cloud service
  • Required proficient in the cloud service
  • Your Service goes down when the cloud service is down
  • Deploying and routing from the control panel(Not programmatically)
  • Have to use the programming languages that serverless service provides
  • Can’t keep connection between middlewares
  • Limited parallelism(Needs upper limit relaxation application)

Hexaville made it wanting to build Swift’s web application in a serverless environment while overcoming some of these drawbacks to some extent.

Hexaville

The modern serverless web application engine and framework for Swift

The concept is execute `web apps made by Swift` as it on lambda+api-gateway.

Hexaville is an engine that can build a serverless environment for Swift and its web application framework. entire codes written in Swift. (Think of it as the Swift version of the serverless)

Currently supports only AWS lambda+api-gateway.

It’s devided two layers, one is for creating environment, building swift and deploying, the other one is framework for creating application that is actually executed in the lambda that called HexavilleFramework.

Using Hexaville for creating serverless application and deploying to the cloud

let’s try building a serveless application with Hexaville from now on. Installation is super easy. just clone and build.

The every commands that I wrote following are executed on my Mac(El Capitan, Xcode 8.3.2)

Installation

Generate a Hexaville Project

First, try to execute Hexaville without any options.

The output should be…

You would see the available commands so far, and then execute generate command to initialize the first Hexaville project.

After finishing generate, Hello directory should be created on the Desktop. Let’s look inside of the directory!

In the directory structure is basic structure for the Swift Package Manager Project. So you can automatically generate xcodeproj file with swift package generate-xcodeproj . It means You can develop Hexaville application with Xcode.

And then, open it with Xcode

main.swift

In the main.swift, the example code of HexavilleFramework application is written. It’s similar with web application made by express or sinatra.

At this time, deploying it without editing.

Edit Hexavillefile.yml

Hexavillefile.yml is a configuration file for Hexaville.

Open it and fill credential, region and role for lambda in. (You can omit credential and region and pass them as environment variables)

Deploy

If you did so far, Let’s Deploy it with deploy command!

This work will take a while….

Get the routing informations

Next let’s get the routing information to confirm available resources on the deployment stage.

Output should be…

Access the resources

Let’s try to access the endpoint with curl

If you can get html response from endpoint, deploying is successfully completed.

We got power!!!

Anatomy of Deployment flow

Well, We could confirm that it actually works, but currently lambda does not support swift runtime. I’ll explain how to execute Swift on lambda.

First, describe the deployment flow. following figure describes what happening in the deploying phase.

Attention to the `Build Swift on Docker` section.

If you build swift on the Mac and upload its binary to lambda, it won’t be able to executed cause lambda is executed on the Amazon Linux.

So, we need to use `Docker for Mac` to build and get binary that have compatibility with Linux_x86. And the built binary and related shared objects(*.so) will be deployed into the shared directory between host machine and docker with `-v` option.

Besides, to run Swift’s binary on Linux, the following .so files are required.

They are grouped in a directory, zipped and uploaded to AWS S3.

Also, it’s mysterious that the routing of the Hexaville application is reflected on the api-gateway as it is. aren’t you?

An application created with HexavilleFramewok is executable as command line application.

Attention to the gen-routing-manif command

Once execute this command, extract routing information from HexavilleFramework app and print it as .routing-manifest.json.

This .routing-manifest.json is mapped to the parameters of RestAPI of api-gateway.

How is Swift executed on Lambda

At the last, I’ll explain you how Swift’s binary is executed in the lambda.

As shown in this figure, we use the node.js as runtime of lambda, and spawn Swift’s binary as a process from child_process module.

After spawning, connecting the parent and child with pipe and sending message from child with original protocol.

PS

How was it?

Although Hexaville still has few functions, and not many things that can do, but I’d like to take advantage of actual operation and constantly provide feedback from it.

I plan to implement following features for the next.

  • logging pipeline(Cloud watch logs -> Amazon Athena)
  • VPC Supports
  • Custom Domain Supports
  • Binary Media type Supports

If it got these features, I believe it will become one of the choice to create practical web application tool.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store