com.amazonaws.services.cloudformation.AmazonCloudFormationClient Java Examples

The following examples show how to use com.amazonaws.services.cloudformation.AmazonCloudFormationClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloudFormationManagerTest.java    From AWS-MIMIC-IIItoOMOP with Apache License 2.0 6 votes vote down vote up
/**
 * Test of terminateCluster method, of class EMRManager.
 */
@Test
public void testTerminateStack() throws IOException {
    AmazonCloudFormationClient client = new AmazonCloudFormationClient();
    String name = "JUnitStack" + UUID.randomUUID().toString();
    CreateStackRequest createStackRequest = new CreateStackRequest();
    CloudFormationManager manager = new CloudFormationManager(Region.getRegion(Regions.US_WEST_2));
    
    client.setRegion(Region.getRegion(Regions.US_WEST_2));
    
    createStackRequest.setStackName(name);
    createStackRequest.setTemplateBody(IOUtils.toString(getClass().getResourceAsStream("cloudformation.template"), "UTF-8"));
    
    client.createStack(createStackRequest);
    
    manager.terminateStack(name);
    
    for(StackSummary stack : client.listStacks().getStackSummaries())
    {
        if(stack.getStackStatus().equalsIgnoreCase("DELETE_COMPLETE")) continue;
        if(stack.getStackStatus().equalsIgnoreCase("DELETE_FAILED")) continue;
        if(stack.getStackStatus().equalsIgnoreCase("DELETE_IN_PROGRESS")) continue;
        
        if(stack.getStackName().equals(name)) fail(name +  " should have been deleted but status is: " + stack.getStackStatus());
    }
}
 
Example #2
Source File: KinesisConnectorExecutor.java    From amazon-kinesis-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to create Elasticsearch cluster at set correct endpoint.
 */
private void createElasticsearchCluster() {
    // Create stack if not already up
    AmazonCloudFormation cloudFormationClient = new AmazonCloudFormationClient(config.AWS_CREDENTIALS_PROVIDER);
    cloudFormationClient.setRegion(RegionUtils.getRegion(config.REGION_NAME));
    CloudFormationUtils.createStackIfNotExists(cloudFormationClient, config);

    // Update the elasticsearch endpoint to use endpoint in created cluster
    AmazonEC2 ec2Client = new AmazonEC2Client(config.AWS_CREDENTIALS_PROVIDER);
    ec2Client.setRegion(RegionUtils.getRegion(config.REGION_NAME));
    config.ELASTICSEARCH_ENDPOINT =
            EC2Utils.getEndpointForFirstActiveInstanceWithTag(ec2Client,
                    EC2_ELASTICSEARCH_FILTER_NAME,
                    EC2_ELASTICSEARCH_FILTER_VALUE);
    if (config.ELASTICSEARCH_ENDPOINT == null || config.ELASTICSEARCH_ENDPOINT.isEmpty()) {
        throw new RuntimeException("Could not find active Elasticsearch endpoint from cluster.");
    }
}
 
Example #3
Source File: AwsNetworkConnectorTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test(expected = CloudConnectorException.class)
public void testDeleteNetworkWithSubNetsShouldThrowAnExceptionWhenTheStackDeletionFailed()
        throws InterruptedException, ExecutionException, TimeoutException {
    NetworkDeletionRequest networkDeletionRequest = createNetworkDeletionRequest();
    AmazonCloudFormationRetryClient cloudFormationRetryClient = mock(AmazonCloudFormationRetryClient.class);
    AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
    when(awsClient.createCloudFormationRetryClient(any(AwsCredentialView.class), eq(networkDeletionRequest.getRegion())))
            .thenReturn(cloudFormationRetryClient);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient);
    when(cfClient.waiters()).thenReturn(cfWaiters);
    when(cfWaiters.stackDeleteComplete()).thenReturn(deletionWaiter);
    doThrow(new WaiterTimedOutException("fail")).when(deletionWaiter).run(any());

    underTest.deleteNetworkWithSubnets(networkDeletionRequest);

    verify(cloudFormationRetryClient).deleteStack(any(DeleteStackRequest.class));
    verify(awsClient).createCloudFormationRetryClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
}
 
Example #4
Source File: AwsNetworkConnectorTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteNetworkWithSubNetsShouldDeleteTheStackAndTheResourceGroup() {
    NetworkDeletionRequest networkDeletionRequest = createNetworkDeletionRequest();
    AmazonCloudFormationRetryClient cloudFormationRetryClient = mock(AmazonCloudFormationRetryClient.class);
    AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
    when(awsClient.createCloudFormationRetryClient(any(AwsCredentialView.class), eq(networkDeletionRequest.getRegion())))
            .thenReturn(cloudFormationRetryClient);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient);
    when(cfClient.waiters()).thenReturn(cfWaiters);
    when(cfWaiters.stackDeleteComplete()).thenReturn(deletionWaiter);

    underTest.deleteNetworkWithSubnets(networkDeletionRequest);

    verify(cloudFormationRetryClient).deleteStack(any(DeleteStackRequest.class));
    verify(awsClient).createCloudFormationRetryClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(deletionWaiter, times(1)).run(any());
}
 
Example #5
Source File: AwsTerminateService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public List<CloudResourceStatus> terminate(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
    LOGGER.debug("Deleting stack: {}", ac.getCloudContext().getId());
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    AuthenticatedContextView authenticatedContextView = new AuthenticatedContextView(ac);
    String regionName = authenticatedContextView.getRegion();
    AmazonEC2Client amazonEC2Client = authenticatedContextView.getAmazonEC2Client();
    AmazonCloudFormationClient amazonCloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName);

    awsCloudWatchService.deleteCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView);
    waitAndDeleteCloudformationStack(ac, stack, resources, amazonCloudFormationClient);
    awsComputeResourceService.deleteComputeResources(ac, stack, resources);
    cleanupEncryptedResources(ac, resources, regionName, amazonEC2Client);
    deleteKeyPair(ac, stack, amazonEC2Client, credentialView, regionName);
    deleteLaunchConfiguration(resources, ac);
    LOGGER.debug("Deleting stack finished");
    return awsResourceConnector.check(ac, resources);
}
 
Example #6
Source File: AwsLaunchTemplateImageUpdateService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public void updateImage(AuthenticatedContext authenticatedContext, CloudStack stack, CloudResource cfResource) {
    AwsCredentialView credentialView = new AwsCredentialView(authenticatedContext.getCloudCredential());
    String regionName = authenticatedContext.getCloudContext().getLocation().getRegion().getRegionName();
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName);

    String imageName = stack.getImage().getImageName();
    String cfStackName = cfResource.getName();
    String cfTemplate = getCfTemplate(cfResource, cloudFormationClient);
    Json templateJson = new Json(cfTemplate);

    Map<String, String> encryptedImages = getEncryptedImagesMappedByAutoscalingGroupName(authenticatedContext, stack);
    stack.getGroups().forEach(group -> {
        String imageIdPath = String.format("Resources.%s.Properties.LaunchTemplateData.ImageId", AwsGroupView.getLaunchTemplateName(group.getName()));
        Object oldImageId = templateJson.getValue(imageIdPath);
        boolean encryptedImage = !"{\"Ref\":\"AMI\"}".equals(oldImageId.toString());
        if (encryptedImage) {
            replaceEncryptedImageInGroup(encryptedImages, imageName, templateJson, group, imageIdPath);
        }
    });

    String newCfTemplate = templateJson.getValue();
    UpdateStackRequest updateStackRequest = awsStackRequestHelper.createUpdateStackRequest(authenticatedContext, stack, cfStackName, newCfTemplate);
    cloudFormationClient.updateStack(updateStackRequest);
}
 
Example #7
Source File: UpdateStackTask.java    From aws-ant-tasks with Apache License 2.0 5 votes vote down vote up
public void execute() {
    checkParams();
    AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class);
    UpdateStackRequest request = new UpdateStackRequest()
            .withStackName(stackName).withStackPolicyBody(stackPolicyBody)
            .withStackPolicyURL(stackPolicyURL)
            .withTemplateBody(templateBody).withTemplateURL(templateURL)
            .withStackPolicyDuringUpdateBody(stackPolicyDuringUpdateBody)
            .withStackPolicyDuringUpdateURL(stackPolicyDuringUpdateURL)
            .withUsePreviousTemplate(usePreviousTemplate);

    if (capabilities.size() > 0) {
        request.setCapabilities(capabilities);
    }
    if (parameters.size() > 0) {
        request.setParameters(parameters);
    }
    if (notificationArns.size() > 0) {
        request.setNotificationARNs(notificationArns);
    }

    try {
        client.updateStack(request);
        System.out.println("Update stack " + stackName
                + " request submitted.");
    } catch (Exception e) {
        throw new BuildException("Could not update stack: "
                + e.getMessage(), e);
    }
}
 
