Created
November 26, 2015 19:08
-
-
Save robinheghan/610447ce20bf25d7c8bb to your computer and use it in GitHub Desktop.
AWS Lambda Elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related work and shameless Plug: https://github.com/ktonon/elm-serverless (pun intended)