Example usage for com.amazonaws.services.cloudformation.model UpdateStackRequest setTemplateBody

List of usage examples for com.amazonaws.services.cloudformation.model UpdateStackRequest setTemplateBody

Introduction

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

Prototype


public void setTemplateBody(String templateBody) 

Source Link

Document

Structure containing the template body with a minimum length of 1 byte and a maximum length of 51,200 bytes.

Usage

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

License:Apache License

public void execute() throws MojoExecutionException {
    getLog().info("Bucket Name: " + bucketName);
    //getLog().info("Cloud Formation Stack Name: " + stackName);

    if (artifactFile == null || !artifactFile.isFile()) {
        throw new MojoExecutionException("Cannot find artifact file to upload");
    }//from   w  w  w. jav a 2s  . c  om
    String artifactKey = artifactFile.getName();
    getLog().info("Artifact Name: " + artifactKey);

    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonCloudFormationClient cfClient = new AmazonCloudFormationClient(awsCredentials);
    cfClient.setEndpoint(getCloudFormationEndPoint());
    AmazonS3Client s3Client = new AmazonS3Client(awsCredentials);

    // Upload Artifact to S3
    try {
        getLog().info("Uploading artifact to S3...");
        s3Client.putObject(bucketName, artifactKey, artifactFile);
    } catch (AmazonServiceException e) {
        throw new MojoExecutionException("[SERVICE] Could Not Upload File to S3", e);
    } catch (AmazonClientException e) {
        throw new MojoExecutionException("[CLIENT] Could Not Upload File to S3", e);
    }

    // Update each stack with the new artifact file
    for (String stackName : stackNames) {
        getLog().info("Cloud Formation Stack Name: " + stackName);
        String templateBody = getTemplateBody(cfClient, stackName);
        Stack stack = getStack(cfClient, stackName);

        // If passed additional parameters, update them
        List<Parameter> parameters = stack.getParameters();
        if (stackParameters != null && !stackParameters.isEmpty()) {
            List<Parameter> tmpParams = new ArrayList<Parameter>();

            // Add Existing Parameters we haven't locally overwritten
            for (Parameter oldParam : parameters) {
                String oldKey = oldParam.getParameterKey();
                if (!stackParameters.containsKey(oldKey)) {
                    tmpParams.add(oldParam);
                }
            }

            // Add Overwrite parameters
            for (String key : stackParameters.keySet()) {
                Parameter newParam = new Parameter();
                newParam.setParameterKey(key);
                newParam.setParameterValue(stackParameters.get(key));
                tmpParams.add(newParam);
            }
            parameters = tmpParams;
        }

        // Update the Stack
        UpdateStackRequest updateStackRequest = new UpdateStackRequest();
        updateStackRequest.setStackName(stackName);
        updateStackRequest.setTemplateBody(templateBody);
        updateStackRequest.setParameters(parameters);
        updateStackRequest.setCapabilities(stack.getCapabilities());
        try {
            getLog().info("Updating Cloud Formation Stack...");
            cfClient.updateStack(updateStackRequest);
        } catch (AmazonServiceException e) {
            throw new MojoExecutionException("[SERVICE] Could Not Update Cloud Formation Stack", e);
        } catch (AmazonClientException e) {
            throw new MojoExecutionException("[CLIENT] Could Not Update Cloud Formation Stack", e);
        }
        getLog().info("Cloud Formation Stack " + stackName + "is now updating...");
    }

    getLog().info("All stacks have been updated. Complete.");
}

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

License:Apache License

public void update(String templateBody, String templateUrl, Collection<Parameter> params, Collection<Tag> tags,
        long pollIntervallMillis, String roleArn) throws ExecutionException {
    try {//from   w  ww.j  a va2s  . co  m
        UpdateStackRequest req = new UpdateStackRequest();
        req.withStackName(this.stack).withCapabilities(Capability.CAPABILITY_IAM,
                Capability.CAPABILITY_NAMED_IAM);

        if (templateBody != null && !templateBody.isEmpty()) {
            req.setTemplateBody(templateBody);
        } else if (templateUrl != null && !templateUrl.isEmpty()) {
            req.setTemplateURL(templateUrl);
        } else {
            req.setUsePreviousTemplate(true);
        }

        req.withParameters(params).withTags(tags).withRoleARN(roleArn);

        this.client.updateStack(req);

        new EventPrinter(this.client, this.listener).waitAndPrintStackEvents(this.stack,
                this.client.waiters().stackUpdateComplete(), pollIntervallMillis);

        this.listener.getLogger().format("Updated CloudFormation stack %s %n", this.stack);

    } catch (AmazonCloudFormationException e) {
        if (e.getMessage().contains("No updates are to be performed")) {
            this.listener.getLogger().format("No updates were needed for CloudFormation stack %s %n",
                    this.stack);
            return;
        }
        this.listener.getLogger().format("Failed to update CloudFormation stack %s %n", this.stack);
        throw e;
    }
}

From source file:jp.classmethod.aws.gradle.cloudformation.AmazonCloudFormationMigrateStackTask.java

License:Apache License