Example #8
Source File: LoaderTest.java    From AWS-MIMIC-IIItoOMOP with Apache License 2.0 5 votes vote down vote up
/**
 * Test of teardown method, of class Loader.
 */
@Test
public void testTeardown() throws IOException, SQLException, ClassNotFoundException 
{
    AmazonCloudFormationClient client = new AmazonCloudFormationClient();
    String name = "TestTeardownCluster" + UUID.randomUUID().toString();
    CreateStackRequest createStackRequest = new CreateStackRequest();
    
    client.setRegion(Region.getRegion(Regions.US_WEST_2)); 
    
    createStackRequest.setStackName(name);
    createStackRequest.setTemplateBody(IOUtils.toString(getClass().getResourceAsStream("cloudformation.template"), "UTF-8"));
    
    client.createStack(createStackRequest);
    
    loader.setConfiguration(configuration);
    loader.teardown(name, Region.getRegion(Regions.US_WEST_2));
    
    for(StackSummary stack : client.listStacks().getStackSummaries())
    {
        if(stack.getStackStatus().equalsIgnoreCase("DELETE_COMPLETE")) continue;
        if(stack.getStackStatus().equalsIgnoreCase("DELETE_FAILED")) continue;
        if(stack.getStackStatus().equalsIgnoreCase("DELETE_IN_PROGRESS")) continue;
        
        if(stack.getStackName().equals(name)) fail(name +  " should have been deleted but status is: " + stack.getStackStatus());
    }
}
 
Example #9
Source File: Cloudformation.java    From lambadaframework with MIT License 5 votes vote down vote up
protected AmazonCloudFormationClient getCloudFormationClient() {
    if (null != cloudformationClient) {
        return cloudformationClient;
    }

    return cloudformationClient = new AmazonCloudFormationClient(getAWSCredentialsProvideChain()).withRegion(Region.getRegion(Regions.fromName(deployment.getRegion())));
}
 
Example #10
Source File: AwsNetworkConnectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateNewNetworkWithSubnetsShouldCreateTheNetworkAndSubnets() {
    String networkCidr = "0.0.0.0/16";
    Set<NetworkSubnetRequest> subnets = Set.of(new NetworkSubnetRequest("1.1.1.1/8", PUBLIC), new NetworkSubnetRequest("1.1.1.2/8", PUBLIC));
    AmazonCloudFormationRetryClient cloudFormationRetryClient = mock(AmazonCloudFormationRetryClient.class);
    AmazonServiceException amazonServiceException = new AmazonServiceException("does not exist");
    amazonServiceException.setStatusCode(400);
    when(cloudFormationRetryClient.describeStacks(any(DescribeStacksRequest.class))).thenThrow(amazonServiceException);
    AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    Map<String, String> output = createOutput();
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(networkCidr, subnets);
    List<SubnetRequest> subnetRequestList = createSubnetRequestList();
    Set<CreatedSubnet> createdSubnets = Set.of(new CreatedSubnet(), new CreatedSubnet(), new CreatedSubnet());

    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(awsSubnetRequestProvider.provide(ec2Client, new ArrayList<>(subnets), new ArrayList<>(subnets))).thenReturn(subnetRequestList);
    when(awsClient.createCloudFormationRetryClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cloudFormationRetryClient);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient);
    when(cfClient.waiters()).thenReturn(cfWaiters);
    when(cfWaiters.stackCreateComplete()).thenReturn(creationWaiter);
    when(cfStackUtil.getOutputs(NETWORK_ID, cloudFormationRetryClient)).thenReturn(output);
    when(awsCreatedSubnetProvider.provide(output, subnetRequestList, true)).thenReturn(createdSubnets);

    CreatedCloudNetwork actual = underTest.createNetworkWithSubnets(networkCreationRequest);

    verify(awsClient).createCloudFormationRetryClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(awsNetworkCfTemplateProvider).provide(networkCreationRequest, subnetRequestList);
    verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(creationWaiter, times(1)).run(any());
    verify(awsTaggingService).prepareCloudformationTags(any(), any());
    verify(cloudFormationRetryClient).createStack(any(CreateStackRequest.class));
    verify(cfStackUtil).getOutputs(NETWORK_ID, cloudFormationRetryClient);
    assertEquals(VPC_ID, actual.getNetworkId());
    assertEquals(NUMBER_OF_SUBNETS, actual.getSubnets().size());
}
 
