Search code examples
javaamazon-web-servicesaws-cloudformationaws-java-sdk

AWS SDK AmazonCloudFormationClient template version issue


I am using the newest version of the AWS Java SDK (1.10.67) and am exploring using the CloudFormation service and creating the stacks using the Java API. I have successfully created stacks using the AWS console, but am running into issues when using the API.

No matter what method I call using the AmazonCloudFormationClient, it throws the same error (the "operation" name changes based on which method I call). I also noticed that I get the same error if I don't define a templateURL or templateBody, so I am wondering if it is not able to parse my template file. But the AWS console reads in the template just fine.

com.amazonaws.AmazonServiceException: Could not find operation DescribeStackResources for version 2010-05-15 (Service: AmazonCloudFormation; Status Code: 400; Error Code: InvalidAction; Request ID: *****)

On the call above, in particular, I am confused as to why it is even looking at the template version because it should just be grabbing the previously created stacks. The template I am using has the newest template version of "2010-09-09" so I don't understand where it is getting that version number.

Here are snippets of the different pieces of code I have used:

GetTemplateSummaryRequest templateSummaryRequest = new GetTemplateSummaryRequest()
    .withTemplateURL("https://s3.amazonaws.com/{bucket}/EC2_POC_CloudFormation.template");
GetTemplateSummaryResult templateSummary = cfClient.getTemplateSummary(templateSummaryRequest);

CreateStackRequest stack = new CreateStackRequest()
    .withStackName(stackName)
    //.withTemplateURL("https://s3.amazonaws.com/{bucket}/EC2_POC_CloudFormation.template")
    .withTemplateBody(cloudFormationTemplateString)
    .withParameters(stackParams);
cfClient.createStack(stack);

DescribeStackResourcesRequest stackResourceRequest = new DescribeStackResourcesRequest()
    .withStackName(existingStackName);
for (StackResource resource : cfClient.describeStackResources(stackResourceRequest).getStackResources()) {
    LOGGER.info(resource.getStackName()+" - "+resource.getResourceType()+", "+resource.getResourceStatus());
}

Any help or insight would be much appreciated as I have spent over 5 hours trying different things.


Solution:
Using Spring with beans:

<bean id="awsproperties" class="com.amazonaws.auth.PropertiesCredentials">
    <constructor-arg type="java.io.File" value="classpath:AwsCredentials.properties"/>
</bean>

<bean id="cfBean" class="com.amazonaws.services.cloudformation.AmazonCloudFormationClient">
    <constructor-arg ref="awsproperties" type="com.amazonaws.auth.AWSCredentials"/>
    <property name="endpoint" value="cloudformation.us-east-1.amazonaws.com"/>     
</bean> 


Using Java:

AmazonCloudFormation cfClient = new AmazonCloudFormationClient(awsCredentials);
cfClient.setRegion(Region.getRegion(Regions.US_EAST_1));

Solution

  • I was finally able to find the solution. The error message thrown by AWS is not at all relevant to the issue.

    For people running into this issue in the future, the problem was in my config for the AmazonCloudFormationClient. I had forgotten to set the region. Once I did that, the problem disappeared.