private void updateStack(AmazonCloudFormation cfn) throws IOException {
    // to enable conventionMappings feature
    String stackName = getStackName();
    String cfnTemplateUrl = getCfnTemplateUrl();
    File cfnTemplateFile = getCfnTemplateFile();
    List<Parameter> cfnStackParams = getCfnStackParams();
    List<Tag> cfnStackTags = getCfnStackTags();
    String cfnStackPolicyUrl = getCfnStackPolicyUrl();
    File cfnStackPolicyFile = getCfnStackPolicyFile();

    getLogger().info("Update stack: {}", stackName);
    UpdateStackRequest req = new UpdateStackRequest().withStackName(stackName).withParameters(cfnStackParams)
            .withTags(cfnStackTags);//from w w w. j a  va 2s.  com

    // If template URL is specified, then use it
    if (Strings.isNullOrEmpty(cfnTemplateUrl) == false) {
        req.setTemplateURL(cfnTemplateUrl);
        getLogger().info("Using template url: {}", cfnTemplateUrl);
        // Else, use the template file body
    } else {
        req.setTemplateBody(FileUtils.readFileToString(cfnTemplateFile));
        getLogger().info("Using template file: {}", "$cfnTemplateFile.canonicalPath");
    }
    if (isCapabilityIam()) {
        Capability selectedCapability = (getUseCapabilityIam() == null) ? Capability.CAPABILITY_IAM
                : getUseCapabilityIam();
        getLogger().info("Using IAM capability: " + selectedCapability);
        req.setCapabilities(Arrays.asList(selectedCapability.toString()));
    }

    // If stack policy is specified, then use it
    if (Strings.isNullOrEmpty(cfnStackPolicyUrl) == false) {
        req.setStackPolicyURL(cfnStackPolicyUrl);
        // Else, use the stack policy file body if present
    } else if (cfnStackPolicyFile != null) {
        req.setStackPolicyBody(FileUtils.readFileToString(cfnStackPolicyFile));
    }

    UpdateStackResult updateStackResult = cfn.updateStack(req);
    getLogger().info("Update requested: {}", updateStackResult.getStackId());
}

From source file:org.xmlsh.aws.cfnUpdateStack.java

License:BSD License

private int updateStack(List<XValue> args, Options opts)
        throws IOException, XMLStreamException, SaxonApiException, CoreException {

    OutputPort stdout = getStdout();/*  w ww  . j  a  v  a 2  s . c om*/
    mWriter = new SafeXMLStreamWriter(stdout.asXMLStreamWriter(getSerializeOpts()));

    startDocument();
    startElement(getName());

    UpdateStackRequest request = new UpdateStackRequest();

    // "capability:+,disable-rollback,notification-arn:+,name:,template:,timeout:,tag:+");

    if (opts.hasOpt("capability"))
        request.setCapabilities(Util.toStringList(opts.getOptValues("capability")));

    request.setStackName(opts.getOptStringRequired("name"));

    if (opts.hasOpt("template-file"))
        request.setTemplateBody(Util.readString(mShell.getFile(opts.getOptValue("template-file")),
                getSerializeOpts().getInput_text_encoding()));
    else
        request.setTemplateURL(opts.getOptStringRequired("template-url"));

    request.setParameters(getParameters(args));

    traceCall("updateStack");

    UpdateStackResult result = getAWSClient().updateStack(request);

    writeStackResult(result);

    endElement();
    endDocument();
    closeWriter();

    stdout.writeSequenceTerminator(getSerializeOpts());

    return 0;

}

From source file:org.xmlsh.aws.gradle.cloudformation.AmazonCloudFormationMigrateStackTask.java

License:BSD License

private void updateStack(AmazonCloudFormation cfn) {
    // to enable conventionMappings feature
    String stackName = getStackName();
    List<Parameter> cfnStackParams = getCfnStackParams();

    getLogger().info("update stack: {}", stackName);
    UpdateStackRequest req = new UpdateStackRequest().withStackName(stackName).withParameters(cfnStackParams)
            .withUsePreviousTemplate(isUsePreviousTemplate());
    if (getTemplateBody() != null)
        req.setTemplateBody(getTemplateBody());

    if (isCapabilityIam()) {
        req.setCapabilities(Arrays.asList(Capability.CAPABILITY_IAM.toString()));
    }//  w w  w  .j  av a  2s .c  o  m
    UpdateStackResult updateStackResult = cfn.updateStack(req);
    getLogger().info("update requested: {}", updateStackResult.getStackId());

}

From source file:org.xmlsh.aws.gradle.cloudformation.AmazonCloudFormationUpdateStackTask.java

License:BSD License

private void updateStack(AmazonCloudFormation cfn) {
    // to enable conventionMappings feature
    String stackName = getStackName();
    List<Parameter> cfnStackParams = getCfnStackParams();

    getLogger().info("update stack: {}", stackName);
    UpdateStackRequest req = new UpdateStackRequest().withStackName(stackName).withParameters(cfnStackParams)
            .withUsePreviousTemplate(isUsePreviousTemplate());
    if (getTemplateBody() != null)
        req.setTemplateBody(getTemplateBody());

    if (isCapabilityIam()) {
        req.setCapabilities(Arrays.asList(Capability.CAPABILITY_IAM.toString()));
    }//from w w w . j  av a  2 s  . c om
    UpdateStackResult updateStackResult = cfn.updateStack(req);
    getLogger().info("update requested: {}", updateStackResult.getStackId());
}