A New Clone Army:

ClojureScript on Lambda

@kevin_noonan

Presented at @clojureireland on 7th Sept 2016.

Hit the [right-arrow] key to proceed.

"Serverless usually refers to an architectural pattern where the server-side logic is run in stateless compute containers that are event-driven and ephemeral."

- Software Engineering Daily podcast.

Colour Me Blue!

3 Runtimes for Lambda

NodeJS

Java Virtual Machine

Python 2.7

Amazon Cloud Jargon

AWS - Amazon Web Services

S3 - file storage service

API Gateway - give a Lambda function a public URL

EC2 - Amazon's virtual server.

Sample applications for Lambda

Process photo / video (trigger: upload to S3 bucket)

Scheduled jobs for data-analytics, etc.

API backend for mobile or web app

Why not Clojure?

Delay for cold starts of the container & JVM.

The frequency of cold starts is unpredictable.

OK for a 'background task' (startup time isn't critical).

Clojure rocks and JavaScript reaches.

Rich Hickey, NYC Clojure meet up 20 July 2011

Requirements to run ClojureScript on Lambda

Amazon Web Services account

aws-cli (command-line tools)

Node

Leiningen

Lambda creation on AWS console

(This is not required. We'll create the lambda on the command-line shortly.)

Triggers for a Lambda function

Prep local environment for AWS

Create a new project

lein new cljs-lambda clonezero

(NB: no need to download cljs-lambda in advance.)

Key elements of project.clj

Create a security role on AWS for the Lambda

lein cljs-lambda default-iam-role

This is just enough to run the Lambda. It'll need more for database-access etc.

'Main' function for Lambda

My Lambda's 'Main' function

Deploy Lambda code to Amazon's cloud

lein cljs-lambda deploy

Invoke Lambda from command-line

lein cljs-lambda invoke work-magic '{ "magic-word": "clonezero-token", "spell": "adder", "x":29, "y":13 }'

Lambda function on the AWS console

Click on "Triggers" to use (say) API Gateway to trigger Lambda.

API Gateway for Lambda

API Gateway for Lambda

Note the public URL to call the Lambda.

Create API key to secure Lambda

Note the generated string of the API key.

Enable CORS (to call API from a browser)

Call Lambda via API Gateway (with Httpie)

http POST https://8hjoags647.execute-api.eu-west-1.amazonaws.com/prod/work-magic X-API-KEY:dy30nXp2gl3RSFGdCqjmH6h35o7SS8RL1vT7bwWr magic-word=clonezero-token spell=adder x:=13 y:=29

Note the API key. Httpie converts it to a HTTP header.

Httpie converts the other parameters to a JSON request-body.

Other cloud-hosted Amazon services you may need...

DynamoDB - NoSQL datastore

S3 - filesystem

ElastiCache - Redis key-value store

SQS - queueing system (similar in principle to RabbitMQ)

RDS - Postgres, MySQL, MariaDB or Oracle.

Pros

Cheap-to-run

Massively scalable, with auto-scaling

Highly available

(Depending on Amazon's uptime is a better bet than servers you run.)

Amazon maintains the servers & other infrastructure.

For you: no upgrades, no need to apply security patches, etc.

Cons

Heavy dependence / lock-in to Amazon Web Services.

Delay for cold start (if you switch to Java).

Hard 300 second limit on a single Lambda.
(Workaround: spawn more Lambdas.)

No built-in support for dev. / staging / prod.
(Workaround: read config-file from S3, based on name of Lambda.)

Difficult to run arbitrary processes, as on a server.
(Workaround: run them on Amazon EC2 servers.)