marcodaniels / elm-aws-cloudfront / CloudFront

The cloudFront function requires an input and output ports as a tuple parameter in order to communicate with the JavaScript handler code.

port module MyModule exposing (..)

import CloudFront exposing (cloudFront)
import Json.Decode as Decode
import Json.Encode as Encode

port inputEvent : (Decode.Value -> msg) -> Sub msg

port outputEvent : Encode.Value -> Cmd msg

The ports must have the same type as the example.

cloudFront : (flags -> Maybe Lambda.InputOrigin -> Lambda.OutputEvent) -> ( (Json.Decode.Value -> Msg) -> Platform.Sub.Sub Msg, Json.Encode.Value -> Platform.Cmd.Cmd Msg ) -> Platform.Program flags (Model flags) Msg

Create a CloudFront origin handler to handle the request/response of your CloudFront distribution:

import CloudFront.Lambda exposing (originRequest, toRequest)

( inputPort, outputPort )
    |> (originRequest
            (\{ request } _ -> request |> toRequest)
            |> cloudFront
       )

Core Model and Msg


type alias Model a =
{ event : Maybe Lambda.InputEvent
, flags : a 
}

Model contains the decoded InputEvent and the optional flags passed on init.


type Msg
    = Input (Result Json.Decode.Error Lambda.InputEvent)

Msg is used to contain the result/error of decoding the input event from ports.

platformWorker : (flags -> Maybe Lambda.InputOrigin -> Lambda.OutputEvent) -> ( (Json.Decode.Value -> Msg) -> Platform.Sub.Sub Msg, Json.Encode.Value -> Platform.Cmd.Cmd Msg ) -> { init : flags -> ( Model flags, Platform.Cmd.Cmd Msg ), update : Msg -> Model flags -> ( Model flags, Platform.Cmd.Cmd Msg ), subscriptions : Model flags -> Platform.Sub.Sub Msg }