Example usage for com.amazonaws.services.cloudformation AmazonCloudFormationClient estimateTemplateCost

List of usage examples for com.amazonaws.services.cloudformation AmazonCloudFormationClient estimateTemplateCost

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudformation AmazonCloudFormationClient estimateTemplateCost.

Prototype

@Override
public EstimateTemplateCostResult estimateTemplateCost(EstimateTemplateCostRequest request) 

Source Link

Document

Returns the estimated monthly cost of a template.

Usage

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

License:Open Source License

protected void createStack(Optional<String> stackName, TereusInput tereusInput, JsonElement templateData,
        boolean logTemplate) throws UnsupportedEncodingException {
    if (tereusInput.dryRun) {
        tereusInput.logger.info("Dry run requested (-n/--noop). Stack creation bypassed.");
        if (logTemplate) {
            final String formattedTemplate = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping()
                    .create().toJson(templateData);
            tereusInput.logger.info("Stack Template:\n {}", formattedTemplate);
        }//from ww w .j  a  v  a  2s  . c om
    } else {
        final String bucketName = tereusInput.params.get(CONSTANTS.PARAMETER_NAMES.S3_BUCKET_NAME).toString();
        // Upload the template
        final String templateContent = new GsonBuilder().create().toJson(templateData);
        final byte[] templateBytes = templateContent.getBytes("UTF-8");
        final InputStream is = new ByteArrayInputStream(templateBytes);
        final String templateDigest = DigestUtils.sha256Hex(templateBytes);
        final String keyName = String.format("%s-tereus.cf.template", templateDigest);

        try (S3Resource resource = new S3Resource(bucketName, keyName, is,
                Optional.of(Long.valueOf(templateBytes.length)))) {
            resource.upload();
            final EstimateTemplateCostRequest costRequest = new EstimateTemplateCostRequest();
            costRequest.setParameters(toParameterList(tereusInput.params));
            costRequest.setTemplateURL(resource.getResourceURL().get());
            final AmazonCloudFormationClient awsClient = new AmazonCloudFormationClient(
                    tereusInput.awsCredentials);
            awsClient.setRegion(tereusInput.awsRegion);
            final EstimateTemplateCostResult costResult = awsClient.estimateTemplateCost(costRequest);
            tereusInput.logger.info("Cost Estimator: {}", costResult.getUrl());

            // Go ahead and create the stack.
            final String defaultTemplateName = String.format("Tereus-%s", System.currentTimeMillis());
            final CreateStackRequest request = new CreateStackRequest()
                    .withStackName(stackName.orElse(defaultTemplateName))
                    .withTemplateURL(resource.getResourceURL().get())
                    .withParameters(toParameterList(tereusInput.params)).withTags(toTagList(tereusInput.tags))
                    .withCapabilities("CAPABILITY_IAM");
            tereusInput.logger.debug("Creating stack: {}", stackName);
            tereusInput.logger.debug("Stack params: {}", request.getParameters());
            tereusInput.logger.debug("Stack tags: {}", request.getTags());
            final Optional<DescribeStacksResult> result = new CloudFormation().createStack(request,
                    tereusInput.awsRegion, tereusInput.logger);
            if (result.isPresent()) {
                tereusInput.logger.info("Stack successfully created");
                tereusInput.logger.info(result.get().toString());
                resource.setReleased(true);
            }
        }
    }
}