Пример #1
0
  /**
   * Describe a set of specific auto-scaling instances.
   *
   * @param instanceIds the instance ids
   * @return the instances
   */
  public List<AutoScalingInstanceDetails> describeAutoScalingInstances(String... instanceIds) {
    if (instanceIds == null || instanceIds.length == 0) {
      LOGGER.info("Getting all auto-scaling instances.");
    } else {
      LOGGER.info(String.format("Getting auto-scaling instances for %d ids.", instanceIds.length));
    }

    List<AutoScalingInstanceDetails> instances = new LinkedList<AutoScalingInstanceDetails>();

    AmazonAutoScalingClient asgClient = asgClient();
    DescribeAutoScalingInstancesRequest request = new DescribeAutoScalingInstancesRequest();
    if (instanceIds != null) {
      request.setInstanceIds(Arrays.asList(instanceIds));
    }
    DescribeAutoScalingInstancesResult result = asgClient.describeAutoScalingInstances(request);

    instances.addAll(result.getAutoScalingInstances());
    while (result.getNextToken() != null) {
      request = request.withNextToken(result.getNextToken());
      result = asgClient.describeAutoScalingInstances(request);
      instances.addAll(result.getAutoScalingInstances());
    }

    LOGGER.info(String.format("Got %d auto-scaling instances.", instances.size()));
    return instances;
  }
Пример #2
0
  /**
   * Describe a set of specific auto scaling groups.
   *
   * @param names the ASG names
   * @return the auto scaling groups
   */
  public List<AutoScalingGroup> describeAutoScalingGroups(String... names) {
    if (names == null || names.length == 0) {
      LOGGER.info("Getting all auto-scaling groups.");
    } else {
      LOGGER.info(String.format("Getting auto-scaling groups for %d names.", names.length));
    }

    List<AutoScalingGroup> asgs = new LinkedList<AutoScalingGroup>();

    AmazonAutoScalingClient asgClient = asgClient();
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
    if (names != null) {
      request.setAutoScalingGroupNames(Arrays.asList(names));
    }
    DescribeAutoScalingGroupsResult result = asgClient.describeAutoScalingGroups(request);

    asgs.addAll(result.getAutoScalingGroups());
    while (result.getNextToken() != null) {
      request.setNextToken(result.getNextToken());
      result = asgClient.describeAutoScalingGroups(request);
      asgs.addAll(result.getAutoScalingGroups());
    }

    LOGGER.info(String.format("Got %d auto-scaling groups.", asgs.size()));
    return asgs;
  }
  public static void deletePolicies() {

    DeletePolicyRequest request = new DeletePolicyRequest();
    request.setAutoScalingGroupName("ProjectTwoAutoScaleGroup");
    request.setPolicyName("ScaleInInstances");
    scaleClient.deletePolicy(request);
    request.setPolicyName("ScaleOutInstances");
    scaleClient.deletePolicy(request);
  }
Пример #4
0
 /** {@inheritDoc} */
 @Override
 public void deleteAutoScalingGroup(String asgName) {
   Validate.notEmpty(asgName);
   LOGGER.info(String.format("Deleting auto-scaling group with name %s.", asgName));
   AmazonAutoScalingClient asgClient = asgClient();
   DeleteAutoScalingGroupRequest request =
       new DeleteAutoScalingGroupRequest().withAutoScalingGroupName(asgName);
   asgClient.deleteAutoScalingGroup(request);
 }
