Deploying Haskell applications to AWS Lambda with Serverless
There are two ways to start, either via the stack template, or directly modifying a project. You may want to use the manual approach as the template specifies a specific stack resolver as it needs to hardcode the
In either case, you will want to have Serverless installed, eg.
npm install -g serverless.
Create a Stack package for your code:
stack new mypackage https://raw.githubusercontent.com/seek-oss/serverless-haskell/master/serverless-haskell.hsfiles
Update the resolver in the
stack.yaml file. This is hardcoded as the resolver number is not known at template interpolation time. You should pick either the latest resolver, or one you have used before and have thus prebuilt many of the core packages for.
Install the dependencies and build the project:
cd mypackage npm install stack build sls invoke local -f mypackage-func
This should invoke serverless locally and display output once everything has built.
Create a Stack package for your code:
stack new mypackage
LTS 17 are supported, older versions are likely to work too but untested.
Initialise a Serverless project inside the Stack package directory and install
cd mypackage npm init -y npm install --save serverless firstname.lastname@example.org
The version of the NPM package to install must match the version of the Haskell package.
serverless.yml with the following contents:
service: myservice provider: name: aws runtime: haskell functions: myfunc: handler: mypackage.mypackage-exe # Here, mypackage is the Haskell package name and mypackage-exe is the # executable name as defined in the Cabal file. The handler field may be # prefixed with a path of the form `dir1/.../dirn`, relative to # `serverless.yml`, which points to the location where the Haskell # package `mypackage` is defined. This prefix is not needed when the # Stack project is defined at the same level as `serverless.yml`. plugins: - serverless-haskell
import qualified Data.Aeson as Aeson import AWSLambda main = lambdaMain handler handler :: Aeson.Value -> IO [Int] handler evt = do putStrLn "This should go to logs" print evt pure [1, 2, 3]
dependencies: - base >= 4.7 && < 5 - aeson - serverless-haskell
Build and test locally using
sls invoke local:
serverless-haskell plugin will build the package using Stack. Note that
the first build can take a long time. Consider adding
export SLS_DEBUG=* so
you can see what is happening.
export SLS_DEBUG=* sls invoke local -f myfunc
sls deploy to deploy the executable to AWS Lambda.
serverless-haskell plugin will build the package using Stack, then upload
from/to AWS Lambda.
export SLS_DEBUG=* sls deploy
You can test the function and see the invocation results with:
sls invoke -f myfunc`
This plugin supports handling API Gateway requests. Declare the HTTP events
serverless.yml and use
in the handler to process them.
Serverless Offline can be used for local testing of API Gateway requests. You
--useDocker flag so that the native Haskell runtime works correctly.
When using Serverless Offline, make sure that the project directory is world-readable, otherwise the started Docker container will be unable to access the handlers and all invocations will return HTTP status 502.
See AWSLambda for documentation, including additional options to control the deployment.
master branch is the stable version. It is normally released to Hackage once
new changes are merged via Git tags.
The package is also maintained in Stackage LTS, provided the dependencies are not blocking it.
Integration test verifies that the project can build and deploy a complete function to AWS, and it runs with expected functionality.
Integration test is only automatically run up to deployment due to the need for an AWS account. To run manually:
Ensure you have the required dependencies:
./integration-test/run.sh. The exit code indicates success.
stack.yaml. To specify a different series, use
--no-clean-dir. This can speed up repeated test runs, but does not guarantee the same results as a clean test.
./bumpversion major|minor|patch. This will increment the version number, update the changelog, create and push the Git tag and the branch.
master, taking care of the conflicts around version numbers and changelog, and release the latest version as well.
Latest commit b2f54ec on Sep 24, 2017