Example #11
Source File: AwsNetworkConnectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateNetworkWithSubnetsShouldReturnTheNetworkAndSubnets() {
    String networkCidr = "0.0.0.0/16";
    Set<NetworkSubnetRequest> subnets = Set.of(new NetworkSubnetRequest("1.1.1.1/8", PUBLIC), new NetworkSubnetRequest("1.1.1.2/8", PUBLIC));
    AmazonCloudFormationRetryClient cloudFormationRetryClient = mock(AmazonCloudFormationRetryClient.class);
    AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    Map<String, String> output = createOutput();
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(networkCidr, subnets);
    List<SubnetRequest> subnetRequestList = createSubnetRequestList();
    Set<CreatedSubnet> createdSubnets = Set.of(new CreatedSubnet(), new CreatedSubnet(), new CreatedSubnet());

    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(awsSubnetRequestProvider.provide(ec2Client, new ArrayList<>(subnets), new ArrayList<>(subnets))).thenReturn(subnetRequestList);
    when(awsClient.createCloudFormationRetryClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cloudFormationRetryClient);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient);

    when(cfClient.waiters()).thenReturn(cfWaiters);
    when(cfWaiters.stackCreateComplete()).thenReturn(creationWaiter);
    when(cfStackUtil.getOutputs(NETWORK_ID, cloudFormationRetryClient)).thenReturn(output);
    when(awsCreatedSubnetProvider.provide(output, subnetRequestList, true)).thenReturn(createdSubnets);

    CreatedCloudNetwork actual = underTest.createNetworkWithSubnets(networkCreationRequest);

    verify(awsClient).createCloudFormationRetryClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(creationWaiter, times(1)).run(any());
    verify(cfStackUtil).getOutputs(NETWORK_ID, cloudFormationRetryClient);
    verify(awsTaggingService, never()).prepareCloudformationTags(any(), any());
    verify(cloudFormationRetryClient, never()).createStack(any(CreateStackRequest.class));
    assertEquals(VPC_ID, actual.getNetworkId());
    assertEquals(NUMBER_OF_SUBNETS, actual.getSubnets().size());
}
 
Example #12
Source File: AwsNetworkConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteNetworkWithSubnets(NetworkDeletionRequest networkDeletionRequest) {
    if (!networkDeletionRequest.isExisting()) {
        AwsCredentialView credentialView = new AwsCredentialView(networkDeletionRequest.getCloudCredential());
        AmazonCloudFormationRetryClient cloudFormationRetryClient = getCloudFormationRetryClient(credentialView, networkDeletionRequest.getRegion());
        DeleteStackRequest deleteStackRequest = new DeleteStackRequest();
        deleteStackRequest.setStackName(networkDeletionRequest.getStackName());
        cloudFormationRetryClient.deleteStack(deleteStackRequest);
        AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, networkDeletionRequest.getRegion());
        Waiter<DescribeStacksRequest> deletionWaiter = cfClient.waiters().stackDeleteComplete();
        LOGGER.debug("CloudFormation stack deletion request sent with stack name: '{}' ", networkDeletionRequest.getStackName());
        DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(networkDeletionRequest.getStackName());
        run(deletionWaiter, describeStacksRequest, null);
    }
}
 
Example #13
Source File: AwsNetworkConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private CreatedCloudNetwork getCreatedNetworkWithPolling(NetworkCreationRequest networkRequest, AwsCredentialView credentialView,
        AmazonCloudFormationRetryClient cloudFormationRetryClient, List<SubnetRequest> subnetRequests) {

    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, networkRequest.getRegion().value());
    Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
    DescribeStacksRequest stackRequestWithStackId = new DescribeStacksRequest().withStackName(networkRequest.getStackName());
    EnvironmentCancellationCheck environmentCancellationCheck = new EnvironmentCancellationCheck(networkRequest.getEnvId(), networkRequest.getEnvName());

    run(creationWaiter, stackRequestWithStackId,
            environmentCancellationCheck);

    return getCreatedCloudNetwork(cloudFormationRetryClient, networkRequest, subnetRequests);
}
 
