Advertisement
NLinker

Haskell amazonka example

Apr 7th, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---------------------------------
  2. describe-instances-transformer.hs
  3. ---------------------------------
  4. {-# LANGUAGE OverloadedStrings #-}
  5.  
  6. module Main where
  7.  
  8. import Control.Lens            -- lens
  9. import Control.Monad.IO.Class  -- transformers
  10. import Control.Monad.Trans.AWS -- amazonka
  11. import Network.AWS.EC2         -- amazonka-ec2
  12.  
  13. main :: IO ()
  14. main = do
  15.     env <- getEnv NorthVirginia Discover
  16.     r   <- runAWST env $ do
  17.         x <- send describeInstances
  18.         -- :type x
  19.         -- DescribeInstancesResponse
  20.         liftIO (print x)
  21.      
  22.         -- any send, paginate, or non-catch variants which fail will cause
  23.         -- the internal ExceptT to short circuit
  24.         -- and return the error contain in the Left branch of r, below.
  25.         y <- send deleteVolume "invalid-id"
  26.      
  27.         -- it's important to note *-Catch variants are not total,
  28.         -- they 'catch' service specific error responses.
  29.         z <- sendCatch (describeTags & dtFilters .~ [filter' "key" & fValues .~ ["Role"]]) --'
  30.        
  31.         -- :type z
  32.         -- Either EC2Error DescribeTagsResponse
  33.         liftIO (print z)
  34.        
  35.         -- :type y
  36.         -- DeleteVolumeResponse
  37.         return y
  38.  
  39.     -- :type r
  40.     -- Either Error DeleteVolumeResponse
  41.     print r -- this will be Left (ServiceError ...) due to the deleteVolume with an invalid-id above.
  42.  
  43. ---------------------
  44. describe-instances.hs
  45. ---------------------
  46.  
  47. {-# LANGUAGE OverloadedStrings #-}
  48.  
  49. module Main where
  50.  
  51. import Network.AWS     -- amazonka
  52. import Network.AWS.EC2 -- amazonka-ec2
  53.  
  54. main :: IO ()
  55. main = do
  56.     env <- getEnv Ireland (FromKeys "an-access-key" "a-secret-key")
  57.     rs  <- send env describeInstances
  58.     print rs
  59.  
  60. ------------------
  61. launch-instance.hs
  62. ------------------
  63. {-# LANGUAGE OverloadedStrings #-}
  64.  
  65. module Main where
  66.  
  67. import            Control.Applicative
  68. import            Control.Lens
  69. import            Control.Monad
  70. import            Control.Monad.IO.Class
  71. import            Control.Monad.Trans.AWS
  72. import            Data.Monoid
  73. import            Data.Text                (Text)
  74. import qualified Data.Text                as Text
  75. import qualified Data.Text.IO             as Text
  76. import            Data.Time.Clock.POSIX
  77. import            Network.AWS.EC2
  78. import            System.IO
  79.  
  80. main :: IO ()
  81. main = do
  82.     hSetBuffering stdout LineBuffering
  83.  
  84.     ts  <- Text.pack . show <$> getTimestamp
  85.     env <- getEnv NorthVirginia Discover
  86.     r   <- runAWST env $ do
  87.         say "Create KeyPair " ts
  88.         k <- send (createKeyPair ts)
  89.  
  90.         let key    = Text.unpack ts ++ ".pem"
  91.             trusty = "ami-5895242f"
  92.  
  93.         say "Writing KeyPair material to " key
  94.         liftIO (Text.writeFile key (k ^. ckprKeyMaterial))
  95.  
  96.         say "Create SecurityGroup " ts
  97.         g <- view csgrGroupId <$>
  98.             send (createSecurityGroup ts "amazonka-examples")
  99.  
  100.         say "Authorizing SSH on SecurityGroup " g
  101.         void . send $ authorizeSecurityGroupIngress
  102.             & asgiGroupId    ?~ g
  103.             & asgiIpProtocol ?~ "tcp"
  104.             & asgiFromPort   ?~ 22
  105.             & asgiToPort     ?~ 22
  106.             & asgiCidrIp     ?~ "0.0.0.0/22"
  107.  
  108.         say "Launching Instance with ImageId " trusty
  109.         i <- sendCatch $ runInstances trusty 1 1
  110.             & riKeyName          ?~ ts
  111.             & riInstanceType     ?~ T2Micro
  112.             & riSecurityGroupIds .~ [g]
  113.  
  114.         either (\e -> do
  115.                    say "Failed to Launch Instance " e
  116.                    say "Deleting SecurityGroup " g
  117.                    void . send $ deleteSecurityGroup & dsgGroupId ?~ g
  118.                    say "Deleting KeyPair " ts
  119.                    void . send $ deleteKeyPair ts
  120.                    throwAWSError e)
  121.                return
  122.                i
  123.  
  124.     print r
  125.  
  126. getTimestamp :: IO Integer
  127. getTimestamp = truncate <$> getPOSIXTime
  128.  
  129. say :: Show a => Text -> a -> AWS ()
  130. say msg = liftIO . Text.putStrLn . mappend msg . Text.pack . show
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement