Skip to content

Commit

Permalink
separate API creation/link from Lambda deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly committed Jun 14, 2016
1 parent 9c96b8c commit 2f42b6d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
17 changes: 9 additions & 8 deletions AWS/ApiGateway.hs
@@ -1,17 +1,18 @@
{-# LANGUAGE ScopedTypeVariables #-}
module AWS.ApiGateway where

import Control.Monad(when)
import Data.Functor(void)
import Control.Monad.Catch
import Control.Lens
import Control.Monad (when)
import Control.Monad.Catch
import Control.Monad.Trans.AWS
import Control.Monad.Trans.Resource
import Data.Functor (void)
import Data.Maybe
import Data.Text (Text)
import Network.AWS.APIGateway
import Control.Monad.Trans.Resource
import Data.Text(Text)
import Data.Maybe

createApi :: (MonadResource m, MonadCatch m) => Text -> AWST m Method
createApi api = do
createApi :: (MonadResource m, MonadCatch m) => Text -> Text -> AWST m Method
createApi api _ = do
restApi <- send (createRestAPI api)
let Just apiId = restApi ^. raId
resources :: [Resource] <- view grrsItems <$> send (getResources apiId)
Expand Down
69 changes: 46 additions & 23 deletions System/Config.hs
Expand Up @@ -4,42 +4,65 @@ import Data.Text
import Options.Applicative

data MainConfig = DeleteApi { deleteApiEndpoint :: Text}
| CreateApi { createApiEndpoint :: Text
, lambdaSrcDirectory :: Text
, lambdaTargetName :: Text
| CreateApi { createApiEndpoint :: Text
, lambdaTargetName :: Text
}
| DeployLambda { lambdaTargetName :: Text
, lambdaSrcDirectory :: Text
}
deriving (Eq,Show,Read)

mainConfig :: Parser MainConfig
mainConfig = subparser
( command "create" (info createConfig
(progDesc "create an AWS Lambda Function and ties it to an API Gateway"))
<> command "delete" (info deleteConfig
(progDesc "deleta an AWS Lambda function and associated API Gateway endpoint"))
( command "api" (info apiConfig
(progDesc "Manipulate AWS API Gateway Endpoints and their relations with Lambda functions"))
<> command "lambda" (info lambdaConfig
(progDesc "Manipulate AWS Lambda functions"))
)

createConfig :: Parser MainConfig
createConfig = CreateApi
apiConfig :: Parser MainConfig
apiConfig = subparser (
command "create" (info createApiConfig
(progDesc "Create an AWS API Gateway endpoint and ties it to AWS Lambda function"))
<> command "delete" (info deleteApiConfig
(progDesc "Delete an API Gateway endpoint"))
)


createApiConfig :: Parser MainConfig
createApiConfig = CreateApi
<$> (pack <$> strOption ( long "endpoint"
<> short 'e'
<> help "Endpoint identifier, a simple string"))
<*> (pack <$> strOption ( long "source-directory"
<> short 'd'
<> value "."
<> metavar "FILE"
<> help "Source directory of lambda function to deploy"))
<> short 'e'
<> help "Endpoint identifier, a simple string"))
<*> (pack <$> strOption ( long "build-target"
<> short 't'
<> metavar "STRING"
<> help "Target of executable to build and deploy"))

<> short 't'
<> metavar "STRING"
<> help "Target of executable to build and deploy"))

deleteConfig :: Parser MainConfig
deleteConfig = DeleteApi
deleteApiConfig :: Parser MainConfig
deleteApiConfig = DeleteApi
<$> (pack <$> strOption ( long "endpoint"
<> short 'd'
<> short 'e'
<> help "Endpoint identifier, a simple string. If multiple identifiers match, the first one will be deleted."))

lambdaConfig :: Parser MainConfig
lambdaConfig = subparser (
command "deploy" (info deployLambdaConfig
(progDesc "Deploy an AWS Lambda function"))
)

deployLambdaConfig :: Parser MainConfig
deployLambdaConfig = DeployLambda
<$> (pack <$> strOption ( long "build-target"
<> short 't'
<> metavar "STRING"
<> help "Target of executable to build and deploy"))
<*> (pack <$> strOption ( long "source-directory"
<> short 'd'
<> value "."
<> metavar "FILE"
<> help "Source directory of lambda function to deploy"))

options :: IO MainConfig
options = execParser opts
where
Expand Down
4 changes: 2 additions & 2 deletions main.hs
Expand Up @@ -30,8 +30,9 @@ initAWS = do
return awsEnv

go :: MainConfig -> IO ()
go CreateApi{..} = initAWS >>= \ awsEnv -> runResourceT (runAWST awsEnv $ createApi createApiEndpoint lambdaTargetName) >>= print
go DeleteApi{..} = initAWS >>= \ awsEnv -> runResourceT (runAWST awsEnv $ deleteApi deleteApiEndpoint)
go CreateApi{..} = do
go DeployLambda{..} = do
-- build docker container
buildDocker
-- build executable with docker
Expand All @@ -41,7 +42,6 @@ go CreateApi{..} = do
-- pack executable with js shim in .zip file
packLambda exe (exe:libs)
awsEnv <- initAWS
runResourceT (runAWST awsEnv $ createApi createApiEndpoint) >>= print
createOrUpdateFunction awsEnv lambdaTargetName "lambda.zip" >>= print
where
createOrUpdateFunction awsEnv target zipFile = runResourceT (runAWST awsEnv $ createFunctionWithZip target zipFile)
Expand Down

0 comments on commit 2f42b6d

Please sign in to comment.