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

Safe HaskellNone



Entry point for AWS Lambda handlers deployed with serverless-haskell plugin.



lambdaMain Source #


:: (FromJSON event, ToJSON res, MonadCatch m, MonadIO m) 
=> (event -> m res)

Function to process the event

-> m () 

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.

lambdaMainRaw :: (MonadCatch m, MonadIO m) => (ByteString -> m ByteString) -> m () Source #

Process the incoming requests (using the AWS Lambda runtime interface or from the standard input). Also set line buffering on standard output for AWS Lambda so the logs are output in a timely manner. This function provides a lower level interface than lambdaMain for users who don't want to use Aeson for encoding and decoding JSON.