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

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

Introduction

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

Prototype

DescribeStacksRequest

Source Link

Usage

From source file:CloudFormation.java

License:Open Source License

public static void main(String[] args) throws Exception {
    /*/* w  w  w .jav  a2s. c  om*/
     * 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:CloudFormation.java

License:Open Source License

public static String waitForCompletion(AmazonCloudFormation stackbuilder, String stackName) throws Exception {

    DescribeStacksRequest wait = new DescribeStacksRequest();
    wait.setStackName(stackName);/*from   w ww. ja  v  a  2  s .c o  m*/
    Boolean completed = false;
    String stackStatus = "Unknown";
    String stackReason = "";

    System.out.print("Waiting");

    while (!completed) {
        List<Stack> stacks = stackbuilder.describeStacks(wait).getStacks();
        if (stacks.isEmpty()) {
            completed = true;
            stackStatus = "NO_SUCH_STACK";
            stackReason = "Stack has been deleted";
        } else {
            for (Stack stack : stacks) {
                if (stack.getStackStatus().equals(StackStatus.CREATE_COMPLETE.toString())
                        || stack.getStackStatus().equals(StackStatus.CREATE_FAILED.toString())
                        || stack.getStackStatus().equals(StackStatus.ROLLBACK_FAILED.toString())
                        || stack.getStackStatus().equals(StackStatus.DELETE_FAILED.toString())) {
                    completed = true;
                    stackStatus = stack.getStackStatus();
                    stackReason = stack.getStackStatusReason();
                }
            }
        }

        // Show we are waiting
        System.out.print(".");

        // Not done yet so sleep for 10 seconds.
        if (!completed)
            Thread.sleep(10000);
    }

    // Show we are done
    System.out.print("done\n");

    return stackStatus + " (" + stackReason + ")";
}

From source file:CloudFormationSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    /*/*from ww w . j  ava 2s . 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.cmd.WaitForStackCommand.java

License:Apache License

public void execute() throws Exception {
    Set<StackEvent> events = new TreeSet<>((o1, o2) -> {
        return o1.getEventId().compareTo(o2.getEventId());
    });/*from  w w  w. ja  v  a  2s  .  co m*/

    boolean done = false;

    Date timeoutsAt = new Date(System.currentTimeMillis() + 60000L * ctx.getTimeoutMins());

    do {
        boolean timedOut = !timeoutsAt.after(new Date(System.currentTimeMillis()));

        if (timedOut)
            throw new IllegalStateException("Timed Out");

        {
            final DescribeStackEventsRequest req = new DescribeStackEventsRequest()
                    .withStackName(ctx.getStackId());
            String nextToken = null;

            do {
                req.withNextToken(nextToken);

                Optional<DescribeStackEventsResult> stackEvents = getDescribeStackEventsResult(req);

                if (!stackEvents.isPresent()) {
                    return;
                } else {
                    for (StackEvent e : stackEvents.get().getStackEvents()) {
                        if (!events.contains(e)) {
                            ctx.getNotifier().info("" + e);

                            events.add(e);
                        }
                    }
                }

            } while (null != nextToken);
        }

        {
            final DescribeStacksResult stacks = ctx.getClient()
                    .describeStacks(new DescribeStacksRequest().withStackName(ctx.getStackId()));

            Optional<Stack> foundStack = stacks.getStacks().stream().filter(
                    stack -> ctx.getStatusesToMatch().contains(StackStatus.fromValue(stack.getStackStatus())))
                    .findFirst();

            done = foundStack.isPresent();
        }

        if (!done) {
            Thread.sleep(15000);
        }

    } while (!done);
}

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

License:Apache License

private Collection<Output> listOutputs() {
    if (isEmpty(stackId)) {
        return Collections.emptyList();
    }//w  w w.  j a  v a  2 s .co  m

    String nextToken = null;
    final DescribeStacksRequest request = new DescribeStacksRequest().withStackName(stackId);
    List<Output> result = new ArrayList<>();

    do {
        request.setNextToken(nextToken);

        final DescribeStacksResult response = getService().describeStacks(request);

        result.addAll(response.getStacks().stream().flatMap(stack -> stack.getOutputs().stream())
                .collect(Collectors.toList()));

        nextToken = response.getNextToken();
    } while (null != nextToken);

    return result;
}

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

License:BSD License

public Stack findStack() throws Exception {

    final DescribeStacksRequest request = new DescribeStacksRequest();

    final DescribeStacksResult result = amazonClient.describeStacks(request);

    for (final Stack stack : result.getStacks()) {
        if (name.equals(stack.getStackName())) {
            return stack;
        }//w w w .j a  v  a  2 s.  c o m
    }

    return null;

}

From source file:com.cleanenergyexperts.aws.cf.CloudFormationMojo.java

License:Apache License

protected Stack getStack(AmazonCloudFormationClient cfClient, String stackName) throws MojoExecutionException {
    Stack stack = null;/* w  w w  .  ja  va2s.c  o m*/
    try {
        DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest();
        describeStacksRequest.setStackName(stackName);
        getLog().info("Getting Cloud Formation Stack Details...");
        DescribeStacksResult describeStacksResult = cfClient.describeStacks(describeStacksRequest);
        if (describeStacksResult == null || describeStacksResult.getStacks() == null
                || describeStacksResult.getStacks().isEmpty()) {
            throw new MojoExecutionException("[NULL] Could Not Get Cloud Formation Stack Details");
        }
        stack = describeStacksResult.getStacks().get(0);
    } catch (AmazonServiceException e) {
        throw new MojoExecutionException("[SERVICE] Could Not Get Cloud Formation Stack Details", e);
    } catch (AmazonClientException e) {
        throw new MojoExecutionException("[CLIENT] Could Not Get Cloud Formation Stack Details", e);
    }
    return stack;
}

From source file:com.clicktravel.infrastructure.provisioning.aws.cloudformation.Stack.java

License:Apache License

public com.amazonaws.services.cloudformation.model.Stack describe() {
    final DescribeStacksRequest request = new DescribeStacksRequest().withStackName(name);
    final DescribeStacksResult result = cloudFormationClient.describeStacks(request);
    return result.getStacks().get(0);
}

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

License:Apache License

/**
 * Finds the stack with the given name. Will only find stack still
 * running// w w  w .  j a va  2  s  . c o m
 * 
 * @param name name of the stack
 * @return the stack object or <code>null</code> if no running stack with
 *         this name was found
 */
public Stack findStack(String name) {
    DescribeStacksResult describeStacksResult = null;
    String nextToken = null;

    do {
        describeStacksResult = client.describeStacks(new DescribeStacksRequest().withNextToken(nextToken));
        nextToken = describeStacksResult.getNextToken();

        for (Stack stack : describeStacksResult.getStacks()) {
            if (stack.getStackName().equals(name)) {
                return stack;
            }
        }
    } while (!StringUtils.isEmpty(nextToken));

    return null;
}

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

License:Open Source License

private List<Stack> describeStack(String stackName) {
    final DescribeStacksResult result = cloudFormation
            .describeStacks(new DescribeStacksRequest().withStackName(stackName));
    return result.getStacks();
}