Skip to content

Instantly share code, notes, and snippets.

@robinheghan
Created November 26, 2015 19:08
Show Gist options
  • Save robinheghan/610447ce20bf25d7c8bb to your computer and use it in GitHub Desktop.
Save robinheghan/610447ce20bf25d7c8bb to your computer and use it in GitHub Desktop.
AWS Lambda Elm
var gulp = require('gulp'),
$ = require('gulp-load-plugins')({lazy:true, camelize: true}),
join = require('path').join;
/*
* SETUP
*/
function path(rel) {
return join(__dirname, rel);
}
var config = {
paths: {
src: {
backend: path('/Main.elm'),
lambdaBackend: path('/Wrapper.js')
},
dist: {
filename: 'elm.js',
lambdaFilename: 'index.js',
archive: 'archive.zip',
backend: path('/out/')
}
}
};
/*
* JS
*/
gulp.task('elm-init', $.elm.init);
gulp.task('build-backend', ['elm-init'], function() {
return gulp.src(config.paths.src.backend)
.pipe($.elm())
.pipe($.rename(config.paths.dist.filename))
.pipe(gulp.dest(config.paths.dist.backend))
});
/*
* DEPLOY
*/
gulp.task('deploy', ['build-backend'], function() {
var elm = config.paths.dist.backend + config.paths.dist.filename;
return gulp.src([elm, config.paths.src.lambdaBackend])
.pipe($.concat(config.paths.dist.lambdaFilename))
.pipe($.uglify())
.pipe($.zip(config.paths.dist.archive))
.pipe($.awslambda('lambda_name', {region: 'us-east-1'}))
.pipe(gulp.dest(config.paths.dist.backend));
});
module Main where
import Signal
type alias Message =
{ operation : String
, message : String
}
port request : Signal Message
port response : Signal Message
port response = Signal.map handleRequest request
handleRequest : Message -> Message
handleRequest m =
case m.operation of
"PING" ->
{ m | operation = "PONG" }
x ->
{ m | operation = "UNKNOWN" }
{
"name": "elm-lambda-demo",
"dependencies": {},
"devDependencies": {
"gulp": "3.9.0",
"gulp-awslambda": "0.2.3",
"gulp-concat": "2.6.0",
"gulp-elm": "0.2.0",
"gulp-load-plugins": "1.1.0",
"gulp-rename": "1.2.2",
"gulp-uglify": "1.5.1",
"gulp-zip": "3.0.2"
}
}
var backend = Elm.worker(Elm.Backend.Main, {
request: {
operation: '',
message: ''
}
});
exports.handler = function(event, context) {
backend.ports.response.subscribe(function(response) {
context.succeed(response);
});
try {
backend.ports.request.send({
operation: event.operation,
message: event.message
});
} catch (e) {
context.fail('Bad Request: invalid input');
}
};
@neocris
Copy link

neocris commented Jun 17, 2016

what event triggers the lambda call?

@ktonon
Copy link

ktonon commented Feb 1, 2017

Related work and shameless Plug: https://github.com/ktonon/elm-serverless (pun intended)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment