Last active
August 4, 2016 20:11
-
-
Save brendanhay/a839f4829ca4e6780956 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Control.Lens -- lens | |
import Control.Monad.IO.Class -- transformers | |
import Control.Monad.Trans.AWS -- amazonka | |
import Network.AWS.EC2 -- amazonka-ec2 | |
main :: IO () | |
main = do | |
env <- getEnv NorthVirginia Discover | |
r <- runAWST env $ do | |
x <- send describeInstances | |
-- :type x | |
-- DescribeInstancesResponse | |
liftIO (print x) | |
-- any send, paginate, or non-catch variants which fail will cause | |
-- the internal ExceptT to short circuit | |
-- and return the error contain in the Left branch of r, below. | |
y <- send deleteVolume "invalid-id" | |
-- it's important to note *-Catch variants are not total, | |
-- they 'catch' service specific error responses. | |
z <- sendCatch (describeTags & dtFilters .~ [filter' "key" & fValues .~ ["Role"]]) | |
-- :type z | |
-- Either EC2Error DescribeTagsResponse | |
liftIO (print z) | |
-- :type y | |
-- DeleteVolumeResponse | |
return y | |
-- :type r | |
-- Either Error DeleteVolumeResponse | |
print r -- this will be Left (ServiceError ...) due to the deleteVolume with an invalid-id above. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Network.AWS -- amazonka | |
import Network.AWS.EC2 -- amazonka-ec2 | |
main :: IO () | |
main = do | |
env <- getEnv Ireland (FromKeys "an-access-key" "a-secret-key") | |
rs <- send env describeInstances | |
print rs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Control.Applicative | |
import Control.Lens | |
import Control.Monad | |
import Control.Monad.IO.Class | |
import Control.Monad.Trans.AWS | |
import Data.Monoid | |
import Data.Text (Text) | |
import qualified Data.Text as Text | |
import qualified Data.Text.IO as Text | |
import Data.Time.Clock.POSIX | |
import Network.AWS.EC2 | |
import System.IO | |
main :: IO () | |
main = do | |
hSetBuffering stdout LineBuffering | |
ts <- Text.pack . show <$> getTimestamp | |
env <- getEnv NorthVirginia Discover | |
r <- runAWST env $ do | |
say "Create KeyPair " ts | |
k <- send (createKeyPair ts) | |
let key = Text.unpack ts ++ ".pem" | |
trusty = "ami-5895242f" | |
say "Writing KeyPair material to " key | |
liftIO (Text.writeFile key (k ^. ckprKeyMaterial)) | |
say "Create SecurityGroup " ts | |
g <- view csgrGroupId <$> | |
send (createSecurityGroup ts "amazonka-examples") | |
say "Authorizing SSH on SecurityGroup " g | |
void . send $ authorizeSecurityGroupIngress | |
& asgiGroupId ?~ g | |
& asgiIpProtocol ?~ "tcp" | |
& asgiFromPort ?~ 22 | |
& asgiToPort ?~ 22 | |
& asgiCidrIp ?~ "0.0.0.0/22" | |
say "Launching Instance with ImageId " trusty | |
i <- sendCatch $ runInstances trusty 1 1 | |
& riKeyName ?~ ts | |
& riInstanceType ?~ T2Micro | |
& riSecurityGroupIds .~ [g] | |
either (\e -> do | |
say "Failed to Launch Instance " e | |
say "Deleting SecurityGroup " g | |
void . send $ deleteSecurityGroup & dsgGroupId ?~ g | |
say "Deleting KeyPair " ts | |
void . send $ deleteKeyPair ts | |
throwAWSError e) | |
return | |
i | |
print r | |
getTimestamp :: IO Integer | |
getTimestamp = truncate <$> getPOSIXTime | |
say :: Show a => Text -> a -> AWS () | |
say msg = liftIO . Text.putStrLn . mappend msg . Text.pack . show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment