/*** * Get list of {@link AutoScalingGroup}s for a given tag * * @param tag Tag to filter the auto scaling groups * @return List of {@link AutoScalingGroup}s qualifying the filter tag */ public List<AutoScalingGroup> getAutoScalingGroupsWithTag(Tag tag) { final AmazonAutoScaling autoScaling = getAmazonAutoScalingClient(); final DescribeAutoScalingGroupsRequest describeAutoScalingGroupsRequest = new DescribeAutoScalingGroupsRequest(); final List<AutoScalingGroup> allAutoScalingGroups = autoScaling .describeAutoScalingGroups(describeAutoScalingGroupsRequest) .getAutoScalingGroups(); final List<AutoScalingGroup> filteredAutoScalingGroups = Lists.newArrayList(); for (AutoScalingGroup autoScalingGroup : allAutoScalingGroups) { for (TagDescription tagDescription : autoScalingGroup.getTags()) { if (tagDescription.getKey().equalsIgnoreCase(tag.getKey()) && tagDescription.getValue().equalsIgnoreCase(tag.getValue())) { filteredAutoScalingGroups.add(autoScalingGroup); } } } return filteredAutoScalingGroups; }
/** * Actual membership AWS source of truth... */ @Override public int getRacMembershipSize() { AmazonAutoScaling client = null; try { client = getAutoScalingClient(); DescribeAutoScalingGroupsRequest asgReq = new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(config.getASGName()); DescribeAutoScalingGroupsResult res = client.describeAutoScalingGroups(asgReq); int size = 0; for (AutoScalingGroup asg : res.getAutoScalingGroups()) { size += asg.getMaxSize(); } logger.info(String.format("Query on ASG returning %d instances", size)); return size; } finally { if (client != null) { client.shutdown(); } } }
@Override public void expandRacMembership(int count) { AmazonAutoScaling client = null; try { client = getAutoScalingClient(); DescribeAutoScalingGroupsRequest asgReq = new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(config.getASGName()); DescribeAutoScalingGroupsResult res = client.describeAutoScalingGroups(asgReq); AutoScalingGroup asg = res.getAutoScalingGroups().get(0); UpdateAutoScalingGroupRequest ureq = new UpdateAutoScalingGroupRequest(); ureq.setAutoScalingGroupName(asg.getAutoScalingGroupName()); ureq.setMinSize(asg.getMinSize() + 1); ureq.setMaxSize(asg.getMinSize() + 1); ureq.setDesiredCapacity(asg.getMinSize() + 1); client.updateAutoScalingGroup(ureq); } finally { if (client != null) { client.shutdown(); } } }
@Bean public AmazonAutoScaling amazonAutoScalingClient(final AWSCredentialsProvider awsCredentialsProvider, final ClientConfiguration awsClientConfig, final Region awsRegion) { return AmazonAutoScalingClientBuilder.standard() .withCredentials(awsCredentialsProvider) .withClientConfiguration(awsClientConfig) .withRegion(awsRegion.getName()) .build(); }
/** * Binds all the Amazon services used. */ @Override protected void configure() { final Region region = Region.getRegion(Regions.fromName(regionName)); bind(AmazonEC2.class).toInstance(createAmazonClientInstance(AmazonEC2Client.class, region)); bind(AmazonCloudFormation.class).toInstance(createAmazonClientInstance(AmazonCloudFormationClient.class, region)); bind(AmazonIdentityManagement.class).toInstance(createAmazonClientInstance(AmazonIdentityManagementClient.class, region)); bind(AWSKMS.class).toInstance(createAmazonClientInstance(AWSKMSClient.class, region)); bind(AmazonS3.class).toInstance(createAmazonClientInstance(AmazonS3Client.class, region)); bind(AmazonAutoScaling.class).toInstance(createAmazonClientInstance(AmazonAutoScalingClient.class, region)); bind(AWSSecurityTokenService.class).toInstance(createAmazonClientInstance(AWSSecurityTokenServiceClient.class, region)); bind(AWSLambda.class).toInstance(createAmazonClientInstance(AWSLambdaClient.class, region)); bind(AmazonSNS.class).toInstance(createAmazonClientInstance(AmazonSNSClient.class, region)); }
@Before public void setup() { autoScalingClient = mock(AmazonAutoScaling.class); ec2Client = mock(AmazonEC2.class); autoScalingService = new AutoScalingService(autoScalingClient, ec2Client); }
/*** * Initialize the AWS SDK Client * * @param awsClusterSecurityManager The {@link AWSClusterSecurityManager} to fetch AWS credentials * @param region The Amazon AWS {@link Region} */ public AWSSdkClient(final AWSClusterSecurityManager awsClusterSecurityManager, final Region region) { this.amazonEC2Supplier = Suppliers.memoize(new Supplier<AmazonEC2>() { @Override public AmazonEC2 get() { AmazonEC2Client amazonEC2 = new AmazonEC2Client(awsClusterSecurityManager.getCredentialsProvider()); amazonEC2.setRegion(region); return amazonEC2; } }); this.amazonS3Supplier = Suppliers.memoize(new Supplier<AmazonS3>() { @Override public AmazonS3 get() { AmazonS3Client amazonS3 = new AmazonS3Client(awsClusterSecurityManager.getCredentialsProvider()); amazonS3.setRegion(region); return amazonS3; } }); this.amazonAutoScalingSupplier = Suppliers.memoize(new Supplier<AmazonAutoScaling>() { @Override public AmazonAutoScaling get() { AmazonAutoScalingClient amazonAutoScaling = new AmazonAutoScalingClient(awsClusterSecurityManager.getCredentialsProvider()); amazonAutoScaling.setRegion(region); return amazonAutoScaling; } }); }
/*** * Delete an auto scaling group by its name * * @param autoScalingGroupName Name of auto scaling group to delete * @param shouldForceDelete If the AutoScalingGroup should be deleted without waiting for instances to terminate */ public void deleteAutoScalingGroup(String autoScalingGroupName, boolean shouldForceDelete) { final AmazonAutoScaling autoScaling = getAmazonAutoScalingClient(); final DeleteAutoScalingGroupRequest deleteLaunchConfigurationRequest = new DeleteAutoScalingGroupRequest() .withAutoScalingGroupName(autoScalingGroupName) .withForceDelete(shouldForceDelete); autoScaling.deleteAutoScalingGroup(deleteLaunchConfigurationRequest); LOGGER.info("Deleted AutoScalingGroup: " + autoScalingGroupName); }
@Override public Map<String, List<String>> getRacMembership(Collection<String> autoScalingGroupNames) { if (CollectionUtils.isEmpty(autoScalingGroupNames)) { return Collections.emptyMap(); } AmazonAutoScaling client = null; try { client = getAutoScalingClient(); DescribeAutoScalingGroupsRequest describeAutoScalingGroupsRequest = new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(autoScalingGroupNames); DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = client.describeAutoScalingGroups(describeAutoScalingGroupsRequest); Map<String, List<String>> asgs = new HashMap<>(); for (AutoScalingGroup autoScalingGroup : describeAutoScalingGroupsResult.getAutoScalingGroups()) { List<String> asgInstanceIds = Lists.newArrayList(); for (Instance asgInstance : autoScalingGroup.getInstances()) { if (!(asgInstance.getLifecycleState().equalsIgnoreCase("terminating") || asgInstance.getLifecycleState().equalsIgnoreCase("shutting-down") || asgInstance.getLifecycleState().equalsIgnoreCase("terminated"))) { asgInstanceIds.add(asgInstance.getInstanceId()); } } asgs.put(autoScalingGroup.getAutoScalingGroupName(), asgInstanceIds); logger.info("AWS returned the following instance ID's for {} ASG: {}", autoScalingGroup.getAutoScalingGroupName(), StringUtils.join(asgInstanceIds, ",")); } return asgs; } finally { if (client != null) { client.shutdown(); } } }
private AmazonAutoScaling findClient(Account account, Region region) { // TODO: need to config client config parameter. ignore it for now. // TODO: need a cached version based on account and region as key AWSCredentials credential = new BasicAWSCredentials(account.getAccessKeyId(), account.getSecretAccessKey()); AmazonAutoScaling as = new AmazonAutoScalingClient(credential); // as.setEndpoint(region.toAsEndpoint()); return as; }
@Override public List<AbstractResource<?>> describeLaunchConfigurations(Account account, Region region, DateTime dt) { AmazonAutoScaling as = findClient(account, region); DescribeLaunchConfigurationsRequest req = new DescribeLaunchConfigurationsRequest(); log.debug("start describing instances for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeLaunchConfigurationsResult res = as.describeLaunchConfigurations(req); return converter.toAsLaunchConfigurations(res.getLaunchConfigurations(), account.getId(), region, dt); }
@Override public List<AbstractResource<?>> describeAutoScalingGroups(Account account, Region region, DateTime dt) { AmazonAutoScaling as = findClient(account, region); DescribeAutoScalingGroupsRequest req = new DescribeAutoScalingGroupsRequest(); log.debug("start describing auto scaling groups for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeAutoScalingGroupsResult res = as.describeAutoScalingGroups(req); return converter.toAsGroups(res.getAutoScalingGroups(), account.getId(), region, dt); }
@Inject public AutoScalingService(final AmazonAutoScaling autoScalingClient, final AmazonEC2 ec2Client) { this.autoScalingClient = autoScalingClient; this.ec2Client = ec2Client; }
@Bean public AmazonAutoScaling autoScaling() { AmazonAutoScaling client = new AmazonAutoScalingClient(); client.setRegion(region); return client; }
public static void main( String[] args ) throws IOException { final String env = "prod"; AWSCredentials credentials = new PropertiesCredentials( ShutdownWorkers.class.getResourceAsStream( "/AwsCredentials.properties" ) ); try { // Scalo a zero int min = 0; int desired = 0; int max = 0; AmazonAutoScaling as = new AmazonAutoScalingClient(credentials); as.setEndpoint("autoscaling.eu-west-1.amazonaws.com"); DescribeAutoScalingGroupsResult dasgres = as.describeAutoScalingGroups(); List<AutoScalingGroup> lasg = dasgres.getAutoScalingGroups(); for (AutoScalingGroup asg : lasg) { if(asg.getAutoScalingGroupName().equals("asg-kopjra-prod-workers")){ desired = asg.getDesiredCapacity(); max = asg.getMaxSize(); min = asg.getMinSize(); break; } } UpdateAutoScalingGroupRequest uasgr = new UpdateAutoScalingGroupRequest(); uasgr.setAutoScalingGroupName("asg-kopjra-prod-workers"); uasgr.setDesiredCapacity(0); uasgr.setMaxSize(0); uasgr.setMinSize(0); as.updateAutoScalingGroup(uasgr); System.out.println("Done: workers shutting down!"); System.out.println("When you run the StartupWorkers, remember to use the following parameters: "+min+" "+max+" "+desired); } catch (AmazonServiceException ase) { System.err.println( "AmazonServiceException" ); } catch (AmazonClientException ace) { System.err.println( "AmazonClientException" ); } catch (Exception e){ System.err.println( "OtherException" ); } }
/*** * Create a launch configuration that can be later used to create {@link AmazonAutoScaling} groups * * @param launchConfigName Desired launch config name * @param imageId AMI image id to use * @param instanceType EC2 instance type to use * @param keyName Key name * @param securityGroups Security groups to apply * @param kernelId Optional kernel id * @param ramdiskId Optional ram disk id * @param blockDeviceMapping Optional EBS device mapping * @param iamInstanceProfile Optional IAM instance profile * @param instanceMonitoring Optional instance monitoring * @param userData User data (eg. shell script to execute at instance boot under this launch config) */ public void createLaunchConfig(String launchConfigName, String imageId, String instanceType, String keyName, String securityGroups, Optional<String> kernelId, Optional<String> ramdiskId, Optional<BlockDeviceMapping> blockDeviceMapping, Optional<String> iamInstanceProfile, Optional<InstanceMonitoring> instanceMonitoring, String userData) { final AmazonAutoScaling autoScaling = getAmazonAutoScalingClient(); CreateLaunchConfigurationRequest createLaunchConfigurationRequest = new CreateLaunchConfigurationRequest() .withLaunchConfigurationName(launchConfigName) .withImageId(imageId) .withInstanceType(instanceType) .withSecurityGroups(SPLITTER.splitToList(securityGroups)) .withKeyName(keyName) .withUserData(userData); if (kernelId.isPresent()) { createLaunchConfigurationRequest = createLaunchConfigurationRequest .withKernelId(kernelId.get()); } if (ramdiskId.isPresent()) { createLaunchConfigurationRequest = createLaunchConfigurationRequest .withRamdiskId(ramdiskId.get()); } if (blockDeviceMapping.isPresent()) { createLaunchConfigurationRequest = createLaunchConfigurationRequest .withBlockDeviceMappings(blockDeviceMapping.get()); } if (iamInstanceProfile.isPresent()) { createLaunchConfigurationRequest = createLaunchConfigurationRequest .withIamInstanceProfile(iamInstanceProfile.get()); } if (instanceMonitoring.isPresent()) { createLaunchConfigurationRequest = createLaunchConfigurationRequest .withInstanceMonitoring(instanceMonitoring.get()); } autoScaling.createLaunchConfiguration(createLaunchConfigurationRequest); LOGGER.info("Created Launch Configuration: " + launchConfigName); }
/*** * 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); }
protected AmazonAutoScaling getAutoScalingClient() { AmazonAutoScaling client = new AmazonAutoScalingClient(provider.getAwsCredentialProvider()); client.setEndpoint("autoscaling." + config.getDC() + ".amazonaws.com"); return client; }
/*** * Delete a launch configuration by its name * * @param launchConfigName Name of launch config to delete */ public void deleteLaunchConfiguration(String launchConfigName) { final AmazonAutoScaling autoScaling = getAmazonAutoScalingClient(); final DeleteLaunchConfigurationRequest deleteLaunchConfigurationRequest = new DeleteLaunchConfigurationRequest() .withLaunchConfigurationName(launchConfigName); autoScaling.deleteLaunchConfiguration(deleteLaunchConfigurationRequest); LOGGER.info("Deleted Launch Configuration: " + launchConfigName); }
/*** * Creates a new Amazon AutoScaling client to invoke service methods on Amazon AutoScaling * * @return Amazon AutoScaling client to invoke service methods on Amazon AutoScaling */ public AmazonAutoScaling getAmazonAutoScalingClient() { return amazonAutoScalingSupplier.get(); }
/** * Returns the {@link AmazonAutoScaling} client through which API operations * can be invoked. * * @return */ public AmazonAutoScaling getApi() { return this.api; }