Example usage for com.amazonaws.services.cloudformation.model DeleteStackRequest DeleteStackRequest

List of usage examples for com.amazonaws.services.cloudformation.model DeleteStackRequest DeleteStackRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudformation.model DeleteStackRequest DeleteStackRequest.

Prototype

DeleteStackRequest

Source Link

Usage

From source file:CloudFormation.java

License:Open Source License

public static void main(String[] args) throws Exception {
    /*/*from   w  w w .  j  a va 2 s  .c o  m*/
     * This credentials provider implementation loads your AWS credentials
     * from a properties file at the root of your classpath.
     * Important: Be sure to fill in your AWS access credentials in the
     *            AwsCredentials.properties file before you try to run this
     *            sample.
     * http://aws.amazon.com/security-credentials
     */
    AmazonCloudFormation stackbuilder = new AmazonCloudFormationClient(
            new ClasspathPropertiesFileCredentialsProvider());
    Region usWest2 = Region.getRegion(Regions.US_EAST_1);
    stackbuilder.setRegion(usWest2);

    System.out.println("===========================================");
    System.out.println("Getting Started with AWS CloudFormation");
    System.out.println("===========================================\n");

    String stackName = "CloudFormationSampleStack";
    String logicalResourceName = "SampleNotificationTopic";

    try {
        // Create a stack
        CreateStackRequest createRequest = new CreateStackRequest();
        createRequest.setStackName(stackName);
        createRequest.setTemplateBody(
                convertStreamToString(CloudFormation.class.getResourceAsStream("CloudFormation.template")));
        System.out.println("Creating a stack called " + createRequest.getStackName() + ".");
        stackbuilder.createStack(createRequest);

        // Wait for stack to be created
        // Note that you could use SNS notifications on the CreateStack call to track the progress of the stack creation
        System.out.println("Stack creation completed, the stack " + stackName + " completed with "
                + waitForCompletion(stackbuilder, stackName));

        // Show all the stacks for this account along with the resources for each stack
        for (Stack stack : stackbuilder.describeStacks(new DescribeStacksRequest()).getStacks()) {
            System.out.println(
                    "Stack : " + stack.getStackName() + " [" + stack.getStackStatus().toString() + "]");

            DescribeStackResourcesRequest stackResourceRequest = new DescribeStackResourcesRequest();
            stackResourceRequest.setStackName(stack.getStackName());
            for (StackResource resource : stackbuilder.describeStackResources(stackResourceRequest)
                    .getStackResources()) {
                System.out.format("    %1$-40s %2$-25s %3$s\n", resource.getResourceType(),
                        resource.getLogicalResourceId(), resource.getPhysicalResourceId());
            }
        }

        // Lookup a resource by its logical name
        DescribeStackResourcesRequest logicalNameResourceRequest = new DescribeStackResourcesRequest();
        logicalNameResourceRequest.setStackName(stackName);
        logicalNameResourceRequest.setLogicalResourceId(logicalResourceName);
        System.out.format("Looking up resource name %1$s from stack %2$s\n",
                logicalNameResourceRequest.getLogicalResourceId(), logicalNameResourceRequest.getStackName());
        for (StackResource resource : stackbuilder.describeStackResources(logicalNameResourceRequest)
                .getStackResources()) {
            System.out.format("    %1$-40s %2$-25s %3$s\n", resource.getResourceType(),
                    resource.getLogicalResourceId(), resource.getPhysicalResourceId());
        }

        // Delete the stack
        DeleteStackRequest deleteRequest = new DeleteStackRequest();
        deleteRequest.setStackName(stackName);
        System.out.println("Deleting the stack called " + deleteRequest.getStackName() + ".");
        stackbuilder.deleteStack(deleteRequest);

        // Wait for stack to be deleted
        // Note that you could used SNS notifications on the original CreateStack call to track the progress of the stack deletion
        System.out.println("Stack creation completed, the stack " + stackName + " completed with "
                + waitForCompletion(stackbuilder, stackName));

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to AWS CloudFormation, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with AWS CloudFormation, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:CloudFormationSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    /*//from  w  w  w. j av  a2s  . c  o m
    * Important: Be sure to fill in your AWS access credentials in the
    *            AwsCredentials.properties file before you try to run this
    *            sample.
    * http://aws.amazon.com/security-credentials
    */
    AmazonCloudFormation stackbuilder = new AmazonCloudFormationClient(new PropertiesCredentials(
            CloudFormationSample.class.getResourceAsStream("AwsCredentials.properties")));

    System.out.println("===========================================");
    System.out.println("Getting Started with AWS CloudFormation");
    System.out.println("===========================================\n");

    String stackName = "CloudFormationSampleStack";
    String logicalResourceName = "SampleNotificationTopic";

    try {
        // Create a stack
        CreateStackRequest createRequest = new CreateStackRequest();
        createRequest.setStackName(stackName);
        createRequest.setTemplateBody(convertStreamToString(
                CloudFormationSample.class.getResourceAsStream("CloudFormationSample.template")));
        System.out.println("Creating a stack called " + createRequest.getStackName() + ".");
        stackbuilder.createStack(createRequest);

        // Wait for stack to be created
        // Note that you could use SNS notifications on the CreateStack call to track the progress of the stack creation
        System.out.println("Stack creation completed, the stack " + stackName + " completed with "
                + waitForCompletion(stackbuilder, stackName));

        // Show all the stacks for this account along with the resources for each stack
        for (Stack stack : stackbuilder.describeStacks(new DescribeStacksRequest()).getStacks()) {
            System.out.println(
                    "Stack : " + stack.getStackName() + " [" + stack.getStackStatus().toString() + "]");

            DescribeStackResourcesRequest stackResourceRequest = new DescribeStackResourcesRequest();
            stackResourceRequest.setStackName(stack.getStackName());
            for (StackResource resource : stackbuilder.describeStackResources(stackResourceRequest)
                    .getStackResources()) {
                System.out.format("    %1$-40s %2$-25s %3$s\n", resource.getResourceType(),
                        resource.getLogicalResourceId(), resource.getPhysicalResourceId());
            }
        }

        // Lookup a resource by its logical name
        DescribeStackResourcesRequest logicalNameResourceRequest = new DescribeStackResourcesRequest();
        logicalNameResourceRequest.setStackName(stackName);
        logicalNameResourceRequest.setLogicalResourceId(logicalResourceName);
        System.out.format("Looking up resource name %1$s from stack %2$s\n",
                logicalNameResourceRequest.getLogicalResourceId(), logicalNameResourceRequest.getStackName());
        for (StackResource resource : stackbuilder.describeStackResources(logicalNameResourceRequest)
                .getStackResources()) {
            System.out.format("    %1$-40s %2$-25s %3$s\n", resource.getResourceType(),
                    resource.getLogicalResourceId(), resource.getPhysicalResourceId());
        }

        // Delete the stack
        DeleteStackRequest deleteRequest = new DeleteStackRequest();
        deleteRequest.setStackName(stackName);
        System.out.println("Deleting the stack called " + deleteRequest.getStackName() + ".");
        stackbuilder.deleteStack(deleteRequest);

        // Wait for stack to be deleted
        // Note that you could used SNS notifications on the original CreateStack call to track the progress of the stack deletion
        System.out.println("Stack creation completed, the stack " + stackName + " completed with "
                + waitForCompletion(stackbuilder, stackName));

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to AWS CloudFormation, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with AWS CloudFormation, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:br.com.ingenieux.mojo.cloudformation.DeleteStackMojo.java

License:Apache License

@Override
protected Object executeInternal() throws Exception {
    shouldFailIfMissingStack(failIfMissing);

    getService().deleteStack(//from w  ww. j  a  v  a 2 s  .c  o m
            new DeleteStackRequest().withStackName(this.stackName).withRetainResources(retainResources));

    WaitForStackCommand.WaitForStackContext ctx = new WaitForStackCommand.WaitForStackContext(this.stackName,
            getService(), this, 30, asList(StackStatus.DELETE_COMPLETE));

    new WaitForStackCommand(ctx).execute();

    return null;
}

From source file:com.carrotgarden.maven.aws.cfn.CarrotCloudForm.java

License:BSD License

/**
 *//*w  w  w  .  j a  v a  2  s . com*/
public Stack stackDelete() throws Exception {

    final DeleteStackRequest request = new DeleteStackRequest();

    request.withStackName(name);

    amazonClient.deleteStack(request);

    final Stack stack = waitForStackDelete();

    return stack;

}

From source file:com.deploymentio.cfnstacker.CloudFormationClient.java

License:Apache License

/**
 * Initiates deletion of a running stack
 */
public void deleteStack() {
    client.deleteStack(new DeleteStackRequest().withStackName(config.getName()));
}

From source file:com.github.kaklakariada.aws.sam.service.CloudformationService.java

License:Open Source License

public void deleteStack(String stackName) {
    logger.info("Delete stack '{}'", stackName);
    cloudFormation.deleteStack(new DeleteStackRequest().withStackName(stackName));
}

From source file:com.mweagle.tereus.aws.CloudFormation.java

License:Open Source License

protected Optional<DescribeStacksResult> waitForStackComplete(final AmazonCloudFormationAsyncClient awsClient,
        final String stackName, List<StackEvent> priorEvents, Logger logger) throws Exception {
    Map<String, StackEvent> eventHistory = new HashMap<>();
    for (StackEvent eachEvent : priorEvents) {
        eventHistory.put(eachEvent.getEventId(), eachEvent);
    }/*w  w  w.j  ava  2  s  .com*/
    Optional<StackEvent> terminationEvent = Optional.empty();

    final Predicate<StackEvent> isNewEvent = event -> {
        return !eventHistory.containsKey(event.getEventId());
    };

    final Predicate<StackEvent> isTerminalEvent = stackEvent -> {
        return (CloudFormation.TERMINAL_EVENTS.contains(stackEvent.getResourceStatus())
                && stackEvent.getResourceType().equals("AWS::CloudFormation::Stack"));
    };

    // Query for events
    final DescribeStackEventsRequest describeRequest = new DescribeStackEventsRequest();
    describeRequest.setStackName(stackName);
    while (!terminationEvent.isPresent()) {
        logger.debug("Waiting for StackEvents");
        Thread.sleep(20 * 1000);

        final List<StackEvent> events = getStackEvents(awsClient, stackName, logger);

        // Get all the events we haven't seen, log and mark them
        events.stream().filter(isNewEvent).forEach(item -> {
            logger.info(item.toString());
            eventHistory.put(item.getEventId(), item);
        });

        // Find the first terminal event
        terminationEvent = events.stream().filter(isTerminalEvent).findFirst();
    }

    // Don't ever delete anything, unless the initial event set length was empty, implying
    // a creation event
    if (priorEvents.size() <= 0 && terminationEvent.get().getResourceStatus().contains("_FAILED")) {
        logger.warn("Stack creation . Deleting stack.");
        final DeleteStackRequest deleteStackRequest = new DeleteStackRequest();
        deleteStackRequest.setStackName(stackName);
        awsClient.deleteStack(deleteStackRequest);
        return Optional.empty();
    } else {
        // Looks good, let's get the final output for the stack...
        return describeStack(awsClient, stackName, logger);
    }
}

From source file:com.mweagle.tereus.commands.DeleteCommand.java

License:Open Source License

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings({ "DM_EXIT", "OBL_UNSATISFIED_OBLIGATION" })
@Override/*  w w  w  .jav  a2  s  . c  om*/
public void run() {
    // Get the stack, delete the stack...
    int exitCode = 0;
    final Logger logger = LogManager.getLogger();

    try {
        final AmazonCloudFormationClient awsClient = new AmazonCloudFormationClient();
        awsClient.setRegion(RegionUtils.getRegion(this.region));
        final DescribeStacksRequest describeRequest = new DescribeStacksRequest().withStackName(this.stackName);
        final DescribeStacksResult describeResult = awsClient.describeStacks(describeRequest);
        logger.info(describeResult);
        logger.info("Deleting stack: {}", this.stackName);

        if (this.dryRun) {
            logger.info("Dry run requested (-n/--noop). Stack deletion bypassed.");
        } else {
            final DeleteStackRequest deleteRequest = new DeleteStackRequest().withStackName(this.stackName);
            awsClient.deleteStack(deleteRequest);
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        exitCode = 1;
    }
    System.exit(exitCode);
}

From source file:com.nike.cerberus.service.CloudFormationService.java

License:Apache License

/**
 * Deletes an existing stack by name./*  w ww .ja  va2  s .co  m*/
 *
 * @param stackId Stack ID.
 */
public void deleteStack(final String stackId) {
    final DeleteStackRequest request = new DeleteStackRequest().withStackName(stackId);
    cloudFormationClient.deleteStack(request);
}

From source file:de.taimos.pipeline.aws.cloudformation.CloudFormationStack.java

License:Apache License

public void delete(long pollIntervallMillis) throws ExecutionException {
    this.client.deleteStack(new DeleteStackRequest().withStackName(this.stack));
    new EventPrinter(this.client, this.listener).waitAndPrintStackEvents(this.stack,
            this.client.waiters().stackDeleteComplete(), pollIntervallMillis);
}