List of usage examples for com.amazonaws.services.cloudformation AmazonCloudFormation createStack
CreateStackResult createStack(CreateStackRequest createStackRequest);
Creates a stack as specified in the template.
From source file:CloudFormation.java
License:Open Source License
public static void main(String[] args) throws Exception { /*/*from www . j av a 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 { /*/* w ww .j a v a 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:com.kinesisboard.amazonaws.utils.CloudFormationUtils.java
License:Open Source License
public static void createStackIfNotExists(AmazonCloudFormation client, KinesisConnectorConfiguration config) { String stackName = config.ELASTICSEARCH_CLOUDFORMATION_STACK_NAME; if (stackExists(client, stackName)) { StackStatus status = stackStatus(client, stackName); switch (status) { case CREATE_COMPLETE: case UPDATE_COMPLETE: LOG.info("Stack " + stackName + " already exists."); return; case CREATE_IN_PROGRESS: case UPDATE_IN_PROGRESS: case UPDATE_COMPLETE_CLEANUP_IN_PROGRESS: LOG.info("Stack " + stackName + " exists with status: " + status + ". Waiting for completion."); break; default:// w w w.j a v a 2 s. co m throw new IllegalStateException( "Stack " + stackName + " exists but has an invalid status: " + status); } } else { CreateStackRequest createStackRequest = new CreateStackRequest(); createStackRequest.setStackName(stackName); String templateBody = null; try { templateBody = loadTemplate(config.ELASTICSEARCH_CLOUDFORMATION_TEMPLATE_URL); } catch (IOException ioe) { LOG.error("Error reading template", ioe); throw new RuntimeException( "Could not load template at location: " + config.ELASTICSEARCH_CLOUDFORMATION_TEMPLATE_URL); } createStackRequest.setTemplateBody(templateBody); List<Parameter> parameters = new ArrayList<Parameter>(); parameters.add(new Parameter().withParameterKey("KeyName") .withParameterValue(config.ELASTICSEARCH_CLOUDFORMATION_KEY_PAIR_NAME)); parameters.add(new Parameter().withParameterKey("InstanceType") .withParameterValue(config.ELASTICSEARCH_CLOUDFORMATION_CLUSTER_INSTANCE_TYPE)); parameters.add(new Parameter().withParameterKey("SSHLocation") .withParameterValue(config.ELASTICSEARCH_CLOUDFORMATION_SSH_LOCATION)); parameters.add(new Parameter().withParameterKey("ClusterSize") .withParameterValue(config.ELASTICSEARCH_CLOUDFORMATION_CLUSTER_SIZE)); parameters.add(new Parameter().withParameterKey("ElasticsearchVersion") .withParameterValue(config.ELASTICSEARCH_VERSION_NUMBER)); createStackRequest.setParameters(parameters); List<String> capabilities = new ArrayList<String>(); capabilities.add("CAPABILITY_IAM"); createStackRequest.setCapabilities(capabilities); client.createStack(createStackRequest); LOG.info("Stack " + stackName + " is creating"); } // now wait for good status while (true) { try { Thread.sleep(1000 * 10); } catch (Exception e) { } StackStatus status = stackStatus(client, stackName); switch (status) { case CREATE_COMPLETE: case UPDATE_COMPLETE: return; case CREATE_IN_PROGRESS: case UPDATE_IN_PROGRESS: case UPDATE_COMPLETE_CLEANUP_IN_PROGRESS: break; default: throw new IllegalStateException("Stack " + stackName + " failed to create with status: " + status); } } }
From source file:com.netflix.spinnaker.clouddriver.aws.deploy.ops.DeployCloudFormationAtomicOperation.java
License:Apache License
private String createStack(AmazonCloudFormation amazonCloudFormation, String template, List<Parameter> parameters) { Task task = TaskRepository.threadLocalTask.get(); task.updateStatus(BASE_PHASE, "Preparing CloudFormation Stack"); CreateStackRequest createStackRequest = new CreateStackRequest().withStackName(description.getStackName()) .withParameters(parameters).withTemplateBody(template); task.updateStatus(BASE_PHASE, "Uploading CloudFormation Stack"); CreateStackResult createStackResult = amazonCloudFormation.createStack(createStackRequest); return createStackResult.getStackId(); }
From source file:io.konig.maven.CreateCloudFormationStackAction.java
License:Apache License
public AwsDeployment from(String path) throws Exception { File jsonFile = deployment.file(path); ObjectMapper mapper = new ObjectMapper(); CloudFormationTemplate cfTemplate = mapper.readValue(jsonFile, CloudFormationTemplate.class); Properties properties = System.getProperties(); StringWriter result = new StringWriter(); properties.setProperty("file.resource.loader.path", jsonFile.getParent()); VelocityEngine engine = new VelocityEngine(properties); Template template = engine.getTemplate(cfTemplate.getTemplate(), "UTF-8"); VelocityContext context = new VelocityContext(); context.put("beginVar", "${"); context.put("endVar", "}"); for (Object key : properties.keySet()) { String k = (String) key; context.put(k, properties.getProperty(k)); }// www .j a va2s . co m template.merge(context, result); String strResult = result.toString(); CreateStackRequest request = new CreateStackRequest().withTemplateBody(strResult) .withStackName(cfTemplate.getStackName()).withCapabilities(Capability.CAPABILITY_IAM); AmazonCloudFormation amazonClient = AmazonCloudFormationClientBuilder.standard() .withCredentials(AWSCloudFormationUtil.getCredential()) .withRegion(Regions.fromName(cfTemplate.getRegion())).build(); CreateStackResult stackResult = amazonClient.createStack(request); List<Output> outputs = getOutputForRequest(cfTemplate.getStackName(), amazonClient); deployment.setResponse("Stack creation complete. Outputs::" + (outputs == null ? "" : outputs.toString())); return deployment; }
From source file:jp.classmethod.aws.gradle.cloudformation.AmazonCloudFormationMigrateStackTask.java
License:Apache License
private void createStack(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(); String cfnOnFailure = getCfnOnFailure(); getLogger().info("create stack: {}", stackName); CreateStackRequest req = new CreateStackRequest().withStackName(stackName).withParameters(cfnStackParams) .withTags(cfnStackTags).withOnFailure(cfnOnFailure); // If template URL is specified, then use it if (Strings.isNullOrEmpty(cfnTemplateUrl) == false) { req.setTemplateURL(cfnTemplateUrl); // Else, use the template file body } else {//from w w w .j a v a2 s . c o m req.setTemplateBody(FileUtils.readFileToString(cfnTemplateFile)); } 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 } else if (cfnStackPolicyFile != null) { req.setStackPolicyBody(FileUtils.readFileToString(cfnStackPolicyFile)); } CreateStackResult createStackResult = cfn.createStack(req); getLogger().info("create requested: {}", createStackResult.getStackId()); }
From source file:org.xmlsh.aws.gradle.cloudformation.AmazonCloudFormationCreateStackTask.java
License:BSD License
private void createStack(AmazonCloudFormation cfn) { // to enable conventionMappings feature String stackName = getStackName(); List<Parameter> cfnStackParams = getCfnStackParams(); getLogger().info("create stack: {}", stackName); CreateStackRequest req = new CreateStackRequest().withStackName(stackName).withParameters(cfnStackParams) .withTemplateBody(getTemplateBody()).withDisableRollback(isDisableRollback()) .withOnFailure(getOnFailure()).withTags(getTags()); if (isCapabilityIam()) { req.setCapabilities(Arrays.asList(Capability.CAPABILITY_IAM.toString())); }/*from ww w . j av a 2s . c o m*/ CreateStackResult createStackResult = cfn.createStack(req); getLogger().info("create requested: {}", createStackResult.getStackId()); }
From source file:org.xmlsh.aws.gradle.cloudformation.AmazonCloudFormationMigrateStackTask.java
License:BSD License
private void createStack(AmazonCloudFormation cfn) { // to enable conventionMappings feature String stackName = getStackName(); List<Parameter> cfnStackParams = getCfnStackParams(); getLogger().info("create stack: {}", stackName); CreateStackRequest req = new CreateStackRequest().withStackName(stackName).withParameters(cfnStackParams) .withTemplateBody(getTemplateBody()); if (isCapabilityIam()) { req.setCapabilities(Arrays.asList(Capability.CAPABILITY_IAM.toString())); }/*from www .j a va 2 s . c om*/ CreateStackResult createStackResult = cfn.createStack(req); getLogger().info("create requested: {}", createStackResult.getStackId()); }
From source file:org.xmlsh.aws.gradle.cloudformation.AmazonCloudFormationUpdateStackTask.java
License:BSD License
private void createStack(AmazonCloudFormation cfn) { // to enable conventionMappings feature String stackName = getStackName(); String cfnTemplateUrl = getCfnTemplateUrl(); List<Parameter> cfnStackParams = getCfnStackParams(); getLogger().info("create stack: {}", stackName); CreateStackRequest req = new CreateStackRequest().withStackName(stackName).withTemplateURL(cfnTemplateUrl) .withParameters(cfnStackParams); if (isCapabilityIam()) { req.setCapabilities(Arrays.asList(Capability.CAPABILITY_IAM.toString())); }//from w ww.ja va2 s . co m CreateStackResult createStackResult = cfn.createStack(req); getLogger().info("create requested: {}", createStackResult.getStackId()); }