hs-aws-lambda: A modern and easy-to-use wrapper for Docker-based Lambda implementations. Now with API Gateway support!

[ aws, library ] [ Propose Tags ] [ Report a vulnerability ]

Modules

[Last Documentation]

  • AWS
    • ApiGateway
      • AWS.ApiGateway.RuntimeAPI
        • AWS.ApiGateway.RuntimeAPI.Types
    • Lambda
      • AWS.Lambda.RuntimeAPI
        • AWS.Lambda.RuntimeAPI.Types

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4, 0.2.0.0
Dependencies aeson, base (>=4.7 && <5), base64, bytestring, case-insensitive, containers, deepseq, http-client, http-types, safe-exceptions, text, time (>=1.9.3 && <2), unliftio, vector [details]
License LicenseRef-PublicDomain
Copyright Released under the Unlicense: https://unlicense.org/
Author Robert Fischer
Maintainer smokejumperit@gmail.com
Category AWS
Home page https://github.com/RobertFischer/hs-aws-lambda#readme
Bug tracker https://github.com/RobertFischer/hs-aws-lambda/issues
Source repo head: git clone https://github.com/RobertFischer/hs-aws-lambda
Uploaded by RobertFischer at 2021-07-12T18:00:24Z
Distributions NixOS:0.2.0.0
Downloads 762 total (16 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
Last success reported on 2021-07-12 [all 1 reports]

Readme for hs-aws-lambda-0.2.0.0

[back to package description]

The Library for Easily Writing Your Lambdas in Haskell

Historically, if you wanted to use Haskell to write your Lambdas, you had to use a custom runtime. However, AWS now allows you to use Docker images instead, which simplifies deployments and avoids some nasty problems.

This library provides the translation layer that you need in order to fully leverage Lambda. It is implemented in a classy way for maximum flexibility, even if it makes the type signatures a bit complicated. There is an example of an implementation in the example folder, including a demonstration of how to do deployment. Here's the simplest usage, adapted slightly from the example:


import AWS.Lambda.RuntimeAPI ( runLambda )
import AWS.Lambda.RuntimeAPI.Types ( LambdaResult(..), LambdaInvocation(..) )
import Data.Aeson ( encode, Value )
import qualified Data.ByteString.Lazy.Char8 as C8

-- Two lines of boilerplate
main :: IO ()
main = runLambda handler

-- Your implementation's code
handler :: LambdaInvocation Value -> IO (LambdaResult String)
handler request = do
	putStrLn . C8.unpack $ encode request
	return $ LambdaSuccess "This is the result payload"

The request payload is wrapped in a LambdaInvocation type, which provides access to the particular invocation that is being processed. In this case, we're treating the type as an Aeson Value, which represents any arbitrary JSON structure.

Your handler is responsible for consuming that request and producing a LambdaResult. A LambdaResult instance is usually either LambdaSuccess payload or LambdaError (Text, Text). In the case of LambdaSuccess, the payload defines the "result" of the Lambda, and will be transformed into JSON via the ToJSON typeclass. In the case of LambdaError, the payload is (ErrorType, ErrorMessage), which is passed to the Lambda Runtime API in order to give a meaningful error to the user. There is also LambdaNop which does nothing, but that's intended for internal use only.

All of this executes in the IO monad in the example above, but you can use any monad that implements MonadUnliftIO, MonadFail, and MonadThrow.

And that's pretty much it.