def delete_stack(context, args): if context.config.project_stack_id is None: raise HandledError("Project stack does not exist.") if context.config.deployment_names: raise HandledError( 'The project has {} deployment stack(s): {}. You must delete these stacks before you can delete the project stack.' .format( len(context.config.deployment_names), ', '.join( deployment_name for deployment_name in context.config.deployment_names))) if context.stack.id_exists(context.config.project_stack_id): logs_bucket_id = context.stack.get_physical_resource_id( context.config.project_stack_id, 'Logs', optional=True, expected_type='AWS::S3::Bucket') pending_resource_status = __get_pending_resource_status(context, deleting=True) context.stack.confirm_stack_operation(context.config.project_stack_id, 'project stack', args, pending_resource_status) context.stack.delete(context.config.project_stack_id, pending_resource_status=pending_resource_status) if logs_bucket_id: s3 = context.aws.client('s3') # Check to see if the bucket still exists, old versions of project-template.json # don't have DeletionPolicy="Retain" on this bucket. try: s3.head_bucket(Bucket=logs_bucket_id) bucket_still_exists = True except: bucket_still_exists = False if bucket_still_exists: stack_name = util.get_stack_name_from_arn( context.config.project_stack_id) util.delete_bucket_contents(context, stack_name, "Logs", logs_bucket_id) context.view.deleting_bucket(logs_bucket_id) s3.delete_bucket(Bucket=logs_bucket_id) else: context.view.clearing_project_stack_id(context.config.project_stack_id) context.config.clear_project_stack_id()
def __remove_bucket_contents(self, stack_id, logical_resource_id): physical_bucket_id = self.get_physical_resource_id( stack_id, logical_resource_id) stack_name = util.get_stack_name_from_arn(stack_id) util.delete_bucket_contents(self.context, stack_name, logical_resource_id, physical_bucket_id)