This Haskell library turns any wai webapp (spock, servant, etc) into a handler for AWS Lambda and API Gateway requests.
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Lambda (run)
app :: Application
app _ respond = do
putStrLn "I've done some IO here"
respond $ responseLBS
status200
[("Content-Type", "text/plain")]
"Hello, Web!"
main :: IO ()
main = run app
This doesn't require any particular Lambda environment. By following the
instructions in the build section the resulting zip
file uploaded
to AWS Lambda is typically smaller than 1MB. For basic webapps the request
handling duration (as reported by AWS) is between 1 and 5 milliseconds.
- Install with either Cabal, stack or Nix.
- Build for AWS with Nix and fully static executables.
- Deploy to AWS with Nix and Terraform.
Installing with Cabal:
$ cabal install wai-lambda
If you use stack and wai-lambda
is not included in your snapshot, add it to
the extra-deps
:
extra-deps:
- wai-lambda-0.1.0.0
or from GitHub:
packages:
- ...
- location:
git: https://github.com/deckgo/wai-lambda.git
commit: <some commit>
extra-dep: true
You do not have to do anything if you use cabal2nix and a Nix commit that
includes wai-lambda
in its Hackage dependencies. If you want to use a
specific commit of wai-lambda
we recommend using niv:
$ niv add deckgo/wai-lambda
Then add an override to your Nix packages:
rec
{
sources = import ./nix/sources.nix; # from niv
pkgs = import sources.nixpkgs {};
myHaskellPackages = pkgs.haskellPackages.override
{ overrides = self: super:
{ "wai-lambda" =
super.callCabal2nix "wai-lambda" sources.wai-lambda;
};
};
}
See also the build section for producing AWS Lambda packages with nix.
TODO
For inspiration on how to build fully static executables with Nix, see DeckDeckGo
TODO
For inspiration on how to deploy with Terraform and Nix, see DeckDeckGo
MIT © David Dal Busco and Nicolas Mattia