A boilerplate snaplet to conveniently allow amazonka aws functions to be lifted into snap web handlers.
- Add the
AWSEnv
to your app's core state:
import Snap.Snaplet.AWS
data App = App
{...
,_awsEnv :: Snaplet AWSEnv
,...
}
- Derive (or manually create) lenses:
makeLenses ''App
- Implement a
HasEnv
instance:
instance HasEnv App where
environment = awsEnv . environment
- In your initialisation function, intialise like so, providing the desired credential discovery method:
app :: SnapletInit App App
app = makeSnaplet "app" "Your application" Nothing $ do
...
awsSnaplet <- nestSnaplet "aws" awsEnv $ awsEnvInit Discover
...
return $ App ... awsSnaplet ...
- Ensure your
Handler b v
has aMonadCatch
instance. E.G.
-- Type-golf implementation...
instance MonadCatch (Handler App App) where
catch m f = join $ liftIO $ catch (return m) (\x -> return $ f x)
- You should now be able to call
liftAWS
to lift aws functions into your handler. E.G.:
-- Send a test email to a given address, returning whether sending was deemed successful by AWS.
sendTestEmail :: Text -> Handler App App Bool
sendTestEmail emailTo = do
let emailBodyText = "<h1>Test</h1>"
emailFrom = "test@ourAddress.com"
emailTo = destination & dToAddresses .~ [emailTo]
emailSubject = content "Test email"
emailBody = body & bHTML .~ (Just $ content $ emailBodyText)
emailMessage = message emailSubject emailBody
awsEmail = sendEmail emailFrom emailTo emailMessage
resp <- liftAWS $ within Ireland $ send awsEmail
case resp&.sersResponseStatus of
200 -> return True
_ -> return False