Пример #5
0
  private static void setupPolicy(
      AmazonAutoScalingClient autoScale, AmazonCloudWatchClient cloudWatch) {

    PutScalingPolicyRequest policyReq = new PutScalingPolicyRequest();
    policyReq.setPolicyName("On Demand Scale Up Policy");
    policyReq.setAutoScalingGroupName("OnDemand ASGroup");
    policyReq.setAdjustmentType("ChangeInCapacity");
    policyReq.setCooldown(60);
    policyReq.setScalingAdjustment(1);
    PutScalingPolicyResult arn_up = autoScale.putScalingPolicy(policyReq);

    PutMetricAlarmRequest putMetricAlarmRequest = new PutMetricAlarmRequest();
    putMetricAlarmRequest.setMetricName("HighCPUAlarm");
    putMetricAlarmRequest.setComparisonOperator("GreaterThanOrEqualToThreshold");
    putMetricAlarmRequest.setEvaluationPeriods(1);
    putMetricAlarmRequest.setMetricName("CPUUtilization");
    putMetricAlarmRequest.setNamespace("AWS/EC2");
    putMetricAlarmRequest.setPeriod(120);
    putMetricAlarmRequest.setStatistic("Average");
    putMetricAlarmRequest.setThreshold(50.0);
    List<String> arnList = new ArrayList<String>();
    arnList.add(arn_up.getPolicyARN());
    putMetricAlarmRequest.setAlarmActions(arnList);
    putMetricAlarmRequest.setAlarmName("On Demand Alarm Up");

    cloudWatch.putMetricAlarm(putMetricAlarmRequest);

    policyReq = new PutScalingPolicyRequest();
    policyReq.setPolicyName("On Demand Scale Down Policy");
    policyReq.setAutoScalingGroupName("OnDemand ASGroup");
    policyReq.setAdjustmentType("ChangeInCapacity");
    policyReq.setCooldown(60);
    policyReq.setScalingAdjustment(-1);
    PutScalingPolicyResult arn_down = autoScale.putScalingPolicy(policyReq);

    putMetricAlarmRequest = new PutMetricAlarmRequest();
    putMetricAlarmRequest.setMetricName("LowCPUAlarm");
    putMetricAlarmRequest.setComparisonOperator("LessThanOrEqualToThreshold");
    putMetricAlarmRequest.setEvaluationPeriods(1);
    putMetricAlarmRequest.setMetricName("CPUUtilization");
    putMetricAlarmRequest.setNamespace("AWS/EC2");
    putMetricAlarmRequest.setPeriod(120);
    putMetricAlarmRequest.setStatistic("Average");
    putMetricAlarmRequest.setThreshold(4.0);
    List<String> arnList2 = new ArrayList<String>();
    arnList2.add(arn_down.getPolicyARN());
    putMetricAlarmRequest.setAlarmActions(arnList2);
    putMetricAlarmRequest.setAlarmName("On Demand Alarm Down");

    cloudWatch.putMetricAlarm(putMetricAlarmRequest);

    System.out.println("Setup autoscaler to monitor average CPU Usage");
  }
  public static void deleteAutoScalingGroup() {

    DeleteAutoScalingGroupRequest request = new DeleteAutoScalingGroupRequest();
    request.setAutoScalingGroupName("ProjectTwoAutoScaleGroup");
    request.setForceDelete(true);
    scaleClient.deleteAutoScalingGroup(request);
  }
  public static void setUpPolicies() {

    // Create policy request
    PutScalingPolicyRequest policyRequest = new PutScalingPolicyRequest();
    policyRequest.setAutoScalingGroupName("ProjectTwoAutoScaleGroup");

    policyRequest.setPolicyName("ScaleOutInstances");
    policyRequest.setScalingAdjustment(1);
    policyRequest.setAdjustmentType("ChangeInCapacity");
    scaleOut = scaleClient.putScalingPolicy(policyRequest).getPolicyARN();

    policyRequest.setPolicyName("ScaleInInstances");
    policyRequest.setScalingAdjustment(-1);
    policyRequest.setAdjustmentType("ChangeInCapacity");
    scaleIn = scaleClient.putScalingPolicy(policyRequest).getPolicyARN();

    System.out.println("Polices Set successfully..");
  }
Пример #8
0
 public void createAutoScalingGroup(
     String name, String launchConfigurationName, String loadBalancerName) {
   CreateAutoScalingGroupRequest scalingGroupRequest = new CreateAutoScalingGroupRequest();
   scalingGroupRequest.setAutoScalingGroupName(name);
   scalingGroupRequest.setLaunchConfigurationName(launchConfigurationName);
   //		scalingGroupRequest.setLoadBalancerNames(Arrays
   //				.asList(loadBalancerName));
   scalingGroupRequest.setMaxSize(3);
   scalingGroupRequest.setMinSize(1);
   scalingGroupRequest.setAvailabilityZones(Arrays.asList("us-east-1a", "us-east-1b"));
   scalingClient.createAutoScalingGroup(scalingGroupRequest);
 }
Пример #9
0
 public void createLaunchConfiguration(
     String imageId, String name, String keyName, String securityGroup) {
   log.info("Creating new launch config ");
   CreateLaunchConfigurationRequest launchConfigurationRequest =
       new CreateLaunchConfigurationRequest();
   launchConfigurationRequest.setImageId(imageId);
   launchConfigurationRequest.setLaunchConfigurationName(name);
   launchConfigurationRequest.setKeyName(keyName);
   launchConfigurationRequest.setInstanceType("t1.micro");
   launchConfigurationRequest.setSecurityGroups(Arrays.asList(securityGroup));
   scalingClient.createLaunchConfiguration(launchConfigurationRequest);
   log.info("Launch Configuration created ");
 }
Пример #10
0
  private static void setupAutoScale(
      AmazonAutoScalingClient autoScale,
      AmazonCloudWatchClient cloudWatch,
      String keyName,
      String zone,
      String securityGroup,
      String imageId) {

    List<LaunchConfiguration> launchList =
        autoScale.describeLaunchConfigurations().getLaunchConfigurations();
    for (LaunchConfiguration ll : launchList)
      if (ll.getLaunchConfigurationName().equalsIgnoreCase("On DemandAWS")) {
        System.out.println("Using Launch Configuration " + ll.getLaunchConfigurationName());
        return;
      }

    CreateLaunchConfigurationRequest launchConfig = new CreateLaunchConfigurationRequest();
    launchConfig.setImageId(imageId);
    launchConfig.setKeyName(keyName);
    launchConfig.setInstanceType("t1.micro");
    List<String> securityGroups = new ArrayList<String>();
    securityGroups.add(securityGroup);
    launchConfig.setSecurityGroups(securityGroups);
    launchConfig.setLaunchConfigurationName("On DemandAWS");
    autoScale.createLaunchConfiguration(launchConfig);

    CreateAutoScalingGroupRequest autoReq = new CreateAutoScalingGroupRequest();
    autoReq.setLaunchConfigurationName("On DemandAWS");
    List<String> availabilityZones = new ArrayList<String>();
    availabilityZones.add(zone);
    autoReq.setAvailabilityZones(availabilityZones);
    autoReq.setMinSize(1);
    autoReq.setMaxSize(1);
    autoReq.setAutoScalingGroupName("OnDemand ASGroup");
    autoScale.createAutoScalingGroup(autoReq);

    System.out.println("Using Launch Configuration On DemandAWS created");
  }
Пример #11
0
  /**
   * Describe a set of specific launch configurations.
   *
   * @param names the launch configuration names
   * @return the launch configurations
   */
  public List<LaunchConfiguration> describeLaunchConfigurations(String... names) {
    if (names == null || names.length == 0) {
      LOGGER.info("Getting all launch configurations.");
    } else {
      LOGGER.info(String.format("Getting launch configurations for %d names.", names.length));
    }

    List<LaunchConfiguration> lcs = new LinkedList<LaunchConfiguration>();

    AmazonAutoScalingClient asgClient = asgClient();
    DescribeLaunchConfigurationsRequest request =
        new DescribeLaunchConfigurationsRequest().withLaunchConfigurationNames(names);
    DescribeLaunchConfigurationsResult result = asgClient.describeLaunchConfigurations(request);

    lcs.addAll(result.getLaunchConfigurations());
    while (result.getNextToken() != null) {
      request.setNextToken(result.getNextToken());
      result = asgClient.describeLaunchConfigurations(request);
      lcs.addAll(result.getLaunchConfigurations());
    }

    LOGGER.info(String.format("Got %d launch configurations.", lcs.size()));
    return lcs;
  }