Example #14
Source File: AwsClient.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public AmazonCloudFormationClient createCloudFormationClient(AwsCredentialView awsCredential, String regionName) {
    AmazonCloudFormationClient client = isRoleAssumeRequired(awsCredential) ?
            new AmazonCloudFormationClient(createAwsSessionCredentialProvider(awsCredential), getDefaultClientConfiguration()) :
            new AmazonCloudFormationClient(createAwsCredentials(awsCredential), getDefaultClientConfiguration());
    client.setRegion(RegionUtils.getRegion(regionName));
    return client;
}
 
Example #15
Source File: AwsTerminateService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void waitAndDeleteCloudformationStack(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources,
        AmazonCloudFormationClient amazonCloudFormationClient) {
    CloudResource stackResource = cfStackUtil.getCloudFormationStackResource(resources);
    if (stackResource == null) {
        LOGGER.debug("No cloudformation stack in resources");
        return;
    }
    String cFStackName = stackResource.getName();
    AmazonCloudFormationRetryClient cfRetryClient = awsClient.createCloudFormationRetryClient(amazonCloudFormationClient);
    LOGGER.debug("Search and wait stack with name: {}", cFStackName);
    DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
    try {
        retryService.testWith2SecDelayMax15Times(() -> isStackExist(cfRetryClient, cFStackName, describeStacksRequest));
    } catch (ActionFailedException ignored) {
        LOGGER.debug("Stack not found with name: {}", cFStackName);
        return;
    }

    resumeAutoScalingPolicies(ac, stack);
    LOGGER.debug("Delete cloudformation stack from resources");
    DeleteStackRequest deleteStackRequest = new DeleteStackRequest().withStackName(cFStackName);
    cfRetryClient.deleteStack(deleteStackRequest);
    Waiter<DescribeStacksRequest> stackDeleteCompleteWaiter = amazonCloudFormationClient.waiters().stackDeleteComplete();
    try {
        WaiterParameters<DescribeStacksRequest> describeStacksRequestWaiterParameters = new WaiterParameters<>(describeStacksRequest)
                .withPollingStrategy(getBackoffCancellablePollingStrategy(null));
        stackDeleteCompleteWaiter.run(describeStacksRequestWaiterParameters);
    } catch (Exception e) {
        LOGGER.debug("Cloudformation stack delete failed ", e);
        throw new CloudConnectorException(e.getMessage(), e);
    }
    LOGGER.debug("Cloudformation stack from resources has been deleted");
}
 
Example #16
Source File: AwsLaunchConfigurationImageUpdateService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public void updateImage(AuthenticatedContext authenticatedContext, CloudStack stack, CloudResource cfResource) {
    AwsCredentialView credentialView = new AwsCredentialView(authenticatedContext.getCloudCredential());
    String regionName = authenticatedContext.getCloudContext().getLocation().getRegion().getRegionName();
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName);
    AmazonAutoScalingClient autoScalingClient = awsClient.createAutoScalingClient(credentialView, regionName);

    Map<String, String> encryptedImages = getEncryptedImagesMappedByAutoscalingGroupName(authenticatedContext, stack);
    Map<AutoScalingGroup, String> scalingGroups = autoScalingGroupHandler.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource);
    List<LaunchConfiguration> oldLaunchConfigurations = launchConfigurationHandler.getLaunchConfigurations(autoScalingClient, scalingGroups.keySet());
    for (LaunchConfiguration oldLaunchConfiguration : oldLaunchConfigurations) {
        changeImageInAutoscalingGroup(authenticatedContext, stack, autoScalingClient, scalingGroups, encryptedImages, oldLaunchConfiguration);
    }
}
 
Example #17
Source File: ContextStackAutoConfiguration.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingAmazonClient(AmazonCloudFormation.class)
public AmazonWebserviceClientFactoryBean<AmazonCloudFormationClient> amazonCloudFormation(
		ObjectProvider<RegionProvider> regionProvider,
		ObjectProvider<AWSCredentialsProvider> credentialsProvider) {
	return new AmazonWebserviceClientFactoryBean<>(AmazonCloudFormationClient.class,
			credentialsProvider.getIfAvailable(), regionProvider.getIfAvailable());
}
 
