Last active
April 4, 2016 02:38
-
-
Save kdrakon/13fdba4edfb6470745b1597e64cfb3d5 to your computer and use it in GitHub Desktop.
Playing around with AWS SDK for ECS
This file contains 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
package aws.playground | |
import aws.playground.AwsPipelineClusterManager._ | |
import com.amazonaws.regions.RegionUtils | |
import com.amazonaws.services.autoscaling.AmazonAutoScalingClient | |
import com.amazonaws.services.autoscaling.model.{CreateAutoScalingGroupRequest, CreateLaunchConfigurationRequest, DeleteAutoScalingGroupRequest, DeleteLaunchConfigurationRequest} | |
import com.amazonaws.services.ecs.AmazonECSClient | |
import com.amazonaws.services.ecs.model.{CreateClusterRequest, CreateClusterResult, DeleteClusterRequest, DeleteClusterResult} | |
import scala.collection.JavaConverters._ | |
import scala.util.{Failure, Success, Try} | |
object AwsPipelineClusterManager { | |
case class ClusterIdentifier(clusterName: String, keyName: String) | |
case class Localisation(region: String, availabilityZone: String) | |
type SecurityGroups = List[String] | |
case class VolumeInfo(deviceName: String, volumeSize: Int, deviceType: String, deleteOnTermination: Boolean) | |
case class LaunchConfigurationParameters(instanceType: String, imageId: String, iamInstanceProfile: String, userData: String) | |
case class AutoScalingGroupParameters(scalingParameters: ScalingParameters, healthCheckType: String, vpcZoneIdentifier: String) | |
case class ScalingParameters(desiredCapacity: Int, minSize: Int, maxSize: Int) | |
} | |
class AwsPipelineClusterManager(implicit | |
clusterIdentifier: ClusterIdentifier, | |
localisation: Localisation, | |
securityGroups: SecurityGroups, | |
launchConfigurationParameters: LaunchConfigurationParameters, | |
autoScalingGroupParameters: AutoScalingGroupParameters) { | |
private lazy val launchConfigurationRequest = { | |
new CreateLaunchConfigurationRequest() | |
.withLaunchConfigurationName(clusterIdentifier.clusterName) | |
.withIamInstanceProfile(launchConfigurationParameters.iamInstanceProfile) | |
.withImageId(launchConfigurationParameters.imageId) | |
.withInstanceType(launchConfigurationParameters.instanceType) | |
.withKeyName(clusterIdentifier.keyName) | |
.withSecurityGroups(securityGroups.asJava) | |
.withUserData(launchConfigurationParameters.userData) | |
} | |
private lazy val autoScalingGroupRequest = { | |
new CreateAutoScalingGroupRequest() | |
.withAutoScalingGroupName(clusterIdentifier.clusterName) | |
.withLaunchConfigurationName(clusterIdentifier.clusterName) | |
.withAvailabilityZones(localisation.availabilityZone) | |
.withDesiredCapacity(autoScalingGroupParameters.scalingParameters.desiredCapacity) | |
.withMinSize(autoScalingGroupParameters.scalingParameters.minSize) | |
.withMaxSize(autoScalingGroupParameters.scalingParameters.maxSize) | |
.withDefaultCooldown(300) | |
.withHealthCheckType("EC2") | |
.withHealthCheckGracePeriod(300) | |
.withVPCZoneIdentifier(autoScalingGroupParameters.vpcZoneIdentifier) | |
} | |
private lazy val clientRegion = RegionUtils.getRegion(localisation.region) | |
private lazy val autoScalingClient = { | |
val autoScalingClient = new AmazonAutoScalingClient() | |
autoScalingClient.setRegion(clientRegion) | |
autoScalingClient | |
} | |
private lazy val ecsClient = { | |
val ecsClient = new AmazonECSClient() | |
ecsClient.setRegion(clientRegion) | |
ecsClient | |
} | |
def createCluster : Either[CreateClusterResult, Throwable] = { | |
Try { | |
autoScalingClient.createLaunchConfiguration(launchConfigurationRequest) | |
autoScalingClient.createAutoScalingGroup(autoScalingGroupRequest) | |
} match { | |
case Success(unit) => { | |
Left(ecsClient.createCluster(new CreateClusterRequest().withClusterName(clusterIdentifier.clusterName))) | |
} | |
case Failure(e) => Right(e) | |
} | |
} | |
def deleteCluster : Either[DeleteClusterResult, Throwable] = { | |
Try { | |
autoScalingClient.deleteAutoScalingGroup(new DeleteAutoScalingGroupRequest().withAutoScalingGroupName(clusterIdentifier.clusterName)) | |
autoScalingClient.deleteLaunchConfiguration(new DeleteLaunchConfigurationRequest().withLaunchConfigurationName(clusterIdentifier.clusterName)) | |
} match { | |
case Success(unit) => { | |
Left(ecsClient.deleteCluster(new DeleteClusterRequest().withCluster(clusterIdentifier.clusterName))) | |
} | |
case Failure(e) => Right(e) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment