public AutoScalingGroup createASGroup(CreateAutoScalingGroupRequest request) { logger.info("create auto scaling group, request={}", request); autoScaling.createAutoScalingGroup(request); DescribeAutoScalingGroupsResult result = autoScaling.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(request.getAutoScalingGroupName())); return result.getAutoScalingGroups().get(0); }
@Override public void execute(Context context) throws Exception { ASGroupHelper helper = new ASGroupHelper(context.env); helper.createLaunchConfig(resource); CreateAutoScalingGroupRequest request = new CreateAutoScalingGroupRequest() .withAutoScalingGroupName(context.env.name + "-" + resource.id) .withLaunchConfigurationName(resource.launchConfig.remoteLaunchConfig.getLaunchConfigurationName()) .withDesiredCapacity(resource.desiredSize) .withMinSize(resource.minSize) .withMaxSize(resource.maxSize) .withDefaultCooldown(60) .withHealthCheckGracePeriod(300) // give 5 mins for server and application startup .withTerminationPolicies(ASGroup.TERMINATE_POLICY_OLDEST_INSTANCE) // always remove oldest instance, OldestLaunchConfiguration should not be used due to during deployment the old LaunchConfig can be deleted first, the ASG may fail to compare, and terminate unwanted instance .withTags(new Tag().withKey("cloud-manager:env").withValue(context.env.name).withPropagateAtLaunch(true), helper.nameTag(resource)); if (resource.elb != null) { request.withHealthCheckType("ELB") .withLoadBalancerNames(resource.elb.remoteELB.getLoadBalancerName()); } else { request.withHealthCheckType("EC2"); } List<String> availabilityZones = Lists.newArrayList(); StringBuilder subnetIds = new StringBuilder(); int index = 0; for (Subnet remoteSubnet : resource.subnet.remoteSubnets) { if (index > 0) subnetIds.append(','); subnetIds.append(remoteSubnet.getSubnetId()); availabilityZones.add(remoteSubnet.getAvailabilityZone()); index++; } request.withAvailabilityZones(availabilityZones) .withVPCZoneIdentifier(subnetIds.toString()); resource.remoteASGroup = AWS.as.createASGroup(request); }
private CreateAutoScalingGroupRequest createAutoScalingRequest(Scaling scaling) { return new CreateAutoScalingGroupRequest().withAutoScalingGroupName(scaling.getGroup()) .withLaunchConfigurationName(scaling.getName()).withAvailabilityZones(scaling.getZone()) .withMaxSize(scaling.getMax()).withMinSize(scaling.getMin()) .withLoadBalancerNames(scaling.getLb()).withHealthCheckType("EC2") .withHealthCheckGracePeriod(300).withDefaultCooldown(600); }
/*** * Create and launch an {@link AmazonAutoScaling} group * * @param groupName Auto scaling group name * @param launchConfig Launch configuration string * @param minSize Minimum number of instances to maintain in auto scaling group * @param maxSize Maximum number of instances to scale up-to for load * @param desiredCapacity Desired number of instances to maintain in auto scaling group * @param availabilityZones Optional availability zones to make use of * @param cooldown Optional cooldown period before any scaling event (default is 300 secs) * @param healthCheckGracePeriod Optional grace period till which no health check is performed after bootup (default is 300 secs) * @param healthCheckType Optional health check type (default is EC2 instance check) * @param loadBalancer Optional load balancer to use * @param terminationPolicy Optional termination policies * @param tags Optional tags to set on auto scaling group (they are set to propagate to EC2 instances implicitly) */ public void createAutoScalingGroup(String groupName, String launchConfig, Integer minSize, Integer maxSize, Integer desiredCapacity, Optional<String> availabilityZones, Optional<Integer> cooldown, Optional<Integer> healthCheckGracePeriod, Optional<String> healthCheckType, Optional<String> loadBalancer, Optional<String> terminationPolicy, List<Tag> tags) { AmazonAutoScaling autoScaling = getAmazonAutoScalingClient(); // Propagate ASG tags to EC2 instances launched under the ASG by default // (we want to ensure this, hence not configurable) final List<Tag> tagsWithPropagationSet = Lists.newArrayList(); for (Tag tag : tags) { tagsWithPropagationSet.add(tag.withPropagateAtLaunch(true)); } CreateAutoScalingGroupRequest createAutoScalingGroupRequest = new CreateAutoScalingGroupRequest() .withAutoScalingGroupName(groupName) .withLaunchConfigurationName(launchConfig) .withMinSize(minSize) .withMaxSize(maxSize) .withDesiredCapacity(desiredCapacity) .withTags(tagsWithPropagationSet); if (availabilityZones.isPresent()) { createAutoScalingGroupRequest = createAutoScalingGroupRequest .withAvailabilityZones(SPLITTER.splitToList(availabilityZones.get())); } if (cooldown.isPresent()) { createAutoScalingGroupRequest = createAutoScalingGroupRequest .withDefaultCooldown(cooldown.get()); } if (healthCheckGracePeriod.isPresent()) { createAutoScalingGroupRequest = createAutoScalingGroupRequest .withHealthCheckGracePeriod(healthCheckGracePeriod.get()); } if (healthCheckType.isPresent()) { createAutoScalingGroupRequest = createAutoScalingGroupRequest .withHealthCheckType(healthCheckType.get()); } if (loadBalancer.isPresent()) { createAutoScalingGroupRequest = createAutoScalingGroupRequest .withLoadBalancerNames(SPLITTER.splitToList(loadBalancer.get())); } if (terminationPolicy.isPresent()) { createAutoScalingGroupRequest = createAutoScalingGroupRequest .withTerminationPolicies(SPLITTER.splitToList(terminationPolicy.get())); } autoScaling.createAutoScalingGroup(createAutoScalingGroupRequest); LOGGER.info("Created AutoScalingGroup: " + groupName); }