Example #18
Source File: AutoScalingGroupHandler.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Map<AutoScalingGroup, String> getAutoScalingGroups(AmazonCloudFormationClient cloudFormationClient, AmazonAutoScalingClient autoScalingClient,
        CloudResource cfResource) {
    DescribeStackResourcesRequest resourcesRequest = new DescribeStackResourcesRequest();
    resourcesRequest.setStackName(cfResource.getName());
    DescribeStackResourcesResult resourcesResult = cloudFormationClient.describeStackResources(resourcesRequest);
    Map<String, String> autoScalingGroups = resourcesResult.getStackResources().stream()
            .filter(stackResource -> "AWS::AutoScaling::AutoScalingGroup".equalsIgnoreCase(stackResource.getResourceType()))
            .collect(Collectors.toMap(StackResource::getPhysicalResourceId, StackResource::getLogicalResourceId));
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
    request.setAutoScalingGroupNames(autoScalingGroups.keySet());
    List<AutoScalingGroup> scalingGroups = autoScalingClient.describeAutoScalingGroups(request).getAutoScalingGroups();
    return scalingGroups.stream()
            .collect(Collectors.toMap(scalingGroup -> scalingGroup, scalingGroup -> autoScalingGroups.get(scalingGroup.getAutoScalingGroupName())));
}
 
Example #19
Source File: TearDownCloudFormationTestsTask.java    From aws-ant-tasks with Apache License 2.0 5 votes vote down vote up
public void execute() {
    checkParams();
    AmazonEC2Client ec2Client = getOrCreateClient(AmazonEC2Client.class);
    ec2Client
            .deleteKeyPair(new DeleteKeyPairRequest().withKeyName(keyName));
    AmazonCloudFormationClient cloudFormationClient = getOrCreateClient(AmazonCloudFormationClient.class);
    cloudFormationClient.deleteStack(new DeleteStackRequest()
            .withStackName(stackName));
}
 
Example #20
Source File: CloudFormationTests.java    From aws-ant-tasks with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() {
    client = new AmazonCloudFormationClient();
    project = new Project();
    SetUpCloudFormationTestsTask setUpTask = new SetUpCloudFormationTestsTask();
    setUpTask.setProject(project);
    setUpTask.execute();
}
 
Example #21
Source File: WaitForStackToReachStateTask.java    From aws-ant-tasks with Apache License 2.0 5 votes vote down vote up
public static boolean waitForCloudFormationStackToReachStatus(
        AmazonCloudFormationClient client, String stackName, String status) {
    int count = 0;
    while (true) {
        if (count++ == 100) {
            System.out
                    .println(stackName + " never reached state " + status);
            return false;
        }
        try {
            Thread.sleep(1000 * 30);
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
            return false;
        }
        String stackStatus = client
                .describeStacks(
                        new DescribeStacksRequest()
                                .withStackName(stackName)).getStacks()
                .get(0).getStackStatus();
        if (stackStatus.equals(status)) {
            return true;
        } else if (stackStatus.contains(FAILED)) {
            System.out.println("The process failed with status " + stackStatus);
            return false;
        }
        System.out.println(stackName + " is in status " + stackStatus);
    }
}
 
Example #22
Source File: WaitForStackToReachStateTask.java    From aws-ant-tasks with Apache License 2.0 5 votes vote down vote up
public void execute() {
    checkParams();
    AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class);
    if (!waitForCloudFormationStackToReachStatus(client, stackName, status)) {
        throw new BuildException("The stack update or creation failed");
    }
}
 
Example #23
Source File: SetStackPolicyTask.java    From aws-ant-tasks with Apache License 2.0 5 votes vote down vote up
public void execute() {
    checkParams();
    AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class);
    SetStackPolicyRequest setStackPolicyRequest = new SetStackPolicyRequest()
            .withStackName(stackName).withStackPolicyBody(stackPolicyBody)
            .withStackPolicyURL(stackPolicyURL);
    try {
        client.setStackPolicy(setStackPolicyRequest);
        System.out.println("Successfully set stack policy");
    } catch (Exception e) {
        throw new BuildException("Could not set the stack policy "
                + e.getMessage(), e);
    }
}
 
