def delete(self, wait=False): cf_stack = AWSSession().resource.Stack(self.StackName) cf_stack.delete() if wait: waiter = AWSSession().client.get_waiter( "stack_delete_complete" ) waiter.wait(StackName=self.StackName)
def create(self, wait=False): self.validate() AWSSession().resource.create_stack(**self.__dict__) if wait: waiter = AWSSession().client.get_waiter( "stack_create_complete" ) waiter.wait(StackName=self.StackName)
def update(self, wait=False, review=True): self.validate() if review: self.manage_change_set() else: cf_stack = AWSSession().resource.Stack(self.StackName) cf_stack.update(**self.__dict__) if wait: waiter = AWSSession().client.get_waiter( "stack_update_complete" ) waiter.wait(StackName=self.StackName)
def get_aws_stack_output(stack, output): if not STACK_CACHE.get(stack): STACK_CACHE[stack] = AWSSession().resource.Stack(stack) for stack_output in STACK_CACHE[stack].outputs: if stack_output["OutputKey"] == output: return stack_output["OutputValue"]
def validate(self): try: AWSSession().client.validate_template( TemplateBody=self.TemplateBody ) except ClientError as exc: raise SystemExit(exc.response["Error"]["Message"])
def changeset_user_input(self, change_set_name): answer = input("Execute(e), Delete (d), or Keep(k) change set? ") if answer == "e": print("Executing changeset {}...".format(change_set_name)) AWSSession().client.execute_change_set( ChangeSetName=change_set_name, StackName=self.StackName ) elif answer == "d": print("Deleting changeset {}. No changes made to stack {}".format(change_set_name, self.StackName)) # noqa AWSSession().client.delete_change_set( ChangeSetName=change_set_name, StackName=self.StackName ) elif answer == "k": print("Changeset {} unchanged. No changes made to stack {}".format(change_set_name, self.StackName)) # noqa else: print("Valid answers: e, d, k") return False return True
def manage_change_set(self, wait=False): # find build ID in tags for tag in self.Tags: if tag["Key"] == "build_id": build_id = tag["Value"] change_set_name = "{}-{}".format(self.StackName, build_id) AWSSession().client.create_change_set( ChangeSetName=change_set_name, ChangeSetType="UPDATE", **self.__dict__ ) # wait for change set to be ready time.sleep(2) waiter = AWSSession().client.get_waiter( "change_set_create_complete" ) waiter.wait(ChangeSetName=change_set_name, StackName=self.StackName) change_set = AWSSession().client.describe_change_set( ChangeSetName=change_set_name, StackName=self.StackName ) change_set.pop("ResponseMetadata") print("---------- Change Set ----------") print(yaml.dump(change_set, indent=2)) print("--------------------------------") answer = False while not answer: answer = self.changeset_user_input(change_set_name) if wait: waiter = AWSSession().client.get_waiter( "stack_update_complete" ) waiter.wait(StackName=self.StackName)