serverless-haskell-0.3.0: Deploying Haskell code onto AWS Lambda using Serverless

Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

AWSLambda

Description

Tools for running Haskell on AWS Lambda using Serverless.

Usage

To deploy a Haskell function on AWS Lambda:

  • Initialise a Serverless project in the same directory as your Stack-enabled package and install the serverless-haskell plugin:
npm init .
npm install --save serverless serverless-haskell
  • Create serverless.yml with the following contents:
provider:
  name: aws
  runtime: nodejs6.10

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

plugins:
  - serverless-haskell
  • Write your main function using lambdaMain.
  • Use sls deploy to deploy the executable to AWS Lambda. Note: sls deploy function is not supported.

The serverless-haskell plugin will build the package using Stack and upload it to AWS together with a JavaScript wrapper to pass the input and output from/to AWS Lambda.

You can test the function and see the invocation results with sls invoke myfunc.

Additional features

Configuration options are passed to the plugin under haskell key in custom section of serverless.yml.

  • To add flags to stack build, specify them as an array under stackBuildArgs:
custom:
  haskell:
    stackBuildArgs:
      - --pedantic
      - --allow-different-user
  • To start the executable with extra arguments, add them to arguments under the function name:
custom:
  haskell:
    arguments:
      myfunc:
        - --arg1
        - --arg2
        - arg3
  • To include dependent libraries, list them under extraLibraries. The libraries will be uploaded to AWS Lambda along with the executable.
custom:
  haskell:
    extraLibraries:
      - libpcre.so.3

Synopsis

Documentation

lambdaMain Source #

Arguments

:: (FromJSON event, ToJSON res) 
=> (event -> IO res)

Function to process the event

-> IO () 

Process incoming events from serverless-haskell using a provided function.

The handler receives the input event given to the AWS Lambda function, and its return value is returned from the function.

This is intended to be used as main, for example:

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]

The handler function can receive arbitrary JSON values from custom invocations, or one of the events from the AWSLambda.Events module, such as S3Event:

import AWSLambda.Events.S3Event

handler :: S3Event -> IO ()
handler evt = do
  print $ records evt

If the Lambda function needs to process several types of events, use Alternative to combine several handlers:

import AWSLambda
import AWSLambda.Events.S3Event
import Data.Aeson
import Data.Aeson.Alternative

main = lambdaMain $ handlerS3 `alternative` handlerCustom

handlerS3 :: S3Event -> IO ()
handlerS3 = _

handlerCustom :: Value -> IO ()
handlerCustom = _

When run outside the AWS Lambda environment, the input is read as JSON from the command line, and the result of the execution is printed, also as JSON, to the standard output.