Example #24
Source File: CreateStackTask.java    From aws-ant-tasks with Apache License 2.0 5 votes vote down vote up
public void execute() {
    checkParams();
    AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class);
    CreateStackRequest createStackRequest = new CreateStackRequest()
            .withDisableRollback(disableRollback).withOnFailure(onFailure)
            .withStackName(stackName).withStackPolicyBody(stackPolicyBody)
            .withStackPolicyURL(stackPolicyURL)
            .withTemplateBody(templateBody).withTemplateURL(templateURL)
            .withTimeoutInMinutes(timeoutInMinutes);

    if (capabilities.size() > 0) {
        createStackRequest.setCapabilities(capabilities);
    }
    if (parameters.size() > 0) {
        createStackRequest.setParameters(parameters);
    }
    if (tags.size() > 0) {
        createStackRequest.setTags(tags);
    }
    try {
        client.createStack(createStackRequest);
        System.out.println("Create stack " + stackName
                + " request submitted.");
        if(waitForCreation) {
            WaitForStackToReachStateTask.waitForCloudFormationStackToReachStatus(client, stackName, CREATE_COMPLETE);
        }
    } catch (Exception e) {
        throw new BuildException(
                "Could not create stack " + e.getMessage(), e);
    }
}
 
Example #25
Source File: AwsLaunchTemplateImageUpdateService.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
private String getCfTemplate(CloudResource cfResource, AmazonCloudFormationClient cloudFormationClient) {
    String cfStackName = cfResource.getName();
    GetTemplateResult template = cloudFormationClient.getTemplate(new GetTemplateRequest().withStackName(cfStackName));
    return template.getTemplateBody();
}
 
Example #26
Source File: AwsIntegrationTestStackRule.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
@Override
protected void before() throws Throwable {
	try {
		String awsCredentialsDir = System.getProperty("aws.credentials.path");
		File awsCredentialsFile = new File(awsCredentialsDir, "aws.credentials.properties");
		Properties awsCredentials = new Properties();
		awsCredentials.load(new FileReader(awsCredentialsFile));
		String accessKey = awsCredentials.getProperty("cloud.aws.credentials.accessKey");
		String secretKey = awsCredentials.getProperty("cloud.aws.credentials.secretKey");
		this.cloudFormation = new AmazonCloudFormationClient(new BasicAWSCredentials(accessKey, secretKey));

		YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
		yamlPropertiesFactoryBean.setResources(new ClassPathResource("application.yml"));
		Properties applicationProperties = yamlPropertiesFactoryBean.getObject();

		this.stackName = applicationProperties.getProperty("cloud.aws.stack.name");

		after();

		ClassPathResource stackTemplate = new ClassPathResource("AwsIntegrationTestTemplate.json");
		String templateBody = FileCopyUtils.copyToString(new InputStreamReader(stackTemplate.getInputStream()));

		this.cloudFormation.createStack(
				new CreateStackRequest()
						.withTemplateBody(templateBody)
						.withOnFailure(OnFailure.DELETE)
						.withStackName(this.stackName));

		waitForCompletion();

		System.setProperty("cloud.aws.credentials.accessKey", accessKey);
		System.setProperty("cloud.aws.credentials.secretKey", secretKey);
	}
	catch (Exception e) {
		if (!(e instanceof AssumptionViolatedException)) {
			Assume.assumeTrue("Can't perform AWS integration test because of: " + e.getMessage(), false);
		}
		else {
			throw e;
		}
	}
}
 