Пример #12
0
 public String putScalingPolicy(
     String autoScalingGroupName,
     String policyName,
     int scalingAdjustment,
     String adjustmentType,
     int cooldown) {
   PutScalingPolicyRequest request = new PutScalingPolicyRequest();
   request.setAutoScalingGroupName(autoScalingGroupName);
   request.setAdjustmentType(adjustmentType);
   request.setCooldown(cooldown);
   request.setPolicyName(policyName);
   request.setScalingAdjustment(scalingAdjustment);
   PutScalingPolicyResult result = scalingClient.putScalingPolicy(request);
   return result.getPolicyARN();
 }
  public static void setUpAutoScaling() {

    System.out.println("Setting up autoscale group..");

    CreateAutoScalingGroupRequest scaleRequest = new CreateAutoScalingGroupRequest();

    scaleRequest.setAutoScalingGroupName("ProjectTwoAutoScaleGroup");
    scaleRequest.setLaunchConfigurationName("ProjectTwoLaunchConfiguration");

    // Start with one instance
    scaleRequest.setMinSize(1);
    scaleRequest.setMaxSize(10);

    // Same zone
    List<String> zones = new ArrayList<String>(1);
    zones.add("us-east-1c");
    scaleRequest.setAvailabilityZones(zones);

    // Choose ELB
    List<String> loadBalancerName = new ArrayList<String>();
    loadBalancerName.add("projecttwoelb");
    scaleRequest.setLoadBalancerNames(loadBalancerName);

    scaleRequest.setHealthCheckType("ELB");
    scaleRequest.setHealthCheckGracePeriod(100);
    scaleRequest.setDefaultCooldown(60);

    // Adding tags
    Collection<com.amazonaws.services.autoscaling.model.Tag> tags =
        new ArrayList<com.amazonaws.services.autoscaling.model.Tag>();

    com.amazonaws.services.autoscaling.model.Tag tag =
        new com.amazonaws.services.autoscaling.model.Tag();
    tag.setKey("Project");
    tag.setValue("2.1");
    tags.add(tag);

    scaleRequest.setTags(tags);

    scaleClient.createAutoScalingGroup(scaleRequest);

    System.out.println("Autoscale group set up successfully..");
    System.out.println("Setting up autoscale policies..");

    setUpPolicies();
  }
  public static void setUpLaunchConfiguration() {

    scaleClient = new AmazonAutoScalingClient(awsCredentials());

    CreateLaunchConfigurationRequest launchConfiguration = new CreateLaunchConfigurationRequest();
    launchConfiguration.setImageId("ami-349fbb5e");
    launchConfiguration.setInstanceType("m3.medium");
    launchConfiguration.setLaunchConfigurationName("ProjectTwoLaunchConfiguration");

    ArrayList<String> securityGroups = new ArrayList<String>();
    securityGroups.add("SecurityGroupForELBAndAutoscale");
    launchConfiguration.setSecurityGroups(securityGroups);

    launchConfiguration.setSpotPrice("0.03");

    scaleClient.createLaunchConfiguration(launchConfiguration);

    System.out.println("Launch configuration successful..");
  }
 public static void deleteLaunchConfiguration() {
   DeleteLaunchConfigurationRequest request = new DeleteLaunchConfigurationRequest();
   request.setLaunchConfigurationName("ProjectTwoLaunchConfiguration");
   scaleClient.deleteLaunchConfiguration(request);
 }
Пример #16
0
 /**
  * Amazon ASG client. Abstracted to aid testing.
  *
  * @return the Amazon Auto Scaling client
  */
 protected AmazonAutoScalingClient asgClient() {
   AmazonAutoScalingClient client = new AmazonAutoScalingClient(cred);
   client.setEndpoint("autoscaling." + region + ".amazonaws.com");
   return client;
 }