amazon web services - Best way to check whether a cloud formation stack exists using AWS Java SDK? -
what best way check whether stack exists using aws java sdk, given stack name?
i've tried below code based on - https://github.com/aws/aws-sdk-java/blob/master/src/samples/awscloudformation/cloudformationsample.java
describestacksrequest wait = new describestacksrequest(); wait.setstackname(stackname); list<stack> stacks = awscftclient.describestacks(wait).getstacks(); if (stacks.isempty()) { logger.log("no_such_stack"); return true; }
however, getting:
amazonserviceexception:com.amazonaws.services.cloudformation.model.amazoncloudformationexception: stack id "stackname" not exist.
thanks in advance!
in case else looking quick , dirty solution, works,
//returns true if stack exists public boolean stackexists(amazoncloudformation awscftclient, string stackname) throws exception{ describestacksrequest describe = new describestacksrequest(); describe.setstackname(stackname); //if stack not exist exception describe stack try { awscftclient.describestacks(describe).getstacks(); } catch (exception e) { logger.log("error message: " + e.getmessage()); if (e.getmessage().matches("(.*)" + stackname + "(.*)does not exist(.*)")) { return false; } else { throw e; } } return true; }
if there better way doing this, please let me know.
wiki
Comments
Post a Comment