Example #27
Source File: AwsLaunchService.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier,
        AdjustmentType adjustmentType, Long threshold) throws Exception {
    createKeyPair(ac, stack);
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationRetryClient cfRetryClient = awsClient.createCloudFormationRetryClient(credentialView, regionName);
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
    Network network = stack.getNetwork();
    AwsNetworkView awsNetworkView = new AwsNetworkView(network);
    boolean mapPublicIpOnLaunch = awsNetworkService.isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
    DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
    try {
        cfRetryClient.describeStacks(describeStacksRequest);
        LOGGER.debug("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException ignored) {
        boolean existingVPC = awsNetworkView.isExistingVPC();
        boolean existingSubnet = awsNetworkView.isExistingSubnet();
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());

        String cidr = network.getSubnet().getCidr();
        String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? awsNetworkService.findNonOverLappingCIDR(ac, stack) : cidr;
        AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
        ModelContext modelContext = new ModelContext()
                .withAuthenticatedContext(ac)
                .withStack(stack)
                .withExistingVpc(existingVPC)
                .withExistingIGW(awsNetworkView.isExistingIGW())
                .withExistingSubnetCidr(existingSubnet ? awsNetworkService.getExistingSubnetCidr(ac, stack) : null)
                .withExistinVpcCidr(awsNetworkService.getVpcCidrs(ac, stack))
                .withExistingSubnetIds(existingSubnet ? awsNetworkView.getSubnetList() : null)
                .mapPublicIpOnLaunch(mapPublicIpOnLaunch)
                .withEnableInstanceProfile(awsInstanceProfileView.isInstanceProfileAvailable())
                .withInstanceProfileAvailable(awsInstanceProfileView.isInstanceProfileAvailable())
                .withTemplate(stack.getTemplate())
                .withDefaultSubnet(subnet)
                .withOutboundInternetTraffic(network.getOutboundInternetTraffic())
                .withVpcCidrs(network.getNetworkCidrs())
                .withPrefixListIds(getPrefixListIds(amazonEC2Client, regionName, network.getOutboundInternetTraffic()))
                .withEncryptedAMIByGroupName(encryptedImageCopyService.createEncryptedImages(ac, stack, resourceNotifier));
        String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfRetryClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate));
    }
    LOGGER.debug("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());

    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
    StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
    run(creationWaiter, describeStacksRequest, stackCancellationCheck);

    List<CloudResource> networkResources = saveGeneratedSubnet(ac, stack, cFStackName, cfRetryClient, resourceNotifier);
    suspendAutoscalingGoupsWhenNewInstancesAreReady(ac, stack);

    AmazonAutoScalingRetryClient amazonASClient = awsClient.createAutoScalingRetryClient(credentialView, regionName);
    List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfRetryClient, amazonASClient, stack.getGroups());

    if (mapPublicIpOnLaunch) {
        associatePublicIpsToGatewayInstances(stack, cFStackName, cfRetryClient, amazonEC2Client, instances);
    }

    awsComputeResourceService.buildComputeResourcesForLaunch(ac, stack, adjustmentType, threshold, instances, networkResources);

    awsTaggingService.tagRootVolumes(ac, amazonEC2Client, instances, stack.getTags());

    awsCloudWatchService.addCloudWatchAlarmsForSystemFailures(instances, stack, regionName, credentialView);

    return awsResourceConnector.check(ac, instances);
}
 
Example #28
Source File: AwsRdsLaunchService.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public List<CloudResourceStatus> launch(AuthenticatedContext ac, DatabaseStack stack, PersistenceNotifier resourceNotifier)
        throws Exception {
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationRetryClient cfRetryClient = awsClient.createCloudFormationRetryClient(credentialView, regionName);
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
    try {
        cfRetryClient.describeStacks(describeStacksRequest);
        LOGGER.debug("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException ignored) {
        // all subnets desired for DB subnet group are in the stack
        boolean existingSubnet = awsNetworkView.isExistingSubnet();
        if (!existingSubnet) {
            throw new CloudConnectorException("Can only create RDS instance with existing subnets");
        }
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());

        RDSModelContext rdsModelContext = new RDSModelContext()
                .withTemplate(stack.getTemplate())
                .withNetworkCidrs(awsNetworkView.getExistingVpcCidrs())
                .withHasPort(stack.getDatabaseServer().getPort() != null)
                .withHasSecurityGroup(!stack.getDatabaseServer().getSecurity().getCloudSecurityIds().isEmpty());
        String cfTemplate = cloudFormationTemplateBuilder.build(rdsModelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfRetryClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, cfTemplate));
    }
    LOGGER.debug("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());

    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
    StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
    run(creationWaiter, describeStacksRequest,
            stackCancellationCheck);

    List<CloudResource> databaseResources = getCreatedOutputs(ac, stack, cFStackName, cfRetryClient, resourceNotifier);
    databaseResources.forEach(dbr -> resourceNotifier.notifyAllocation(dbr, ac.getCloudContext()));
    // FIXME: For now, just return everything wrapped in a status object
    return databaseResources.stream()
            .map(resource -> new CloudResourceStatus(resource, ResourceStatus.CREATED))
            .collect(Collectors.toList());
    // FIXME check does nothing?!
    //return awsResourceConnector.check(ac, databaseResources);
}
 
Example #29
Source File: AwsClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public AmazonCloudFormationRetryClient createCloudFormationRetryClient(AmazonCloudFormationClient amazonCloudFormationClient) {
    return new AmazonCloudFormationRetryClient(amazonCloudFormationClient, retry);
}
 
Example #30
Source File: AmazonCloudFormationRetryClient.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public AmazonCloudFormationRetryClient(AmazonCloudFormationClient client, Retry retry) {
    this.client = client;
    this.retry = retry;
}